Python's Memory Allocator Rarely Shrinks: A Deep Dive

First Steps into Memory Management in Python

A personal exploration of CPython 3.12's memory management: pymalloc for small objects (≤512 bytes) and malloc for larger ones, reference counting, and the garbage collector. The key insight: once memory is allocated, freeing it doesn't always reduce the memory the OS sees, because the allocator decides what goes back. You can control how long a reference lives, though.

Once memory is allocated, freeing it doesn't always reduce the memory the OS sees.

More from this day

2026-10-07