Python GC: How Often Each Generation Collects

Generation 0 triggers a collection after approximately 700 net object allocations, Generation 1 after around 7,000 allocations (10 Gen 0 collections), and Generation 2 after roughly 70,000 allocations (10 Gen 1 collections), showing a significantly decreasing collection frequency for older objects.

The top 3

  1. Generation 0: Most Frequent Collections: Python's youngest generation (Generation 0) collects most frequently, typically triggered when the number of newly allocated objects minus deallocated objects exceeds a default threshold (e.g., 700 or 2000 depending on Python version), efficiently reclaiming short-lived memory.
  2. Generation 2: Least Frequent Collections: Generation 2, containing the oldest objects, experiences the least frequent collections, typically running only after Generation 1 has been collected ten times (which itself is after ten Generation 0 collections), to minimize overhead for long-lived objects.
  3. Generation 1: Intermediate Collection Frequency: Objects that survive Generation 0 collections are promoted to Generation 1. This generation is collected less frequently than Gen 0 but more often than Gen 2, typically triggered after Gen 0 has been collected 10 times, based on Python's default thresholds.

Sources

Open the full topic