r/Python • u/Striking-Pride-2663 • 4h ago
Discussion tracemalloc said 9.5 KB. macOS said 142 KB: why Python profilers miss C heap
tracemalloc.get_traced_memory() reported 9,457 B per compiled 500-item $in rule on Python 3.14.5. Calling macOS malloc_zone_statistics around the same 1,000-instance loop reported 142,317 B per instance, 15x more RAM than Python admitted existed.
Built-in Python profilers stay blind to C heap unless both your benchmark and your C extension cross the allocator boundary.
Why tracemalloc misses C heap
Python's tracemalloc hooks PYMEM_DOMAIN_RAW, PYMEM_DOMAIN_MEM, and PYMEM_DOMAIN_OBJ (PyObject_* and PyMem_*). Direct libc malloc or calloc calls inside a wrapped C library skip all three hooks, leaving only the thin Python wrapper shell visible.
sys.getsizeof(obj) has the same blind spot unless the extension implements __sizeof__ to add up the native structs and pools behind the PyObject.
Measuring real bytes outside the VM
On macOS (arm64), Python's standard library can read both live native heap and process RSS via ctypes and resource:
=
=
=
=
return
return
On Linux (glibc 2.33+), use mallinfo2().uordblks for live malloc bytes and /proc/self/status (VmRSS / VmHWM), noting that ru_maxrss returns KiB on Linux and bytes on macOS.
Where the hidden 142 KB went (and when C arenas lose)
Holding 1,000 compiled rules alive on an Apple M3 (macOS arm64) exposed the gap:
| Rule (1,000 retained) | Python tracemalloc | Real malloc (size_in_use) |
|---|---|---|
Simple (age >= 18) | 1,171 B | 5,646 B |
Nested (profile.city + 2 ops) | 1,210 B | 9,740 B |
500-item $in hash | 9,457 B | 142,317 B |
Each compiled object allocates two 2 KB bump-allocated arena pools (one for the AST and lookup tables, one reusable scratch pool for zero-malloc matching) plus binding structs, setting a ~5 KB floor per rule (pure C uses 2,256 B standalone or 520 B with a shared arena).
Comparing against a pure-Python peer (mongoquery 1.4.3, +0.2 MiB RSS) shows the trade-off runs both ways:
mongoqueryretains only64 Bto92 Bper rule and peaks at2.3 KiBto3.7 KiBbecause CPython refcounting frees temporaries right away, shifting the cost into9xto2,314xhigher CPU time (258,170 ns/recvs111.6 ns/rec).- Caching 500,000 tiny distinct rules flips the win around:
~5 KBper rule eats~2.5 GBof hidden C heap until OOM-kill. Pure GC ports avoid the 2 KB C chunk floor (640 Bin Go,834 Bin JS), and one-off queries over small lists are cheaper with plain Python.
Fixing it with PyMem_RawMalloc and __sizeof__
Wiring the C library's allocator callbacks to PyMem_RawMalloc and PyMem_RawFree routed every C arena into tracemalloc, and adding __sizeof__ exposed native pools to sys.getsizeof.
Two practical constraints:
PyMem_RawMalloc/PyMem_RawFreeare GIL-free and returnNULLon OOM, so they don't break a C library'sNULL-checked OOM paths (unlikePyMem_Malloc, which requires the GIL).- Every pointer allocated through that hook must be freed with
PyMem_RawFree, never libcfree, ortracemalloccrashes on teardown.
After wiring it up:
tracemallocjumped from9,457 Bto141,137 Bper 500-item$ininstance (97.8%of the144,364 Btracedmalloc_zone_statisticstotal).sys.getsizeofreported4,408 B,8,504 B, and131,384 B.- Matching speed stayed flat (
89.2 ns/recvs86.8 ns/rec).
If you maintain Python C extensions with custom allocators or arena pools, do you wire them through PyMem_RawMalloc or test native heap (mallinfo2 / malloc_zone_statistics) in CI?