Skip to content

2. Objects and Memory

The Python object model, PyObject and PyVarObject layout, reference counting, the garbage collector, memory allocators, and built-in type implementations.

ChapterTitle
7The Python Object Model
8PyObject and PyVarObject
9Reference Counting
10The Garbage Collector
11Memory Allocators
12Object Layout and Type Slots
13Built-in Object Implementations
14Strings, Bytes, and Unicode
15Lists, Tuples, and Arrays
16Dictionaries and Sets
17Integers, Floats, and Complex Numbers
7. The Python Object ModelHow every value in Python is a heap-allocated object, and the role of type objects in defining behavior.
14 min
8. PyObject and PyVarObjectThe PyObject and PyVarObject C structs: ob_refcnt, ob_type, and the ob_size extension for variable-length objects.
13 min
9. Reference CountingPy_INCREF, Py_DECREF, and the deterministic lifetime rules that drive most memory reclamation in CPython.
14 min
10. The Garbage CollectorCycle detection algorithm, generational collection, gc thresholds, and the interaction with reference counting.
13 min
11. Memory AllocatorsThe pymalloc small-object allocator, arenas, pools, blocks, and when CPython falls back to system malloc.
13 min
12. Object Layout and Type SlotsPyTypeObject slot layout, tp_* function pointers, and how type slots encode object behavior for the interpreter.
12 min
13. Built-in Object ImplementationsC-level implementations of None, True, False, NotImplemented, and Ellipsis as statically allocated objects.
12 min
14. Strings, Bytes, and UnicodePyUnicodeObject internal encodings, the interning table, bytes vs. bytearray, and the codec infrastructure.
12 min
15. Lists, Tuples, and ArraysPyListObject dynamic resizing strategy, PyTupleObject immutability, and the array module's typed buffer.
12 min
16. Dictionaries and SetsCompact dict implementation, hash table probing, split-key sharing, and PySetObject collision resolution.
13 min
17. Integers, Floats, and Complex NumbersPyLongObject digit array for arbitrary precision, PyFloatObject IEEE 754 storage, and complex number layout.
13 min