QA
Senior LevelPython

How do you tune Generational Garbage Collection thresholds (gc.set_threshold(700, 10, 10)) to eliminate GC pause latency spikes in high-throughput API servers?

❓ Interview Prompt: Explain Generational Garbage Collection (Gen 0, Gen 1, Gen 2), cyclic garbage collection, `gc.set_threshold()`, and eliminating GC pause latency spikes.

CPython uses Generational Garbage Collection to collect cyclic object references:

Tuning High-Throughput API Servers:

Under heavy traffic allocating millions of short-lived JSON objects per second, default Gen 0 threshold (700 allocations) triggers constant GC sweeps, causing severe CPU latency spikes!

•
Tuning: Increase thresholds via 'gc.set_threshold(50000, 50, 50)' or call 'gc.disable()' during request handling, executing 'gc.collect(0)' manually in background worker idle loops.

Code Implementation Example

python
import gc

# Check default generational garbage collection thresholds
print('Default Thresholds:', gc.get_threshold()) # (700, 10, 10)

# Tune thresholds for high-throughput API server to reduce GC pause latency
gc.set_threshold(50000, 50, 50)
print('Tuned Thresholds:', gc.get_threshold())
💡Tuning CPython generational garbage collection thresholds for high API throughput.
🎯 Interview Key Takeaway

Tune gc.set_threshold to reduce Generation 0 garbage collection sweeps in high-throughput API servers.

#Garbage Collection#gc module#Performance Tuning#Senior
Back to all Python Questions→

More Python Interview Questions

Beginner LevelPython

What is the Python Global Interpreter Lock (GIL) and how does it affect multi-threading?

Beginner LevelPython

What is the difference between a List, Tuple, Set, and Dictionary in Python?

Beginner LevelPython

Why are mutable default arguments (like def func(items=[])) dangerous in Python and how do you fix them?