Generational Garbage Collection Pipeline
Understand generational garbage collection visually. Follow how JVM-style runtimes manage memory through Eden, Survivor spaces (S0/S1), and the Old Generation
Continue with the guided workspace
Use the paired workspace to connect this guide's mental model with live component state, visible signals, and the outcome each step or scenario is meant to prove.
Mental Model
The weak generational hypothesis states that most allocated objects are short-lived. To exploit this, generational GC divides the heap into separate zones. Eden receives new allocations. When Eden fills, a Minor GC copies surviving objects to a Survivor space (S0 or S1) and increments their age. Subsequent Minor GCs copy survivors back and forth between S0 and S1. Objects surviving enough cycles are promoted to the Old Generation, which undergoes less frequent, more intensive Major GCs.
Components and Why They Exist
GC Roots
The baseline starting points for the liveness analysis, representing local variables, static fields, and active threads. Serves as the anchors of the reachability graph; any object that cannot be traced from these roots is considered garbage. In the demo, watch how references originate from the GC Roots card.
Eden Space
The primary landing strip where all new objects are allocated by mutator threads. Accumulates young allocations until capacity is reached, which triggers a Minor GC. In the demo, watch Eden accumulate objects and get completely cleared (swept) during Minor GC.
Survivor Spaces (S0 / S1)
Provide a middle ground where objects age before promotion to Old Gen, preventing short-lived objects from polluting long-term storage. One survivor space is kept completely empty to allow copying from Eden and the active survivor space during Minor GC. In the demo, watch objects copy and swap roles between S0 and S1, incrementing their tenuring age counter.
Old Generation (Tenured Space)
Stores long-lived objects that have survived multiple young collection cycles. Manages memory for persistent data and triggers a Major GC (Full GC) when capacity is reached, requiring marking, sweeping, and compaction. In the demo, watch objects promote from S0/S1 to Old Gen, and watch compaction slide survivors to one end.
Edge Cases and Misconceptions
- Misconception: Objects are promoted to Old Gen based on their size: While some runtimes promote very large objects directly to Old Gen, the primary mechanism is tenuring age (how many minor collection cycles they survive).
- Misconception: Survivor spaces swap roles continuously during application run: Roles only swap during a Minor GC cycle. Between GCs, one is active (receives survivors) and the other remains idle and empty.
- Edge Case: Premature Promotion: If the young generation is too small, survivor spaces overflow, forcing young objects to be promoted to Old Gen prematurely. This fills Old Gen quickly, triggering expensive Major GC cycles.
Common Questions
Why do we swap between Survivor 0 (S0) and Survivor 1 (S1) spaces?
Swapping survivor roles ensures that one survivor space is always 100% empty. During a minor GC, all live objects are compacted sequentially into the empty space. This avoids memory fragmentation in the young generation entirely.
What triggers a Major GC, and how does it differ from a Minor GC?
A Minor GC collects only the Young Generation (Eden and Survivor spaces) and is very fast. A Major GC (or Full GC) is triggered when the Old Generation fills up. It scans a much larger volume of objects and requires a Mark-Sweep-Compact cycle, leading to longer Stop-The-World pauses.
What is the weak generational hypothesis?
It is the empirical observation that 'most objects die young.' Generational garbage collection is designed around this rule to optimize execution performance by focusing collection efforts on young, short-lived allocations.
What is a write barrier in generational garbage collection?
When an object in the Old Gen is modified to reference a Young Gen object, the GC must track this reference so it does not collect the Young Gen object during a Minor GC. A write barrier intercepts reference updates and records them in a card table or remembered set, avoiding scanning the entire Old Gen.