JVM, memory and Java versions · mid
OutOfMemoryError, leaks and heap dumps
OutOfMemoryError has several kinds, each with a different first check. Leaks come from static collections, caches without limits and unclosed listeners. A heap dump shows who holds the memory.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
Java heap space: a leak or a heap too small.GC overhead limit exceeded: the GC spends almost all time collecting.Metaspace: too many classes.unable to create native thread: too many threads or OS limits.Direct buffer memory: NIO buffers past the limit.- A container OOM kill (exit code 137, OOMKilled) is not a Java error: the kernel kills the process. No exception and no heap dump.
-XX:+HeapDumpOnOutOfMemoryErrorworks only for heap exhaustion. Point-XX:HeapDumpPathat a volume that survives the restart. Do not catch OutOfMemoryError and carry on.- Common leaks: a map that grows (sessions never removed), a static unbounded list, listeners never removed, ThreadLocals in pooled threads, caches without limits. WeakHashMap does not fit expiry.
- Cache key without equals and hashCode: every lookup misses and adds a new entry, so the cache grows forever. Use a record for keys, a bounded cache (Caffeine
maximumSize,expireAfterWrite) and a hit-rate metric. - Metaspace leak: each script compiled into a new class in a new class loader. Classes unload only when the whole class loader is unreachable. Compile once per version and cache; sandbox business-edited scripts.
- Take a dump:
jcmd <pid> GC.heap_dump /dumps/app.hprof. The JVM pauses while writing, the file is about the size of the used heap, and it contains secrets, so protect and delete it. Lighter first:GC.class_histogramtwice. - Eclipse MAT: Leak Suspects, the dominator tree and path to GC roots. Shallow size is the object itself; retained size is everything freed if it were gone. Heap after GC climbing steadily is the leak signature. Roll back when a release is the clear trigger; more heap only delays the crash.
The red pen
Common traps: mistake → fix
Mistake: More heap fixes OutOfMemoryError
Fix: for a leak it only delays the crash.
Mistake: Every OOM leaves a heap dump
Fix: only heap exhaustion with the flag; a container OOM kill leaves nothing.
Mistake: A cache keyed by objects is safe
Fix: without equals and hashCode it grows with every lookup.
Easy to hard
Questions interviewers ask, easy to hard
- Which kinds of OutOfMemoryError do you know?
- Find every leak in this SessionRegistry class.
- How do you take and read a heap dump in production?
- After a release, pods die every six hours with heap space errors. What do you do?
- A rules engine with Groovy scripts crashes with Metaspace errors. Why?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.