Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

If MATLAB reports java.lang.OutOfMemoryError: Java heap space or GC overhead limit exceeded, increase the Java heap in MATLAB’s settings, then restart MATLAB. First confirm the error is actually Java-related: MATLAB’s Java heap stores Java objects, not ordinary MATLAB arrays, and assigning more memory to it leaves less available for MATLAB data.

1. Confirm that the problem is the Java heap

Java heap errors identify memory exhaustion in the Java Virtual Machine (JVM). Look for messages such as:

  • java.lang.OutOfMemoryError: Java heap space
  • java.lang.OutOfMemoryError: GC overhead limit exceeded
  • A message about insufficient Java memory, or a failure while creating or manipulating Java objects

These can occur while using a Java library, database driver, Java-dependent toolbox, or Java-based document conversion. MATLAB Report Generator, for example, has a documented Java-memory failure mode during report generation (MathWorks: Java memory usage).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A freeze or generic “out of memory” message alone does not prove that Java is the cause. MATLAB arrays, native MATLAB and toolbox components, graphics, and the operating system use memory outside the Java heap. If the failing operation only manipulates MATLAB arrays, changing the Java heap may not help.

2. Increase the Java heap in MATLAB settings

For current releases, use the settings panel:

  1. On the Home tab, in Environment, select Settings.
  2. Go to MATLAB → General → Java Runtime Environment.
  3. Increase Java Heap Size using the slider or spinner.
  4. Select OK, then close and restart MATLAB.

The new allocation takes effect after a restart. MathWorks notes that if MATLAB cannot allocate the requested amount, it may restore the default and display an error dialog. Check for that message rather than assuming the change succeeded. See the Java heap settings documentation.

Older releases use a different menu

In older releases, including R2023b documentation, the setting is under Home → Preferences → MATLAB → General → Java Heap Memory. The labels and location changed; use the path shown by your release rather than assuming the older instructions apply. See the R2023b instructions.

3. Choose an allocation cautiously

There is no universally safe heap size or percentage of RAM to assign. Start from MATLAB’s default and increase in modest steps, testing the operation that failed after each restart. Keep enough memory available for MATLAB arrays, the operating system, and other applications. The right balance depends on installed RAM, other workloads, the size and lifetime of Java objects, and whether this is a one-off task or a persistent Java integration.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A larger Java heap is not extra memory for MATLAB as a whole: it can leave less room for workspace data. If the machine begins paging heavily, slows down, or becomes unresponsive, reduce the allocation. Avoid setting the maximum just because the control permits it.

4. Verify that MATLAB started with the new heap

In the MATLAB Command Window, check the JVM’s maximum heap, expressed here approximately in gigabytes:

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

You can also inspect the Java startup arguments:

java.lang.management.ManagementFactory.getRuntimeMXBean.getInputArguments

Look for an -Xmx argument, which specifies a maximum heap. The first command is a useful diagnostic; the second is documented by MathWorks for inspecting Java options supplied at startup (Java options documentation). The reported heap may not exactly match the settings control’s displayed value because of units and JVM behavior.

If the value did not change, make sure you restarted the correct MATLAB installation. If it did change but the failure remains, the operation may need more memory than is available, may be retaining Java objects, or may not be a Java-heap problem at all.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

5. Advanced fallback: configure java.opts

For desktop MATLAB, MathWorks recommends the settings panel for changing heap size. A java.opts text file is an advanced option for custom Java startup settings, such as when the UI cannot be reached or a controlled startup configuration is needed. Its location depends on the release and how MATLAB is launched, so follow the documentation for the specific installation rather than placing it at an assumed operating-system path.

A file contains one option per line. For example:

-Xmx2048m

This is an example, not a recommendation for every computer. Close all MATLAB processes before changing the file, restart MATLAB, and verify the effective arguments with the command above. MathWorks says file options are appended to MATLAB’s built-in Java options and warns that whether one option overrides another is JVM-dependent; a later -Xmx is not guaranteed to win in every configuration.

If MATLAB fails to start after you add the file, close MATLAB and remove or rename java.opts, then try launching again. Avoid editing MATLAB installation or preference files as a first-line fix. See MathWorks’ java.opts guidance.

6. If increasing the heap does not solve it

  • Java exception persists: Reproduce the smallest failing operation. Review Java code and integrations for large collections, buffers, or objects that remain referenced after use; a leak or retained data can exhaust even a larger heap.
  • MATLAB reports an array or workspace allocation problem: Inspect variables with whos, clear unused ones with clearvars, avoid unnecessary copies, and consider processing data in chunks. Where suitable, use a lower-precision type such as single, datastores, tall arrays, or matfile rather than loading all data at once.
  • MATLAB reports that an array exceeds a configured limit: Review the array-size preference, but do not disable safeguards unless the array can genuinely fit in available memory.
  • The computer is swapping or MATLAB becomes unresponsive: Lower the Java heap, close other memory-heavy applications, and assess total memory use. If you added java.opts, remove or rename it before retrying.
  • The issue is a report or export failure: Check whether the specific conversion step uses Java; not every report or export error is a heap problem.

MathWorks’ broader guidance covers MATLAB out-of-memory errors and alternatives such as data-management changes, datastores, and tall arrays (Resolving out-of-memory errors).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

7. When to reduce Java memory or use -nojvm

If you need memory for MATLAB arrays and do not use Java-dependent functionality, a smaller Java allocation may be more appropriate. In compatible workflows, starting MATLAB with -nojvm disables the JVM and can free memory for workspace data. It also removes JVM-dependent features, including desktop tools and graphics, so it is not a general-purpose fix for interactive MATLAB. Starting with -nodesktop does not provide substantial memory savings, according to MathWorks’ out-of-memory guidance.

8. Release and Java-version considerations

Java’s role and supported Java versions vary by MATLAB release. MathWorks’ community discussion says the desktop, editor, and graphics stack became less dependent on Java beginning with R2025a; treat that as release-specific context, not proof that every toolbox or integration no longer uses Java. A Java heap increase is therefore not a general fix for ordinary graphics or desktop problems in newer releases. Identify the actual exception and feature involved.

If you use a custom Java integration or select an external JRE/OpenJDK, check the compatibility table for your MATLAB release and platform. The supported versions change over time, and platform qualifications may apply. Consult MathWorks’ OpenJDK compatibility table rather than relying on a list copied from another release.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.