This page provides an overview of CPython's object system and memory management architecture. Every Python value is represented as an object at the C level, with a common header that enables reference counting, type identification, and garbage collection.
CPython's object system consists of:
ob_refcnt (or biased counting in free-threaded builds)PyDictObject and PySetObjectRelated pages:
For how these objects are manipulated during execution, see Bytecode Interpreter and Tiered Optimization.
All Python objects begin with a PyObject header that provides identity, type information, and reference counting:
PyObject Layout
Key structures:
PyObject - Base for fixed-size objects (int, float, type, etc.)PyVarObject - Base for variable-size objects (list, tuple, str, etc.) with ob_size fieldob_refcnt - Reference count for memory management (default build)ob_ref_local/ob_ref_shared - Split reference counts (free-threaded build)ob_type - Pointer to the object's type (PyTypeObject)Sources: Include/object.h124-166 Include/cpython/object.h1-50
The type system defines object behavior through PyTypeObject structures:
Type System Architecture
Key type system features:
tp_dict - Contains methods and attributes accessible via lookuptp_mro - Method resolution order for inheritancetp_version_tag - Enables efficient cache invalidation when types changetp_getattr, tp_setattr, tp_call, etc.) - Function pointers for operationsFor detailed type system mechanics, see PyObject and Type System.
Sources: Objects/typeobject.c1-100 Objects/typeobject.c987-1048 Include/cpython/object.h155-403
CPython's primary memory management mechanism is reference counting. Each object tracks how many references point to it:
Reference Counting Flow
Key functions:
Py_INCREF(op) / Py_DECREF(op) - Increment/decrement reference count_Py_Dealloc(op) - Deallocate object when count reaches zero_Py_IsImmortal(op) - Check if object is immortal (never deallocated)_Py_ExplicitMergeRefcount(), _Py_brc_queue_object() for biased countingDetailed reference counting mechanisms, including free-threaded biased reference counting, are covered in Reference Counting and Garbage Collection.
Sources: Include/object.h570-650 Objects/object.c334-431 Include/internal/pycore_object.h134-196
Reference counting cannot detect cyclic references (e.g., a list containing itself). CPython includes a garbage collector for this:
Garbage Collection Architecture
Generational GC:
Key GC structures and functions:
PyGC_Head - Linked list node for GC tracking (default build)_PyGC_TRACK(op) / _PyGC_UNTRACK(op) - Add/remove from GC trackinggc_collect_main() - Main collection entry point_PyGCState - Per-interpreter GC state in PyInterpreterStateSee Reference Counting and Garbage Collection for detailed GC algorithms and cycle detection.
Sources: Python/gc.c1-173 Python/gc_free_threading.c1-108 Include/internal/pycore_gc.h115-180
Dictionaries are the most critical container type in CPython, used for namespaces, object attributes, and general key-value storage:
Dictionary Structure
Key dictionary features:
DICT_KEYS_UNICODE) when all keys are stringsPERTURB_SHIFT and probingDictionary operations:
dictkeys_get_index() - Map hash to entry indexinsertdict() - Insert key-value pairlookdict() / lookdict_unicode() - Key lookupdictresize() - Grow dictionary when load factor exceededFor detailed hash table mechanics and split-table optimization, see Dictionary and Container Objects.
Sources: Objects/dictobject.c8-89 Objects/dictobject.c507-558 Include/internal/pycore_dict.h1-84
Sets provide unordered collections with fast membership testing:
Set Structure
Set features:
set_add(), set_discard(), set_intersection(), set_union()Sources: Objects/setobject.c1-46 Include/setobject.h
The free-threading build implements biased reference counting to reduce contention:
This approach allows most reference counting operations to avoid expensive atomic operations when objects are accessed primarily by their creating thread.
Sources: Objects/object.c357-523 Include/internal/pycore_object.h345-412
Certain high-frequency objects use specialized per-thread reference counting:
This optimization is particularly important for function calls, attribute access, and other operations that frequently manipulate type and code objects.
Sources: Include/internal/pycore_object.h325-412 Include/internal/pycore_uniqueid.h
Refresh this wiki