Skip to content
ohsudo, Home
Linux

java.lang.OutOfMemoryError: Java heap space: size the heap, then find what fills it

Fix "java.lang.OutOfMemoryError: Java heap space": check the default heap, set -Xmx or MaxRAMPercentage in containers, take a heap dump, and fix Maven and Gradle builds.

Intermediate3 min readApplies to Java 11+ · OpenJDK · Eclipse Temurin · Spring Boot · Maven · Gradle · Docker · Kubernetes

by OhSudo ·

Checked on real software: Eclipse Temurin 21 · November 2026How we check

Not sure this is your cause?Find it step by step: Disk full or out of memory →

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.Arrays.copyOf(Arrays.java:3541)
…

The JVM’s heap, where Java objects live, is full, and garbage collection can’t free enough. The JVM stops at its maximum heap size, whatever the machine has. So either that limit is too low for the work, or the program holds on to objects it no longer needs.

TL;DR

Terminal window
# The maximum heap this JVM would use, here and now
java -XX:+PrintFlagsFinal -version 2>/dev/null | grep -E ' MaxHeapSize | MaxRAMPercentage '
# A bigger heap
java -Xmx2g -jar app.jar
# In a container: a share of its memory limit
java -XX:MaxRAMPercentage=75.0 -jar app.jar

1. What’s the limit now?

Without -Xmx, the JVM uses a quarter of the memory it sees (MaxRAMPercentage=25). In a container, it sees the container’s limit, not the host’s. So a service in a 1 GB container gets a 256 MB heap by default, which is often too little:

Terminal window
java -XX:+PrintFlagsFinal -version 2>/dev/null | grep ' MaxHeapSize '

MaxHeapSize is in bytes. For a running process, jcmd <pid> VM.flags shows the flags it started with, and jcmd <pid> GC.heap_info shows how full it is.

2. Set the heap size

Terminal window
java -Xmx2g -jar app.jar

In containers, a percentage of the limit adapts when you change the limit:

Terminal window
java -XX:MaxRAMPercentage=75.0 -jar app.jar

Leave the rest for the JVM’s other memory: threads, Metaspace, the JIT and direct buffers. With the heap close to the whole limit, the kernel kills the JVM instead, with exit code 137 and no Java error at all.

When you can’t change the command line, such as an image’s own start script, set JAVA_TOOL_OPTIONS. The JVM reads it, and prints Picked up JAVA_TOOL_OPTIONS: … at startup to confirm:

env:
- name: JAVA_TOOL_OPTIONS
value: "-XX:MaxRAMPercentage=75.0"

The Kubernetes Manifest Generator and the Dockerfile Generator give you a place to set it, next to the memory limit.

3. Builds: Maven and Gradle

A build that fails this way needs memory for the build tool’s JVM, not for your app:

Terminal window
export MAVEN_OPTS="-Xmx2g"
mvn package

Gradle runs the build in a daemon, set in gradle.properties:

org.gradle.jvmargs=-Xmx2g

Tests run in their own JVMs. For Maven Surefire, that’s <argLine>-Xmx1g</argLine>. For Gradle, maxHeapSize = "1g" in the test task.

4. Still full? Take a heap dump

If the heap fills up whatever its size, something keeps objects alive: a cache without limits, a static list, sessions that never expire, or a query that loads a whole table. Have the JVM write a dump when it fails:

Terminal window
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp -jar app.jar

In a container, point the path at a volume, or the dump disappears with it. Open the .hprof file in Eclipse MAT or VisualVM, and look at the biggest objects and what holds them. On a running JVM, jcmd <pid> GC.class_histogram | head -n 20 shows which classes take the most space, without a full dump.

To restart cleanly instead of limping on after the error, add -XX:+ExitOnOutOfMemoryError: the JVM exits at once, with code 3, and Docker or Kubernetes restarts it.

Warning

A bigger heap fixes a heap that’s too small, not a leak. If memory use climbs steadily in your monitoring, a bigger -Xmx only makes the failure come later, and garbage collection pauses get longer on the way.

Shell, we have a problem!

Similar, but different: OutOfMemoryError: GC overhead limit exceeded is the same problem, caught earlier: the JVM spends almost all its time collecting garbage. OutOfMemoryError: Metaspace is about loaded classes, set with -XX:MaxMetaspaceSize. unable to create native thread means too many threads, or a process limit. Direct buffer memory is memory outside the heap, limited by -XX:MaxDirectMemorySize. For Node.js, see JavaScript heap out of memory.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles