Back
+20 XP
Concept

CPython Memory Architecture: Arenas, Pools & pymalloc

Calling OS `malloc` for millions of tiny allocations creates catastrophic memory fragmentation. CPython's `pymalloc` allocator partitions memory into 256KB Arenas, 4KB Pools, and fixed-size Blocks (8 to 512 bytes), allocating small objects in single-digit CPU cycles 🧱⚡.

python
import sys

# Small allocations (< 512 bytes) go to pymalloc pools:
int_size = sys.getsizeof(42)           # 28 bytes
float_size = sys.getsizeof(3.14159)    # 24 bytes
empty_tuple = sys.getsizeof(())        # 40 bytes

# Large allocations (> 512 bytes) bypass pymalloc to system malloc:
large_blob = sys.getsizeof(b"x" * 1024) # 1057 bytes

print(f"Int: {int_size}B, Float: {float_size}B (Serviced by pymalloc) 🚀")
print(f"Large Blob: {large_blob}B (Serviced by system malloc) 🧱")

# 79. CPython Memory Architecture: Arenas, Pools & pymalloc If Python called the Linux kernel's `brk()` or `mmap()` every time you instantiated a number or appended a list item, your CPU would spend 80% of its time executing system call interrupts in kernel space 🛑. ```python # The 3-Tier pymalloc Hierarchy: # 1. Arenas (256 KB): Allocated from OS via malloc(). Aligned on page boundaries. # 2. Pools (4 KB): Sliced out of an Arena. Dedicated to ONE specific block size class. # 3. Blocks (8B to 512B): Sliced out of a Pool. Handed directly to PyObject! ``` ### The Block Size Classes 1. **Size Class Granularity**: `pymalloc` defines 64 size classes in 8-byte increments: class 0 = 8 bytes, class 1 = 16 bytes, class 2 = 24 bytes, up to class 63 = 512 bytes. 2. **Zero Internal Fragmentation**: When allocating a 20-byte object, CPython assigns it to a 24-byte block pool. Every block in that 4KB pool is exactly 24 bytes, eliminating external heap fragmentation. 3. **Large Object Fallback**: Objects larger than 512 bytes bypass `pymalloc` completely and are allocated directly through the standard C library `malloc()` 🧱⚡. --- ### Interactive Pop-Quiz 🧠 #### What is the maximum object size threshold serviced by CPython's specialized pymalloc allocator before falling back to system malloc()? - [ ] 512 bytes (allocated in 4KB pools across 256KB arenas) 🧱 - [ ] 64 kilobytes - [ ] 4 megabytes - [ ] 16 bytes --- ### Practical Challenge 🎯 Inspect the memory size of `sys.getsizeof(obj)` for an integer, a float, and a 600-byte byte string `b'a' * 600`. Verify that the integer and float are `<= 512` bytes (pymalloc tier) and the byte string is `> 512` bytes (system malloc tier). Print `'PYMALLOC_TIERS_VERIFIED'`. <details> <summary>Solution</summary> ```python import sys sz_int = sys.getsizeof(100) sz_float = sys.getsizeof(2.718) sz_large = sys.getsizeof(b"a" * 600) assert sz_int <= 512 assert sz_float <= 512 assert sz_large > 512 print("PYMALLOC_TIERS_VERIFIED") ``` </details>

Checkpoint

What is the maximum object size threshold serviced by CPython's specialized `pymalloc` allocator before falling back to system `malloc()`? 🧠

Practice

Inspect the memory size of `sys.getsizeof(obj)` for an integer, a float, and a 600-byte byte string `b'a' * 600`. Verify that the integer and float are `<= 512` bytes (pymalloc tier) and the byte string is `> 512` bytes (system malloc tier). Print `'PYMALLOC_TIERS_VERIFIED'`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks