Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

Technical Solutions

How do I increase the heap space for the Java VM in MATLAB 6.0 (R12) and later versions?


Date Last Modified: Wednesday, November 18, 2009
Solution ID:   1-18I2C
Product:   MATLAB
Reported in Release:   R12
Platform:   All Platforms
Operating System:   All OS
 

Subject:

How do I increase the heap space for the Java VM in MATLAB 6.0 (R12) and later versions?

Problem Description:

I would like to increase the Java VM heap space in MATLAB 6.0 (R12) and later versions.

Solution:

As of MATLAB 6.0 (R12), you can increase the heap space for the Java Virtual machine (VM) by using the following file:


java.opts

1. Create a text file named java.opts in the $MATLABROOT/bin/$ARCH directory. If you do not have write-access to $MATLABROOT/bin/$ARCH, or if you want a user-specific version, create the java.opts file in the MATLAB startup directory. The contents of java.opts depends on your version of the Java JVM.

$MATLABROOT is the MATLAB root directory, which you find by typing at the MATLAB Command Prompt:
matlabroot

$ARCH is your system architecture, which you find by typing at the MATLAB Command Prompt:
computer('arch')

Starting from MATLAB 7.7 (R2008b), MATLAB default installation directory on the MAC OS is

/Applications/MATLAB_<Release>.app
where <Release> is the MATLAB Release's version, i.e., R2008b, R2009a, etc.
To access the contents of the /Applications/MATLAB_<Release>.app directory, in Finder, navigate to Applications and right-click on MATLAB_<Release>.app. Click 'Show Package Contents' and you will be able to see the contents of the directory.

The MATLAB startup directory can be found in two ways:

a) If you launch MATLAB from the command line, it is the current working directory in the command-line window.

b) If you launch MATLAB from a desktop icon, execute the following at the first MATLAB Command Prompt (before any M-code is executed):
pwd

To determine the version of the JVM you are running, type the following at the MATLAB Command Prompt:
version -java

NOTE: If you are performing these operations in MATLAB 7.5 (R2007b), read this bug report:

http://www.mathworks.com/support/bugreports/details.html?rp=398525

2. Determine the value to put in the java.opts file.

The following table shows the default settings MATLAB uses for versions of the JVM:

JVM Initial Heap Size Max Heap Size
1.6.0 -Xms64m -Xmx128m (32-bit)
-Xmx196m (64-bit)
1.5.0 -Xms64m -Xmx96m (32-bit)
-Xmx128m (64-bit)
1.4.2 -Xms16m -Xmx96m
1.3.1 -Xms16000000 -Xmx64000000
1.2.2 -Xms16000000 -Xmx64000000
1.1.8 -ms16000000 -mx64000000

You can use the following commands to obtain information about the Java heap:
java.lang.Runtime.getRuntime.maxMemory
java.lang.Runtime.getRuntime.totalMemory
java.lang.Runtime.getRuntime.freeMemory
When the free memory nears zero, Java will increase the heap size (up to the Max Heap Size setting).

The java.opts file contains the new value MATLAB should use as the Max Heap Size. The contents of the file is as follows, using the suggested value of 256MB:

For JVM of 1.2.2 and later, 'java.opts' contains:

-Xmx256m

For JVM 1.1.8 'java.opts' appears as follows:

-mx256m
This will give you 256MB of JVM memory and you can adjust the parameter as needed. Remember that increasing the size of the Java heap may correspondingly decrease the amount of space available for MATLAB arrays.

3. Troubleshooting.

In some cases, increasing the Java heap size above a certain limit can cause Java memory problems. See this solution for more information:
 
http://www.mathworks.com/support/bugreports/details.html?rp=398525

If you are increasing the Java heap size in response to an "OutOfMemoryError: Java heap space", and you get the same error with the larger heap, you may have a memory leak, or you may be retaining references to Java objects beyond their usefulness. The Java SE Troubleshooting guide may help isolate the problem:

http://java.sun.com/javase/6/webnotes/trouble/

 

Related Solutions:

Please provide feedback to help us improve this Solution
Contact support
E-mail this page
Print this page