Video in progress
What does it mean when you see "OutOfMemoryError: Java heap space" in the log?
The JVM couldn't find room on the heap for a new object, and the GC couldn't free enough space either. The cause is either a heap that is too small, a memory leak, or too much data being loaded at once.
📖 Answer
The error appears exactly when the -Xmx limit is reached. Three usual causes:
- The heap is small: the application really needs more memory (for example, the load grew).
- A leak: objects stay in some collection even though they aren't needed, and the heap slowly fills.
- One big request: loading a million rows with
findAll(), doingreadAllBytes()on a 500 MB file.
The first steps:
- look at the GC log or the monitoring graph: is the heap a "sawtooth", but with the low point rising over time? That is a sign of a leak;
- find out from the log which request was running when the error appeared;
- take a heap dump (
-XX:+HeapDumpOnOutOfMemoryError) and look at the biggest objects in Eclipse MAT.
Rushing to raise -Xmx only helps in case 1. If there is a leak, it just pushes the error further out.
Sign up to mark studied questions and save your progress.