Related Questions

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

Asked by MathWorks Support Team on 7 May 2013
Latest activity Commented on by Alexander Mering on 28 Nov 2013

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


MathWorks Support Team


1 Answer

Answer by MathWorks Support Team on 18 Oct 2013
Accepted answer

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) Create a java.opts file in the $matlabroot/bin/<arch>

(where $matlabroot is the result of the MATLAB command matlabroot and <arch> is the system architecture).

3) Add the following lines to the java.opts file

4) Restart MATLAB.

5) 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 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.

MathWorks Support Team

Contact us