Skip to content

Error explained

java.lang.OutOfMemoryError: causes and fixes

The message after OutOfMemoryError tells you which memory area ran out. Each one has a different cause and fix.

3 min read8 sectionsUpdated

On this page

Key points

  • Read the suffix: heap space, GC overhead, Metaspace, native thread or direct memory.
  • Take a heap dump on OOM so you can see what filled the heap.
  • Raising -Xmx helps only if the live set legitimately grew.
  • In containers, leave room outside the heap.

The types

MessageMeaningTypical fix
Java heap spaceHeap full of live objectsFind the leak or large allocation; raise heap if the live set is genuinely bigger
GC overhead limit exceededGC is using almost all CPU and freeing littleSame as heap space: too much live data
MetaspaceToo many classes loadedClassloader leaks after redeploys; raise MaxMetaspaceSize if legitimate
unable to create native threadOS refused another threadThread leak or limits (ulimit -u, container pids limit)
Direct buffer memoryOff-heap NIO buffers exhaustedNetty/NIO buffers not released; tune MaxDirectMemorySize
Requested array size exceeds VM limitOne huge arrayStream or chunk the data

Capture evidence automatically

bash
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dumps \
     -XX:+ExitOnOutOfMemoryError \
     -XX:MaxRAMPercentage=75 -jar app.jar

ExitOnOutOfMemoryError makes the JVM exit instead of limping on in a broken state, so the orchestrator restarts it. Analyse the dump with Eclipse MAT: the dominator tree shows which objects retain the memory. See Java memory usage for all the areas a JVM uses.

Common root causes

  • Loading an entire table or file into memory instead of streaming.
  • Unbounded caches and static maps.
  • Sessions or request data kept after the request ends.
  • Thread pools created per request (native thread OOM).
  • A heap sized for a laptop, deployed to a busier production.

Catching it in production

An OOM crash shows as restarts and errors; the heap-after-GC trend on dashboards usually climbed for hours before. Alert on heap after GC above 85% and on container memory near its limit.

VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.

Frequently asked questions

Should I just increase -Xmx?

Only after confirming the live data set legitimately needs it. If memory grows without bound, a bigger heap only delays the crash.

What is the difference between OOMKilled and OutOfMemoryError?

OutOfMemoryError is thrown by the JVM when one of its own areas is full. OOMKilled is the kernel killing the whole process for exceeding the container memory limit.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.