← Concepts

阅读中文版本

Generational Garbage Collection

Generational GC exploits the weak generational hypothesis — most objects die young — by collecting the young space often and cheaply, promoting survivors to older spaces collected rarely.

runtime

Definition

Generational collection partitions the heap by age. Young collections copy live young objects to a survivor or old space and reclaim the rest by sweeping the whole young region — cheap because little survives. Old generations accumulate long-lived objects and collect less frequently with more expensive algorithms. Write barriers track old-to-young references to keep young scans complete.

Context and scope

The default shape of JVM, .NET, V8, and Python collectors. Explains allocation cost, promotion thresholds, and why object pools can backfire under generational runtimes.

Common misconceptions

  • Generational GC makes allocation free — allocation is pointer-bump cheap; surviving allocations cost copying and promotion
  • Object pools always speed things up — pooled long-lived objects defeat generational reclamation and enlarge the old generation the expensive collector must handle
  • All objects in a batch die together — batch lifetimes straddle promotions; tuning survivor spaces decides churn between spaces

Anti-patterns

  • Ponding small short-lived objects into a global pool and converting cheap young deaths into old-generation pressure
  • Setting promotion thresholds from folklore instead of survivor statistics and thrashing copies between spaces
  • Assuming the hypothesis holds for your workload without measuring survival curves

Related concepts

Authoritative source

https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)

What it is

Heap lifetimes are heavily skewed: most allocations die almost immediately. Generational collectors bet on that skew — collect the young space frequently where reclamation is nearly total, and the old space rarely where collection is expensive.

How it works

New objects allocate in an eden space; young GC copies survivors to survivor spaces and, after enough cycles, promotes them old. An old-to-young reference would be missed by young scans, so write barriers record such references in remembered sets. Old-generation algorithms — mark-sweep, mark-compact, concurrent mark — trade fragmentation against pause length.

When to tune

Promotion rates and survivor occupancy are the dials: excessive promotion means objects outliving their welcome as young; tiny survivor spaces mean repeated copying of medium-lived objects. Profile allocation sites and shorten the lifetimes that straddle the boundary.

Pitfalls

Medium-lived objects — session-scoped or request-batched — are the pathological case: they survive young collections repeatedly, pay copying, then die before old collection. Generational assumptions fail on workloads with genuinely long-tail lifetimes, where region-based collectors like G1 or non-generational designs fit better.