MATLAB Answers

How can I monitor MATLAB's JVM heap memory usage?

21 views (last 30 days)
I would like to investigate the Java Virtual Machine (JVM) memory profile as my MATLAB code is executing.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Oct 2015
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
Information on JMP is available here:
Here are the steps to monitor Java VM heap memory using JConsole:
1) You will need to install Oracle's Java Developer Kit (JDK) for the version of the JRE being used by MATLAB. This can be downloaded from
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
3) Restart MATLAB.
4) Open the JConsole utility from MATLAB
system('"<JDKBIN>\jconsole" <PID> &')
where <PID> is the process ID for this MATLAB session and can be found from the MATLAB command
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
  1 Comment
Alexander Mering
Alexander Mering on 28 Nov 2013
While trying to find the reason for a debugger problem looking at the heap space, I realized (in contact with matlab support) that the given solution leads to two major problems:
a) It is not possible anymore to start more than one matlab instance at once!
b) The mcc compiler stops with an error:
Fehler: Agent-Ausnahmefehler :
java.rmi.server.ExportException: Listen failed on port: 9999;
nested exception is:
Unrecognized Windows Sockets error: 0:
JVM_Bind Error using mcc Error executing mcc, return status = 1 (0x1)
In both cases, the error is related to the fixation of port 9999 in above solution. Removing this line from the java.opts results in a non-working JConsole (as expected) and working mcc and multiple instances.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!