LogLens AI / Errors / java.lang.OutOfMemoryError

java.lang.OutOfMemoryError: causes and how to fix it

TL;DR. java.lang.OutOfMemoryError means the JVM ran out of memory for an allocation. The sub-message matters: Java heap space (heap too small or a leak), GC overhead limit exceeded (GC thrashing), or Metaspace (too many classes).

What causes java.lang.OutOfMemoryError

Heap (-Xmx) too small for the workload; a memory leak holding references (caches, static collections, unclosed resources); loading huge result sets into memory; or Metaspace growth from classloader leaks.

How to diagnose it

Read the full message and stack. Enable a heap dump on OOM: -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dump, then analyse with Eclipse MAT or VisualVM. Watch GC logs for rising post-GC heap (a leak).

How to fix java.lang.OutOfMemoryError

  1. Read the OOM sub-type. "Java heap space" vs "GC overhead" vs "Metaspace" each point at a different fix — don't just raise -Xmx blindly.
  2. Raise -Xmx or fix the leak. If the workload is legitimately large, increase -Xmx (and the container memory limit). If post-GC heap keeps rising, take a heap dump and fix the leak.
  3. In containers, set heap below the limit. Use -XX:MaxRAMPercentage or an -Xmx safely under the container memory limit, or you'll get OOMKilled (exit 137) instead.

Example log line

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3210)

How LogLens detects java.lang.OutOfMemoryError

LogLens flags OutOfMemoryError stack traces and the restart burst that follows, grouped as one incident. LogLens AI is a free, self-hosted AI log analyzer that surfaces lines like this automatically, groups them into one incident, and explains them in plain language.

Install LogLensSee the tour

Related errors

FAQ

What is the difference between OutOfMemoryError and OOMKilled?

OutOfMemoryError is the JVM failing to allocate within its own heap. OOMKilled (exit 137) is the OS/kernel killing the whole process for exceeding the container memory limit. In containers, set the JVM heap below the limit to avoid the latter.

Does increasing -Xmx fix OutOfMemoryError?

Only if the heap was genuinely too small. If there is a leak, a bigger heap just delays the error — take a heap dump and fix the retained references.