Hard👤 8-15 years 1 min read

A long-running service's heap keeps growing until OutOfMemoryError — how do you diagnose it?

Asked inAmazonMicrosoftGoogleDeloitte
#memory leak#heap dump#oom#gc#profiling#production
Report issue

⚡ 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

1. ConfirmGC logs — old gen grows post-Full-GC
2. CaptureHeapDumpOnOutOfMemoryError / jmap
3. AnalyzeMAT dominator tree → GC root
Usual suspectunbounded cache / static map / ThreadLocal

⌨️Hands-on Keyboard

Learn by Doing

bash
# 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:

GC logs firstheap dump + MAT dominator treeGC roots / retained sizeunbounded cache / static / ThreadLocalbounded cache fix

⚠️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

Eclipse MATjmap/jcmdCaffeine cacheG1/ZGCPrometheus JVM metrics

📚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.
Open inChatGPTGeminiClaude

Was this answer helpful?

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.

Related Questions