Variables and Memory · Lecture 02 · 9 min
Reference Counting
CPython tracks how many references point to each object and frees the object the moment that count reaches zero.
How does Python know when to free memory?
You never call free() in Python. Instead, CPython’s memory manager keeps a reference count in every object: the number of references currently pointing at it.
my_var = [1, 2, 3]creates a list with a reference count of 1.other_var = my_varadds a second reference, so the count is 2.- When
other_varis re-bound or deleted, the count drops back to 1. - When the count reaches 0, nothing can reach the object any more, and CPython reclaims its memory immediately.
Peeking at the reference count
sys.getrefcount() reports an object’s reference count:
import sys
my_var = [1, 2, 3, 4]
sys.getrefcount(my_var)You probably expected 1, but got 2. Passing my_var into getrefcount() creates a temporary extra reference, the function’s own parameter, for the duration of the call.
To avoid that, we can read the count straight out of memory. In CPython, the first field of every object is its reference count, so given an address, ctypes can read it without creating a new reference:
import ctypes
def ref_count(address: int) -> int:
"""Read the reference count stored at an object's memory address."""
return ctypes.c_long.from_address(address).value
ref_count(id(my_var))Here we pass id(my_var), which is just an integer, so no extra reference to the list is created.
Now add a second reference and watch the count change:
other_var = my_var
print(hex(id(my_var)), hex(id(other_var)))
print('ref count:', ref_count(id(my_var)))Both names point to the same address, and the count is now 2. Drop one of them:
other_var = None
print('ref count:', ref_count(id(my_var)))Some objects are used so often that CPython marks them immortal: None, True, False, small integers, and many interned strings. Their reference count is pinned to a huge sentinel value and never changes, which avoids a lot of bookkeeping (and helps free-threaded Python).
print(sys.getrefcount(None))
print(sys.getrefcount(10))That’s why we use a freshly created list in this lesson: its count behaves normally.
Reading memory with ctypes.from_address is only for exploring. If the address no longer belongs to a live object, you are reading garbage. Memory management in Python is automatic. We look under the hood only to understand the concepts that follow.
Key takeaways
- CPython stores a reference count in every object and frees the object as soon as the count drops to 0.
sys.getrefcount(x)is always one higher than you expect, because the call itself holds a reference.- Since Python 3.12, objects like
Noneand small ints are immortal and report a huge, fixed count.