MATLAB: How to request Java garbage collection in the Java Virtual Machine (JVM) in MATLAB 7.3 (R2006b)

javaMATLABmemoryreclaimvm

I would like to request Java garbage collection in the Java Virtual Machine (JVM) in MATLAB 7.3 (R2006b).

Best Answer

While it is not possible to force the Java VM to release memory of de-referenced objects, you can request that the JVM performs garbage collection. This may result in the desired memory being released. Issue the following command from the MATLAB command prompt:
java.lang.System.gc()
Another method to request Java garbage collection is to create a Java Runtime object and call its 'gc' method. This can be done at the MATLAB command prompt with:
java.lang.Runtime.getRuntime().gc
Garbage collection is run as separate thread in the JVM. The garbage collector (GC) runs as Java priority 1 (the lowest priority). The virtual machine, VM, runs at Java priority 10 (the highest priority). In most cases, the GC runs during idle times, generally when the VM is waiting for user input or when available memory is low. In the latter case, the GC interrupts high-priority processing in the application in order to free memory.