Under The Hood
backend python CPython 3.12+

CPython Memory Allocator: pymalloc Arenas, Pools, and Size Classes

Last updated
Prerequisites:
Python object memory model
Operating system `malloc`/`free`
  • python
  • cpython
  • memory
  • pymalloc
  • arenas
  • pools
  • allocation

Read at your depth

The practical view

CPython uses a dedicated small-object allocator called `pymalloc` for allocations `<= 512 bytes` (which covers 95%+ of typical Python integers, strings, tuples, and dict entries). `pymalloc` organizes memory hierarchically into **Arenas** (256KB chunks requested from OS `malloc`), **Pools** (4KB blocks inside arenas corresponding to a single size class), and **Blocks** (small chunks in 8-byte increments from 8 to 512 bytes). Objects larger than 512 bytes bypass `pymalloc` and use system `malloc`.

The same idea in other frameworks

python equivalent
CPython `pymalloc` (Arenas 256KB -> Pools 4KB -> Blocks <=512B).

Legacy vs modern

Small Object Allocation (pymalloc fastpath) vs Large Object Allocation (System malloc)

Small Python objects use fast `pymalloc` pools; objects >512B incur system `malloc` overhead.

before → after
Large Object (>512 bytes) System Malloc
bytearray(1024) # ❌ Bypasses pymalloc, calls system malloc()
Small Object (<=512 bytes) pymalloc Fastpath
items = [i for i in range(1000)] # ✅ NEW: Fast 8-instruction pymalloc pool bump

Interview gotchas

Context

This is the classic production Python memory debugging interview question.

The mechanical answer

Because `pymalloc` operates in **256KB Arenas**. An arena can only be released back to the OS via `free()` if **every single pool inside that arena is 100% empty**. If you allocate 1,000,000 objects and then delete 999,000 of them, but the remaining 1,000 surviving objects happen to be scattered across different arenas (1 object per arena), **none of the arenas can be freed**, and Python's RSS memory usage reported by the OS will remain high.

Trap

Calling `gc.collect()` or `del my_list` frees objects inside `pymalloc` pools for Python reuse, but does not guarantee the OS will see lower process RSS memory.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.