Memory and Garbage Collection
28 interview questions and answers in the Memory and Garbage Collection section.
Questions in this section
- What is the difference between Heap and Stack
- What is stored in Heap
- What is stored in Stack
- What is Garbage Collection
- When does an object become eligible for GC
- What is a memory leak in Java
- How can a memory leak occur in Java
- What are generations in GC
- What is Young Generation
- What is Old Generation (Tenured)
- What is Metaspace (or PermGen)
- What GC algorithms exist
- What is G1 GC
- What is ZGC
- What is Shenandoah GC
- What is stop-the-world
- Which GCs minimize stop-the-world pauses
- What are -Xms and -Xmx parameters
- What happens on OutOfMemoryError
- What types of OutOfMemoryError exist
- What is a memory leak and how to detect it
- What tools help analyze memory
- What is a heap dump
- How to get a heap dump
- What are GC roots
- What is reachability in the context of GC
- Can you manually invoke GC
- Why you should not call System.gc()
Study navigator
A comprehensive collection of 28 interview questions covering JVM Memory Layout, Garbage Collection algorithms, low-latency collectors (G1, ZGC, Shenandoah), and memory diagnostics for Junior 🟢, Middle 🟡, and Senior 🔴 Java Developers.
📋 All Questions
🗺️ Topic Dependency Map
┌──────────────────────────────────────────┐
│ JVM MEMORY ARCHITECTURE (1-3) │
│ 1. Heap vs Stack │
│ 2. Stored in Heap │
│ 3. Stored in Stack │
└──────────────────┬───────────────────────┘
│
┌─────────────────────────┼─────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ GC BASICS │ │ GENERATIONS │ │ LEAK MECHANICS │
│ (4-5) │ │ (8-11) │ │ (6-7) │
│ 4. GC Overview│ │ 8. Generations│ │ 6. Memory Leak Def │
│ 5. Eligibility│ │ 9. Young Gen │ │ 7. Root Causes │
│ │ │ 10. Old Gen │ │ │
│ │ │ 11. Metaspace │ │ │
└───────┬───────┘ └───────┬───────┘ └────────┬───────────┘
│ │ │
└─────────────────────────┼──────────────────────────┘
▼
┌──────────────────────────────────────────┐
│ GC ALGORITHMS & COLLECTORS (12-17) │
│ 12. Algorithms Overview │
│ 13. G1 GC │
│ 14. ZGC (Generational ZGC) │
│ 15. Shenandoah GC │
│ 16. Stop-the-World Safepoints │
│ 17. Low-Latency Collectors │
└──────────────────┬───────────────────────┘
│
┌─────────────────────────┼─────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ TUNING & OOM │ │ DIAGNOSTICS │ │ REACHABILITY & JMM │
│ (18-20) │ │ (21-24) │ │ (25-28) │
│ 18. -Xms/-Xmx │ │ 21. Leak Detect│ │ 25. GC Roots │
│ 19. OOM Action│ │ 22. Tooling │ │ 26. Reference Types│
│ 20. OOM Types │ │ 23. Heap Dump │ │ 27. Manual Invocation│
│ │ │ 24. Capture │ │ 28. System.gc() Risk│
└───────────────┘ └───────────────┘ └────────────────────┘
🎯 Recommended Study Progression
🟢 Junior Track
| Step | Topic | Questions | Core Learning Objectives |
|—|—|—|—|
| 1 | Heap vs Stack | Q1, Q2, Q3 | Allocation models, primitive vs reference storage, lifecycle |
| 2 | GC Fundamentals | Q4, Q5 | Automatic garbage collection, GC eligibility |
| 3 | Memory Generations | Q8, Q9, Q10, Q11 | Young (Eden/Survivor), Old, Metaspace vs PermGen |
| 4 | Memory Sizing | Q18 | Heap configuration flags (-Xms, -Xmx) |
| 5 | GC Invocations | Q27, Q28 | System.gc() non-determinism and explicit collection traps |
🟡 Middle Track
| Step | Topic | Questions | Core Learning Objectives |
|—|—|—|—|
| 1 | GC Algorithms | Q12 | Mark-Sweep, Mark-Compact, Copying trade-offs |
| 2 | G1 Collector | Q13 | Region partitioning, Remembered Sets (R-Sets), Collection Sets (C-Sets), Mixed GC |
| 3 | Memory Leaks | Q6, Q7 | Static references, ThreadLocal leaks in thread pools, unclosed resources |
| 4 | OutOfMemoryErrors | Q19, Q20 | OOM failure modes (Java heap space, Metaspace, GC overhead limit) |
| 5 | STW Pauses | Q16, Q17 | Safepoint synchronization, allocation stalls, latency collectors |
| 6 | Memory Tooling | Q22, Q23, Q24 | jcmd, jstat, Eclipse MAT, HPROF dumps, -XX:+HeapDumpOnOutOfMemoryError |
🔴 Senior Track
| Step | Topic | Questions | Core Learning Objectives |
|—|—|—|—|
| 1 | ZGC Deep-Dive | Q14 | Colored Pointers, Load Barriers, Self-Healing references, Generational ZGC |
| 2 | Shenandoah Deep-Dive | Q15 | Concurrent compaction, Brooks Pointers vs Load-Reference Barriers |
| 3 | Reachability & References | Q25, Q26 | GC Roots categories, Reference hierarchy (Strong, Soft, Weak, Phantom), Cleaner |
| 4 | Memory Diagnostics | Q21 | Heap baseline growth, Dominator Tree analysis, Retained vs Shallow heap |
| 5 | Production Tuning | Q18 | Sizing -Xms = -Xmx, Container-aware JVM (-XX:MaxRAMPercentage), NUMA |
| 6 | JIT Escape Analysis | Q1 | Scalar Replacement, Lock Elision, TLAB allocation optimization |
🔗 Key Topic Relationships
JVM Memory Cluster
Q1 (Heap vs Stack) ──> Q2 (Stored in Heap) ──> Q3 (Stored in Stack)
│
▼
Q4 (GC Overview) ──> Q5 (Eligibility) ──> Q25 (GC Roots) ──> Q26 (Reachability)
- Q1 ↔ Q25: Thread stacks contain local variable frames which serve as root pointers (GC Roots).
- Q2 ↔ Q11: Heap contains object instances; Metaspace resides in native memory holding loaded class metadata.
Memory Generations Cluster
Q8 (Generations) ──> Q9 (Young Gen) ──> Q10 (Old Gen) ──> Q11 (Metaspace)
- Q8 ↔ Q12: The Weak Generational Hypothesis dictates using Copying algorithms for Young Gen and Compact algorithms for Old Gen.
- Q9 ↔ Q10: Objects surviving
-XX:MaxTenuringThresholdMinor GC cycles are promoted from Survivor to Tenured space.
Garbage Collector Cluster
Q12 (Algorithms) ──> Q13 (G1) ──> Q14 (ZGC) ──> Q15 (Shenandoah)
│
▼
Q16 (Stop-the-World) ──> Q17 (Low-Latency Collectors)
- Q13 ↔ Q14: G1 provides predictable target pauses (~200ms); ZGC achieves sub-millisecond pauses (<1ms) via colored pointers.
- Q14 ↔ Q15: ZGC uses metadata bits inside pointers + load barriers; Shenandoah uses load-reference barriers.
🎓 Level-by-Level Knowledge Matrix
🟢 Junior
- Heap stores all class instances and arrays (shared across threads); Stack stores method frames and local variables (thread-isolated).
- The Garbage Collector automatically reclaims memory occupied by unreachable objects.
- Generations: Young (Eden, S0, S1), Old (Tenured), Metaspace (Class metadata in native RAM).
-Xmssets initial heap size;-Xmxsets maximum heap ceiling.
🟡 Middle
- GC Roots include active thread stack variables, static class fields, JNI handles, and JVM internal references.
- G1 GC divides heap into equal-sized Regions (1MB–32MB) and uses SATB (Snapshot-At-The-Beginning) concurrent marking.
- ThreadLocal Leaks: Occur when worker threads in thread pools reuse threads without calling
ThreadLocal.remove(). - Heap Dumps (
.hprof): Triggered via-XX:+HeapDumpOnOutOfMemoryErrororjcmd <pid> GC.heap_dump.
🔴 Senior
- ZGC Colored Pointers: Encodes metadata (Marked0, Marked1, Remapped) directly into the upper bits of 64-bit object references; load barriers intercept references and self-heal them during relocation.
- Escape Analysis (JIT): If an object does not escape the allocating method, HotSpot performs Scalar Replacement, breaking the object down into primitive registers/stack variables to bypass Heap allocation completely.
- TLAB (Thread Local Allocation Buffer): Pre-allocated Eden buffers per thread allowing fast, lock-free bump-the-pointer heap allocations.
- Eclipse MAT Analysis: Dominator Tree calculation distinguishes between Shallow Heap (memory consumed by the object itself) and Retained Heap (memory freed if the object is garbage collected).