A long-running service's heap keeps growing until OutOfMemoryError — how do you diagnose it?
⚡ Short Answer
Confirm with GC logs (old gen not reclaimed after Full GC), capture a heap dump (jmap / -XX:+HeapDumpOnOutOfMemoryError), analyze dominator tree in Eclipse MAT to find the GC root holding the growing object set — usually an unbounded cache, static collection, or ThreadLocal.
☕Coffee Chat Question
Concept Made Simple
“A long-running service's heap keeps growing until OutOfMemoryError — how do you diagnose it?”
🧠Mind Map Answer
Remember It Faster
⌨️Hands-on Keyboard
Learn by Doing
# auto-capture on OOM
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dumps
# or on demand
jmap -dump:live,format=b,file=heap.hprof <pid>
# then open heap.hprof in Eclipse MAT → 'Leak Suspects'🔥What If?
Think Beyond the Expected
GC runs constantly but frees little and CPU is pegged — what's happening?
That's GC thrashing: the live set nearly fills the heap, so Full GCs run back-to-back reclaiming little (the 'GC overhead limit exceeded' precursor). Either the heap is too small or there's a leak retaining objects.
😂Real World
The #1 enterprise Java leak is an unbounded HashMap cache (no eviction/TTL) or a static List that only ever grows. MAT's dominator tree points straight at the retaining GC root.
🎯Interviewer's Expectation
Keywords they're listening for:
⚠️Common Mistakes
- ✗Just bumping -Xmx instead of finding the root cause
- ✗Analyzing without a heap dump (guessing)
- ✗Caches with no max size or eviction policy
✅Best Practices
- ✓Always enable HeapDumpOnOutOfMemoryError in prod
- ✓Bound every cache (size + TTL) — Caffeine/Guava
- ✓Watch old-gen occupancy as an alert signal
🔁Follow-up Questions
- 1How do you tell a leak from just under-sized heap?
- 2What's retained vs shallow size in MAT?
- 3How does a ThreadLocal leak in a thread pool?
🧩Related Technologies
📚References
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Memory/GC (Core Java) Interview question: "A long-running service's heap keeps growing until OutOfMemoryError — how do you diagnose it?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
⭐ Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.