Memory management in CPython, fast or slow?
summary
CPython spends one third of runtime on memory management and currently services roughly half of allocations from small-object free lists; there are 32 size classes up to 512 bytes, 41 per-class free lists (20 for tuples), arenas are ~2 MB and pages typically 4 KB. Fragmentation and thread‑state lookups cost time; stack-local ref elision removes about 90% of local ref ops and converting free lists to size-class allocation plus JIT unboxing aims to push common-case allocation to 99% from free lists and cut overhead.
AI-written summary · about
chapters (16)
- 0:00Memory management in CPython
- 2:13Reference counting in CPython
- 3:36Free lists for fast allocation
- 5:45Pages and arenas in CPython
- 7:28Size classes and fragmentation
- 11:00Inefficiencies in CPython memory
- 13:08Thread state overhead
- 15:55Allocator overheads
- 17:21Free list limitations
- 18:22Optimizing reference counting
- 19:24Future free list improvements
- 20:47Fast and slow allocation paths
- 21:49JIT optimizations for tuples
- 22:51Conclusion and contributions
- 23:13Cyclic garbage collection
- 27:10Q&A on free lists
related talks
23mMemoirs of a Python Object: Memory Management and Improving Performance
25mIs it me or Python memory management? 🤔💻
29mTrip Down Memory Lane: OOM in Python
42mOptimizing Code Performance for Python Internals
33mMemory Tiering in Python Virtual Machine
01h 18mPython 103: Mmmm... Understanding Python's Memory Model, Mutability, and Methods
01h 03mA tour of CPython's runtime
33mWhy is Python slow?
