CPython Memory Internals, Reference Counts & Garbage Collection
Understanding PyObject headers, arena allocators, cyclic reference detection, and weakref mechanics.
CPython Stack & Heap Pointer Explorer
Explore how CPython manages memory pointers, cached singletons, and reference counting.
CPython pre-allocates an internal cache for integers between [-5, 256].
Every variable in Python is a pointer referencing an underlying PyObject allocated on the heap. In this lesson, we explore how CPython tracks reference counts and when the cyclic garbage collector triggers generation-based sweeps.
import sys
import gc
a = [1, 2, 3]
print("Initial Refcount:", sys.getrefcount(a) - 1) # getrefcount adds 1 temporary ref
b = a
print("After assignment:", sys.getrefcount(a) - 1) # 2
del b
print("After del:", sys.getrefcount(a) - 1) # 1
Cyclic Garbage Collection
Reference counting cannot reclaim objects referencing each other in a closed loop. CPython’s generational GC tracks container objects across three generations (Gen 0, 1, and 2), identifying unreachable reference islands.
Python Systems: Interactive Lab
Python SystemsMatched to lessonInspects heap addresses, sys.getrefcount(), and integer caching.
Click Run Code to execute this algorithm in the browser sandbox.
Finished this lesson?
Mark it as complete to record your progress and unlock the next module.
