Q40: What causes Memory Leaks in Java and how do you diagnose them?
Memory leaks occur when unreferenced objects remain reachable through accidental reference paths, preventing GC reclamation.
- Causes: Un-removed static collection references, unclosed streams/connections, non-static inner class scope leaks, missing
equals()/hashCode()in keys. - Diagnosis: Analyze heap dumps (
.hprof) using Eclipse Memory Analyzer (MAT) or JProfiler, inspecting Dominator Trees and GCRoots paths.
Key Takeaways
- Unbounded static collections are a major cause of memory leaks.
- Diagnose using heap dumps (
jcmd,jmap) and MAT Dominator Trees.
Knowledge Check
Why do static collection instances frequently cause Java Memory Leaks if unmonitored?