PlainError

java.lang.OutOfMemoryError: Java heap space

Last checked 2026-10-26 · 3 fixes · reviewed answer

TL;DRThe Java program ran out of memory allocated for its heap, which stores objects and data during execution.

Most likely cause: The application processes large amounts of data or has inefficient memory usage

Try first: Review code for large data structures or loops that accumulate objects

Likely causes

  1. The application processes large amounts of data or has inefficient memory usagemost likely
  2. Default or configured heap size is too small for the workloadpossible
  3. Memory leak in the code where objects are not releasedpossible

How to fix it

Safest first: SafeChanges settingsDangerous
  1. Review code for large data structures or loops that accumulate objectsSafe

    Look for places where lists, arrays, or caches grow without bound and add limits or clearing logic.

  2. Monitor memory usage during executionSafe

    Add JVM flags to print heap statistics and review the output for patterns.

    java -XX:+PrintGCDetails -XX:+PrintGCTimeStamps YourProgram
  3. Run the program with a larger heap sizeChanges settings

    Increase the maximum heap using the -Xmx flag when starting Java, for example to 2 gigabytes.

    java -Xmx2g YourProgram

If that didn't fix it

Use a heap dump tool to capture and analyze memory contents at the time of the error.

When to see a technician

Consult a Java developer if the error occurs in production code you cannot modify or debug yourself.

Quick answers

What does java.lang.OutOfMemoryError: Java heap space mean?
The Java program ran out of memory allocated for its heap, which stores objects and data during execution.
What causes java.lang.OutOfMemoryError: Java heap space?
The application processes large amounts of data or has inefficient memory usage. (most likely) Default or configured heap size is too small for the workload. (possible) Memory leak in the code where objects are not released. (possible)
How do I fix java.lang.OutOfMemoryError: Java heap space?
Try these in order, safest first: 1. Review code for large data structures or loops that accumulate objects. 2. Monitor memory usage during execution. 3. Run the program with a larger heap size.
What if that didn't fix it?
Use a heap dump tool to capture and analyze memory contents at the time of the error.
When should I see a technician?
Consult a Java developer if the error occurs in production code you cannot modify or debug yourself.
Did this fix it? Report a problem

Related errors

All Coding errors →