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 (young, old, metaspace)?
- 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
28 questions for Middle/Senior Java Developer interview preparation.
All Questions
Topic Dependency Map
┌──────────────────────────────────────────┐
│ JVM MEMORY (1-3) │
│ 1. Heap vs Stack │
│ 2. What's in Heap │
│ 3. What's in Stack │
└──────────────────┬───────────────────────┘
│
┌──────────────────────────┼──────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ GC BASICS │ │ GENERATIONS │ │ LEAKS │
│ (4-5) │ │ (8-11) │ │ (6-7) │
│ 4. GC overview │ │ 8. Generations │ │ 6. Memory Leak │
│ 5. Candidates │ │ 9. Young Gen │ │ 7. How it occurs │
│ for removal │ │ 10. Old Gen │ │ │
│ │ │ 11. Metaspace │ │ │
└───────┬───────┘ └───────┬───────┘ └────────┬───────────┘
│ │ │
└────────────────────────┼────────────────────────┘
▼
┌──────────────────────────────────────────┐
│ GC ALGORITHMS (12-17) │
│ 12. Algorithms overview │
│ 13. G1 GC │
│ 14. ZGC │
│ 15. Shenandoah GC │
│ 16. Stop-the-World │
│ 17. Low-latency GC │
└──────────────────────────────────────────┘
│
┌────────────────────────┼────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌────────────────────┐
│ TUNING & OOM │ │ DIAGNOSTICS │ │ REACHABILITY │
│ (18-20) │ │ (21-24) │ │ (25-28) │
│ 18. -Xms/-Xmx │ │ 21. Leak detect│ │ 25. GC Roots │
│ 19. OOM what │ │ 22. Tools │ │ 26. Reachability │
│ 20. OOM types │ │ 23. Heap dump │ │ 27. Invoke GC │
│ │ │ 24. Get dump │ │ 28. System.gc() │
└───────────────┘ └───────────────┘ └────────────────────┘
Recommended Study Order
Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Heap vs Stack | Q1, Q2, Q3 | Where objects, primitives, references are stored |
| 2 | GC basics | Q4, Q5 | What GC is, when object is eligible for removal |
| 3 | Generations | Q8, Q9, Q10, Q11 | Young, Old, Metaspace — why and what’s there |
| 4 | -Xms/-Xmx | Q18 | Heap configuration |
| 5 | System.gc() | Q27, Q28 | Why not to call manually |
Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | GC Algorithms | Q12 | Mark-Sweep, Mark-Compact, Copying — differences |
| 2 | G1 GC | Q13 | Regions, RSet, CSet, Mixed GC, String Deduplication |
| 3 | Memory Leaks | Q6, Q7 | ThreadLocal, static collections, inner classes |
| 4 | OOM | Q19, Q20 | What happens, OOME types |
| 5 | STW | Q16, Q17 | Why, which GCs minimize, Allocation Stall |
| 6 | Tools | Q22, Q23, Q24 | jmap, jstat, MAT, heap dump |
Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | ZGC | Q14 | Colored Pointers, Load Barriers, Multi-mapping, Generational |
| 2 | Shenandoah | Q15 | Load Reference Barriers, Brooks Pointers, vs ZGC |
| 3 | Reachability | Q25, Q26 | GC Roots, Strong/Soft/Weak/Phantom, Cleaner, OopMaps |
| 4 | Leak detection | Q21 | Delta analysis, sawtooth pattern, dominator tree |
| 5 | GC tuning | Q18 | -Xms=-Xmx, Compressed OOPs, NUMA, G1 tuning |
| 6 | Escape Analysis | Q1 (Senior) | Scalar Replacement, Lock Elision, TLAB |
Key Connections Between Topics
Topic: JVM Memory
Q1 (Heap vs Stack) → Q2 (What's in Heap) → Q3 (What's in Stack)
↓
Q4 (GC overview) → Q5 (Candidates for removal)
Key connections:
- Q1 <-> Q5: Reachability from GC Roots determines eligibility for removal
- Q2 <-> Q11: Metaspace — memory area outside of Heap
- Q3 <-> Q25: Stack contains GC Roots (local variables)
Topic: GC Generations
Q8 (Generations) → Q9 (Young Gen) → Q10 (Old Gen) → Q11 (Metaspace)
Key connections:
- Q9 <-> Q10: Objects promoted from Young to Old after surviving several Minor GCs
- Q8 <-> Q12: Generational hypothesis → different algorithms for Young and Old
- Q11 <-> Q2: Metaspace is not in Heap, but OOME is still possible
Topic: GC Algorithms
Q12 (Algorithms) → Q13 (G1) → Q14 (ZGC) → Q15 (Shenandoah)
↓ ↓
Q16 (STW) ←──── Q17 (Low-latency GC)
Key connections:
- Q13 <-> Q14: G1 → predictable pauses, ZGC → < 1 ms pauses
- Q14 <-> Q15: ZGC (Colored Pointers) vs Shenandoah (Brooks Pointers) — different approaches
- Q16 <-> Q17: STW — what freezes, which GCs minimize
Topic: Diagnostics and Reachability
Q25 (GC Roots) → Q26 (Reachability) → Q27 (Invoke GC) → Q28 (System.gc())
↓
Q21 (Leak detect) → Q22 (Tools) → Q23 (Heap dump) → Q24 (Get dump)
Key connections:
- Q25 <-> Q26: GC Roots — starting point of reachability analysis
- Q21 <-> Q23: Heap dump — main tool for leak diagnostics
- Q27 <-> Q28: You can call it, but you SHOULDN’T — why
Cheat Sheet: What to Know for Each Level
Junior
- Heap = objects, Stack = local variables + method calls
- GC automatically frees memory of unreachable objects
- Generations: Young (new), Old (long-lived), Metaspace (class metadata)
- -Xms = initial Heap, -Xmx = maximum Heap
- System.gc() — a suggestion, JVM may ignore it
Middle
- GC Roots: stack variables, static fields, JNI, active threads
- G1: regions, RSet, CSet, Mixed GC, MaxGCPauseMillis
- Young GC = Minor GC (Eden + Survivor → Survivor/Old)
- Full GC = entire Heap, long pause (seconds)
- ThreadLocal — common cause of leaks (not calling .remove())
- Heap Dump = snapshot of Heap in binary format (hprof), STW during capture
Senior
- ZGC: Colored Pointers (metadata in pointer bits), Load Barriers, Multi-mapping
- Shenandoah: Brooks Pointers (forwarding pointer), Load Reference Barriers
- SATB (Snapshot-At-The-Beginning) — concurrent marking algorithm
- Escape Analysis → Scalar Replacement → Stack allocation instead of Heap
- TLAB → lock-free per-thread allocation
- OopMaps → JIT reference map, GC doesn’t scan every byte of the stack
- Allocation Stall — young gen fills faster than concurrent marking finishes
File Format
Each file contains:
- Junior Level — basic understanding, simple analogies, examples
- Middle Level — internals, common mistakes, practical examples
- Senior Level — deep dive, edge cases, production experience, monitoring
- Interview Cheat Sheet — key points, common questions, red flags, related topics