Generational Garbage Collection
Most modern engines organize the heap by object age, since most objects die young — collecting the young generation frequently and cheaply, the old generation less often.
Purpose
Generational garbage collection is based on an empirical observation — most objects become unreachable shortly after allocation — and organizes collection around it: a young generation collected frequently and cheaply, and an old generation (containing objects that survived several young-generation collections) collected less often, at higher cost per collection.
The model
| Generation | Contains | Collected |
|---|---|---|
| Young (nursery) | Recently allocated objects | Frequently, cheaply |
| Old | Objects that survived several young-generation collections | Less often, more expensively |
An object surviving enough young-generation collections gets promoted to the old generation, under the assumption that something long-lived is likely to stay long-lived.
Behavior notes
- A genuinely short-lived object that happens to survive just long enough to be promoted adds real cost — it now occupies old-generation space and requires the more expensive old-generation collection process to eventually reclaim, despite its actual brief usefulness.
- This connects directly to environment retention — a closure or reference that keeps otherwise-short-lived data reachable slightly too long can push it into the more expensive generation unnecessarily.
- Exact generation counts, promotion thresholds, and collection algorithms are entirely engine-specific implementation details, not specified by ECMAScript.
Common mistakes
- Assuming all garbage collection has the same cost profile — young-generation collection is typically fast and frequent; old-generation (or full-heap) collection can cause a much larger, more noticeable pause.
- Holding a reference to data slightly longer than necessary “just in case,” inadvertently promoting it into the more expensive generation.
Runtime notes
An engine-implementation strategy, not an ECMAScript guarantee — the general generational principle is common across major engines, but specifics vary and are subject to change between versions.