I am trying to do MATLAB calculations via JAVA using MatlabEngine But I ran into a memory leak when trying to transfer an array of data from JAVA to MATLAB via putVariable. The process occupies all available memory and swap and I don't understand where to dig...
JAVA code example
public class MemoryLeaks {
@Test
public void test() throws ExecutionException, InterruptedException {
String[] options = {"-nojvm"};
for (int a = 0; a < 1000; a++) {
MatlabEngine eng = MatlabEngine.startMatlab(options);
for (int i = 0; i < 100; i++) {
double[][] data = new double[600000][2];
eng.putVariable("data", (Object)data);
// eng.eval("[h,pValue,stat,cValue,mles] = jcitest(data)", NULL_WRITER, NULL_WRITER);
}
eng.quit();
Thread.sleep(5000L);
System.out.println("cycle "+a);
}
}
}
memory consumption after a short period
jcmd $pid VM.native_memory detail.diff
....
- Internal (reserved=32808312KB +5151601KB, committed=32808312KB +5151601KB)
(malloc=32808276KB +5151601KB #31150 +4405)
(mmap: reserved=36KB, committed=36KB)
- Other (reserved=10KB, committed=10KB)
(malloc=10KB #2)
- Symbol (reserved=1519KB, committed=1519KB)
(malloc=1159KB #16352)
(arena=360KB #1)
- Native Memory Tracking (reserved=1275KB +204KB, committed=1275KB +204KB)
(malloc=258KB +108KB #3843 +1716)
(tracking overhead=1018KB +96KB)
....
[0x00007fd9f8c49382] jni_GetDoubleArrayElements+0x162
[0x00007fd73acb871f]
[0x00007fd73acba487]
[0x00007fd73acba968]
(malloc=32807813KB type=Internal +5151563KB #6999 +1099)
while true
do
cat /proc/175836/smaps | grep -i 'Pss:' | awk '{Total+=$2} END {print "PSS: " Total}'
sleep 5
done
PSS: 27240963
PSS: 27775401
PSS: 28225477
PSS: 28267110
tried different versions of java, different scopes of variables, closing and not closing the MATLAB process, clearing memory in MATLAB via "clear all".