Generational Collection Basics
What you'll learn: How modern JavaScript engines optimize garbage collection by treating new and old objects differently.
The Two-Generation Model
Most JavaScript engines (like V8 in Chrome/Node) don't treat all objects equally during garbage collection. Instead, they divide the heap into generations based on a simple observation: most objects die young.
Think of it like managing a library. New books get returned or discarded quickly, while reference books stay on the shelves for years. It makes sense to check the "new arrivals" section frequently and the "classics" section rarely.
Young Generation (Nursery)
Newly created objects start here. This area is small and collected very frequently (sometimes every few milliseconds). The GC assumes most of these objects will become unreachable quickly—temporary variables, intermediate results, short-lived closures.
Because this space is small, collection is fast. Most objects never leave this generation.
Old Generation (Tenured Space)
Objects that survive multiple young-generation collections get promoted to the old generation. These are the "classics"—long-lived data structures, global objects, persistent caches.
The old generation is collected much less frequently because:
- It's larger, so full collection is expensive
- Objects here are statistically more likely to remain reachable
Why This Matters
This strategy dramatically improves performance. Instead of scanning all memory constantly, the engine focuses on the small young generation where most garbage actually lives. The cost is distributed: frequent tiny pauses for young GC, occasional longer pauses for old GC.
You don't control generational collection directly, but understanding it helps explain why short-lived objects are cheap and why memory profilers show "generations" in their reports.
Key Takeaway: Modern JavaScript engines optimize GC by dividing objects into young (frequently collected) and old (rarely collected) generations, based on the observation that most objects die young.