When considering Python performance, memory management often gets less attention than features like free-threading or the JIT compiler. Yet, roughly a third of the runtime of a typical Python program is spent performing memory management. Understanding how CPython allocates, tracks, and frees memory is important for understanding Python performance.
Most Python developers know that CPython uses reference counting to determine when objects can be freed. But that’s only one part of a much more intricate system. Behind the scenes, CPython must also:
- Allocate memory for new objects quickly and efficiently
- Return freed memory back to the system or to internal allocators for reuse
- Detect and collect cyclic references, which reference counting alone cannot handle; CPython includes a dedicated cycle detector to ensure these objects are eventually freed
In this talk, I will:
- Trace the lifetime of a Python object through the lens of memory management
- Examine the time and space costs associated with each component of memory management
- Highlight improvements made in recent CPython releases, and discuss future opportunities to further reduce these costs