Master the fundamental concepts of cpython internals through this focused micro-challenge.
Most \`malloc\` calls in CPython do not hit the system allocator directly. \`obmalloc\` in \`Objects/obmalloc.c\` layers pools, arenas, and size classes tuned for small, short-lived objects.
Allocation tiers:
Small object requests (typical \`PyObject\` headers plus payload) are served from a free list inside a pool, avoiding syscall overhead and reducing fragmentation.
For example, allocating thousands of identical small tuples reuses slots from the same pool rather than calling \`malloc\` each time.
Stats to watch: pool utilization, arena count, and fallback rate to the system allocator under memory pressure.
Falling through to system `malloc` for large allocations is intentional. Pool exhaustion under fragmentation is a sign workloads allocate many different sizes; watch arena growth in long-running servers.
This exercise asks you to document obmalloc's pool and arena design. You will explain why Python allocates small objects differently than a generic \`malloc\` and how that shapes interpreter performance.
You will use the same mental model here when reading production interpreter source later in the track. Sketch one concrete input on paper, predict the outcome, then confirm with code. That discipline catches logic errors early and makes debugging far faster when you extend the implementation in follow-on tasks.
Write a C program documenting CPython's memory allocator.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github