Phase 9 of 25 · Topic 9.5

CPython Memory Allocator Hierarchy (PyMem & PyMalloc)

1Concept

CPython organizes memory into a 4-tier hierarchy: 1. OS Allocator (`malloc`); 2. Python Core Allocator (`PyMem_Malloc`); 3. `PyMalloc` (Small Object Allocator for objects <= 512 bytes, divided into Arenas (256KB), Pools (4KB), and Blocks); 4. Object-specific allocators.

2Architecture Diagram

Object Request (<= 512B) ---> [ PyMalloc ] ---> [ 4KB Pool ] ---> [ Size-class Block ]
Object Request (> 512B)  ---> [ OS malloc() directly ]

3Code Example

Python 3.12
import sys

print("=== CPython Memory Allocator Configuration ===")
print(f"Pointer Size: {sys.maxsize.bit_length() + 1}-bit architecture")
print(f"Int Cache Range: -5 to 256")
print(f"PyMalloc Threshold: <= 512 bytes allocated in sub-pools")

4Expected Output

=== CPython Memory Allocator Configuration ===
Pointer Size: 64-bit architecture
Int Cache Range: -5 to 256
PyMalloc Threshold: <= 512 bytes allocated in sub-pools

5Key Takeaways

  • PyMalloc avoids OS system call overhead by pre-allocating 256KB memory arenas.
  • Memory released back to PyMalloc pools might not immediately return to the OS heap.
  • Use `PYTHONMALLOC=debug` environment variable to detect buffer overruns in native extensions.