Asked by MathWorks Support Team
on 6 May 2013

I am using a MATLAB version that is newer than MATLAB 6.0 (R12).*

How do I increase Java VM heap space?

- Note that the information in this solution is applicable to recent releases such as MATLAB 7.14 (R2012a).

Answer by MathWorks Support Team
on 18 Oct 2013

Accepted answer

This enhancement has been incorporated in Release 2010a (R2010a). For previous product releases, read below for any possible workarounds:

For MATLAB 7.10 (R2010a) onwards you can change this setting using the preferences dialog box. For more information, refer to the documentation page on 'Java Heap Memory Preferences'. This can be accessed as follows:

1. Open the help browser by typing at the MATLAB Command Prompt:

doc

2. Search for 'Java Heap Memory Preferences' on the search bar of this documentation browser.

For previous releases please see the instructions given below as to how the Java heap space memory can be increased.

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 java.opts file:

Create a text file named java.opts in the $MATLABROOT/bin/$ARCH directory. $MATLABROOT is the MATLAB root directory and $ARCH is your system architecture, which you find by typing at the MATLAB Command Prompt:

matlabroot computer('arch')

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 MATLAB startup directory can be found by executing the following at the MATLAB Command Prompt (before any MATLAB code is executed):

pwd

If you use MATLAB 7.7 (R2008b) or later versions on a MAC OS system, MATLAB default installation directory 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.

2. Determine the version of the Java Virtual Machine (JVM) in use:

The contents of java.opts depend on your version of the JVM. 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>

3. Determine the value to put in the java.opts file:

When MATLAB is launched, it starts up the Java Runtime Environment. MATLAB uses a set of default values for the initial and maximum heap size (managed by the heap manager). When the free memory nears zero, Java will dynamically increase the heap size (up to the Max Heap Size setting). 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 space usage in MATLAB:

java.lang.Runtime.getRuntime.maxMemory java.lang.Runtime.getRuntime.totalMemory java.lang.Runtime.getRuntime.freeMemory

Users can override these values by setting them manually in a java.opts file. For example, including the following line in a java.opts file sets the Max Heap Size value to 256 MB:

For JVM of 1.2.2 and later, place the following in your java.opts file:

-Xmx256m

For JVM 1.1.8 'java.opts', place the following in your java.opts file:

-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. For more information, refer the following guide on Memory Management:

<http://www.mathworks.com/support/tech-notes/1100/1106.html>

4. 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/>

Answer by Igor Varfolomeev
on 19 Jun 2015

Although the Answer by MathWorks Support Team on 18 Oct 2013 is correct for old matlab versions, here's a cople of notes, concerning more recent versions:

-------

**1.** First of all, now "Max Java Heap Memory" parameter could be changed via a slider in Preferences->Matlab->General->Java Heap Memory

**2.** If the maximum value, allowed by this slider, is too small for your needs, (e.g. R2014b only allows me to set it to 49GB, while physically I have 192GB), it could be directly adjusted by editing

"c:\Users\%USERNAME%\AppData\Roaming\MathWorks\MATLAB\R2014b\matlab.prf"

**3.** Open it with a text editor and modify "JavaMemHeapMax" value. It should look like

JavaMemHeapMax=I190000

**4.** Restart Matlab

-------

**Notes:**

- Since this preferences file is in your AppData folder, you even don't need administrative permissions (in contrast to adding "java.opts" to $MATLABROOT, as discussed in the mentioned answer.

- To check the actual value, used by Matlab (in GB):

>> java.lang.Runtime.getRuntime.maxMemory/1e9

- Preferences UI works OK with this new value (thanks, Mathwoks!). It just displays a warning, that it is larger, then recommended.

Answer by Igor Varfolomeev
on 19 Jun 2015

Although the Answer by MathWorks Support Team on 18 Oct 2013 is correct for old matlab versions, here's a cople of notes, concerning more recent versions:

-------

**1.** First of all, now "Max Java Heap Memory" parameter could be changed via a slider in Preferences->Matlab->General->Java Heap Memory

**2.** If the maximum value, allowed by this slider, is too small for your needs, (e.g. R2014b only allows me to set it to 49GB, while physically I have 192GB), it could be directly adjusted by editing

"c:\Users\%USERNAME%\AppData\Roaming\MathWorks\MATLAB\R2014b\matlab.prf"

**3.** Open it with a text editor and modify "JavaMemHeapMax" value. It should look like

JavaMemHeapMax=I190000

**4.** Restart Matlab

-------

**Notes:**

- Since this preferences file is in your AppData folder, you even don't need administrative permissions (in contrast to adding "java.opts" to $MATLABROOT, as discussed in the mentioned answer.

- To check the actual value, used by Matlab (in GB):

>> java.lang.Runtime.getRuntime.maxMemory/1e9

- Preferences UI works OK with this new value (thanks, Mathwoks!). It just displays a warning, that it is larger, then recommended.

Answer by Lucilla
on 21 Nov 2014

I'm using Matlab 2012a in Yosemite (after patching it with R2012a_patch_1098655) with the Java version 1.6.0_65-b14-466.1-11M4716 with Apple Inc. Java HotSpot™ 64-Bit Server VM mixed mode.

My current Java heap memory max size is set to 256MB and I can not increase it. I've tried both methods described below that is changing the value from the Preferences window and creating a java.opts file. None of these solutions worked for me. I'm stuck with 256MB.

Any suggestion?

Answer by Benigno Barbés Fernández
on 12 Nov 2015 at 17:07

Thanks a lot for your help. At the end, my problem was very basic: I had not Java in the new computer. After installing it, everything worked.

Bni

Answer by Benigno Barbés Fernández
on 12 Nov 2015 at 17:41

Thanks a lot for your help. At the end, my problem was very basic: I had not Java in the new computer. After installing it, everything worked.

Bni

Answer by Benigno Barbés Fernández
on 12 Nov 2015 at 17:43

Thanks a lot for your help. At the end, my problem was very basic: I had not Java in the new computer. After installing it, everything worked.

Bni

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

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments