How do reference counting and cyclic garbage collection work together in CPython?
PICTURE THIS: HOW TO EXPLAIN IT
Simple meaning
Each object has a refcount
WHY — Memory instead of guessing?
Why interviewers care about Memory:
question about Memory.
trade-offs, and what you would actually do on a Python project - not buzzwords.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens step by step?
Before you speak the answer, walk the interviewer through these steps:
- 1Define it
Each object has a refcount
- 2when it hits zero
the object is deallocated immediately.
- 3Cycles (A points to
B points to A) never hit zero, so the generational cyclic GC hunts them.
- 4You still close files
and DB connections explicitly
- 5GC is not a
substitute for resource management.
- 6Close
When you pick this over the alternative.
EXAMPLE — See it in action
Here's a short line you can speak, broken into clear beats:
Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.
Key takeaway
Each object has a refcount when it hits zero the object is deallocated immediately.