Home โบ Java exceptions โบ OutOfMemoryError
OutOfMemoryError: causes and fixes
The JVM could not allocate memory: heap space, metaspace, direct buffers, native threads or GC overhead.
What it looks like
java.lang.OutOfMemoryError: Java heap spaceExtends VirtualMachineError in java.lang.
What it means
The text after the colon tells you which area ran out. Java heap space means live objects fill the heap. GC overhead limit exceeded means the collector is spending almost all its time and reclaiming almost nothing. Metaspace means too many classes were loaded. Unable to create native thread means the OS refused another thread. Direct buffer memory means off-heap buffers filled their limit. Each has a different fix, so read the message before changing flags.
Common causes
- A memory leak: a static map or cache that only grows, listeners never removed, ThreadLocals in thread pools.
- Loading too much at once: a query without paging, reading a whole large file or response into memory.
- A heap limit that is simply too small for the workload or container.
- Class loader leaks after repeated redeploys (Metaspace), or an unbounded number of threads.
How to fix it
- Take a heap dump on failure (
-XX:+HeapDumpOnOutOfMemoryError) and open it in Eclipse MAT or VisualVM to see what holds the memory. - Stream or page large data instead of loading it all; bound caches with a maximum size and expiry.
- Size the heap against the container limit; see the JVM memory calculator.
- Add
-XX:+ExitOnOutOfMemoryErrorso an orchestrator restarts the process instead of leaving it half dead.
Broken and fixed
List<Row> all = repo.findAll(); // millions of rows in one listtry (Stream<Row> rows = repo.streamAll()) {
rows.forEach(this::process);
}๐ Unravel the failing code ๐ Unravel the fix
In Spring and enterprise code
In Kubernetes a pod killed with exit code 137 is not an OutOfMemoryError: the container limit was exceeded by heap plus non-heap memory and the kernel killed the process. Check both.
Debug it faster
Paste the whole stack trace into the stack trace beautifier to hide framework frames, jump to the root cause and find the first line of your own code. It runs in your browser, so the trace never leaves your machine.
Frequently asked questions
Is OutOfMemoryError a checked or unchecked exception?
OutOfMemoryError is an Error (it extends VirtualMachineError). Errors signal serious problems in the JVM or environment; applications normally should not catch them, and ordinary catch (Exception e) blocks will not.
Which package is OutOfMemoryError in?
It lives in java.lang, so the import is import java.lang.OutOfMemoryError; (java.lang needs no import).
What is the first thing to check when I see OutOfMemoryError?
Read the message and the first stack trace line in your own code. The usual causes are: A memory leak: a static map or cache that only grows, listeners never removed, ThreadLocals in thread pools; and Loading too much at once: a query without paging, reading a whole large file or response into memory.