Python GC's Default Thresholds Explained
Python's generational garbage collector uses default thresholds of (700, 10, 10). The first number (700) dictates that a Generation 0 collection triggers when allocations minus deallocations exceed this value, while the subsequent 10s determine how many times younger generations must be collected before older ones are swept.
The top 3
- Why Python's GC Defaults to (700, 10, 10): These default thresholds balance frequent collection of short-lived objects in Generation 0 with less frequent, more expensive scans for older, potentially long-lived objects in Generations 1 and 2, based on the generational hypothesis.
- 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.
- When to Override Python's Default GC Thresholds: For specific high-performance needs, applications like Instagram have entirely disabled Python's generational GC for a reported 10% speed improvement, while others have seen 20% faster responses by increasing the Generation 0 threshold from 700 to 50,000, demonstrating significant tuning potential.
Sources
Open the full topic