categories.language Intermediate
What Are the Core Concepts of Memory Management and Garbage Collection?
Memory Management and Garbage Collection
Memory Regions
- Stack: Function call frames, local variables; automatically allocated/freed; fast
- Heap: Dynamic allocation (objects, arrays); managed by GC
GC Algorithms
Mark and Sweep
- Start from root objects, mark all reachable objects
- Sweep (collect) unmarked objects ❌ Can cause memory fragmentation
Reference Counting
Track how many references point to an object; collect when count reaches zero ✅ Immediate reclamation ❌ Cannot handle circular references (Python has a cycle detector)
Generational GC
Partition objects by age (Young / Old / Permanent)
- Most objects die young; focus on scanning Young Generation
- Used by JVM (Java) and V8 (Node.js)
Stop-the-World (STW)
GC pauses all application threads—the main source of latency spikes
- Modern GCs (ZGC, G1 GC) minimize STW pause times
Language Comparison
| Language | GC Mechanism |
|---|---|
| Java | Generational GC (G1/ZGC) |
| Go | Concurrent Mark-Sweep |
| Python | Reference Counting + Cycle Detector |
| Rust | No GC; Ownership system at compile time |
Interview bonus: Common memory leak causes—unremoved event listeners, unbounded global caches, circular references.
✦ AI Mock Interview
Type your answer and get instant AI feedback
Sign in to use AI scoring
