How can I monitor MATLAB's JVM heap memory usage?
7 views (last 30 days)
Show older comments
MathWorks Support Team
on 7 May 2013
Edited: MathWorks Support Team
on 15 May 2025
I would like to investigate the Java Virtual Machine (JVM) memory profile as my MATLAB code is executing.
Accepted Answer
MathWorks Support Team
on 15 May 2025
Edited: MathWorks Support Team
on 15 May 2025
The best way to monitor is to use "jvisualvm" which comes with the JAVA JDK and which can attach to MATLAB JVM without any needed changes on the MATLAB side.
An alternative would be to monitor the memory usage of the JVM by using "JConsole". Note that this will prevent starting a second MATLAB instance and may lead to problems with the MATLAB Compiler ("mcc"). Additionally there are some 3rd party tools, such as "JMP", which provide more features. With either option, you must use a tool which is compatible with the version of the Java Runtime Environment (JRE) which MATLAB is using. You can find the version of the JRE by executing the following command at the MATLAB prompt:
version -java
For more information on JMP see JMP - Java Memory Profiler
Here are the steps to monitor Java VM heap memory using JConsole:
1) You will need to download and install Oracle's Java Developer Kit (JDK) for the version of the JRE being used by MATLAB.
2) Skip this step if using Java 6 or above. Create a java.opts file in the $matlabroot/bin/<arch> directory, where $matlabroot is the result of the MATLAB command "matlabroot" and "<arch>" is the system architecture. Add the following lines to the java.opts file
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-XX:+PerfBypassFileSystemCheck
3) Restart MATLAB.
4) Open the JConsole utility from MATLAB
system('"<JDKBIN>\jconsole" <PID> &')
where "<PID>" is the process ID for this MATLAB session which can be found using the MATLAB command
(R2025a and later)
matlabProcessID
(R2024b and earlier)
feature getpid
and "<JDKBIN>" is the bin directory of the JDK installed in step 1.
For example the command may look like this
system('"C:\Program Files\Java\jdk1.5.0_07\bin\jconsole" 4296 &')
You should now see the JConsole window and be able to monitor the memory. For example, you should be able to see the effect of forcing Garbage Collection by entering the following command in MATLAB
java.lang.System.gc
0 Comments
More Answers (0)
See Also
Categories
Find more on Java Package Integration in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!