Skip to main content

CPython memory management, refcounting, GC and __slots__

How does CPython reclaim memory, why does cyclic GC exist alongside refcounting, and what does __slots__ trade off?

Official Reference

Model Answer & Explanation

CPython immediately frees objects when their reference count hits zero, and uses a generational cyclic garbage collector to detect reference cycles (e.g. parent <-> child graphs). Defining __slots__ replaces the per-instance __dict__ with fixed descriptor slots, cutting memory and speeding attribute access at the cost of disabling arbitrary dynamic attributes.

Follow-up Interview Question · How can an exception traceback or global cache silently keep large objects alive?

Module Context & References

Python & FastAPI · Popular interview theory The 20% of Python & FastAPI concepts asked in 80% of backend interviews
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.