Sub topic 8
How CPython obtains memory: pymalloc, arenas, pools, and blocks
This chapter assumes you know that CPython frees most objects through reference counting, and that sys.getsizeof reports an object's size. Here you will see where the bytes for those objects come from.
Why CPython has its own allocator
A typical program creates and frees huge numbers of small objects such as ints, short strings, tuples, and bound methods. Calling the operating system's malloc for each one is slow and wastes space on bookkeeping. CPython therefore puts a small-object allocator, called pymalloc, in front of the system allocator.
pymalloc handles requests of 512 bytes or less. Larger requests go straight to the C library's malloc.
The three levels
pymalloc organizes memory into three nested units.
- A block is the unit handed to an object. Every block in a pool has the same size.
- A pool is a fixed-size chunk that holds blocks of one size class only.
- An arena is a large chunk obtained from the operating system. It is divided into pools.
Sizes depend on the version. Before Python 3.10, arenas were 256 KiB and pools were 4 KiB. From 3.10 on, 64-bit builds use 1 MiB arenas and 16 KiB pools. Block sizes come in steps of 16 bytes on 64-bit builds (8 bytes in older versions), so a 20-byte request receives a 32-byte block.
How an allocation works
- Python asks for
nbytes. Ifnis over 512, the request goes to the systemmalloc. - Otherwise pymalloc rounds
nup to a size class. - It finds a pool for that class that still has a free block. If none exists, it takes a fresh pool from an arena, and if no arena has room, it asks the operating system for a new arena.
- It returns a free block from that pool.
When an object is freed, its block goes back onto its pool's free list. The block can be reused immediately by a later object of the same size class.
Seeing it in a running program
CPython has a diagnostic function that prints the allocator's state to standard error. The underscore means it is not a stable public API.
import sys
keep = [object() for _ in range(10_000)]
sys._debugmallocstats()The output has a table of size classes with the number of pools, used blocks, and free blocks for each. It ends with the number of arenas allocated and the bytes they hold. Run it before and after creating the list to compare.
Why memory is not always returned
An arena goes back to the operating system only when every pool in it is empty. If a long-lived object sits in one block of an arena, the whole arena stays. This is a form of fragmentation. After a program frees millions of objects, the process size can stay high even though Python reports little memory in use.
Free blocks are still reused. Your next small allocation fills the gaps, so the process rarely grows without bound for this reason alone.
What this means in practice
sys.getsizeofreports the size of the object, not the block. The block may be slightly larger because of rounding.- The process's resident memory (the figure shown by
topor Task Manager) can differ a lot from the total of your object sizes. - Many small, short-lived objects are cheap, because their blocks are recycled. Many small objects that are alive at the same time cost the most.
The next chapters use tracemalloc, which reports Python-level allocations and does not show arena layout.
Exercise
A program builds a list of 5 million small tuples, then deletes the list. Memory use in the operating system's process monitor drops only slightly. Give one reason based on pymalloc.
Show answer
The tuples' blocks live in pools inside arenas. An arena returns to the operating system only when all of its pools are empty. If a few long-lived objects share arenas with the freed tuples, those arenas stay allocated. The freed blocks remain available for reuse by later small objects, but the process size does not shrink.