Menu

tracemalloc said 9.5 KB. macOS said 142 KB: why Python and Ruby profilers miss C heap?
📰

tracemalloc said 9.5 KB. macOS said 142 KB: why Python and Ruby profilers miss C heap?

DEV Community: ruby·Frank Liao·3 days ago
#DFtR4o0I
#devtoruby#devto#dev#include#ruby
Long article (1207 words) — no AI summary yet.
Reading 0:00
15s threshold

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.

In Ruby 4.0.5 wrapping the same C99 library, ObjectSpace.memsize_of reported only one wrapper slot while hiding over 137 KB of native heap. Built-in VM profilers stay blind to C heap unless both your benchmark and your extension cross the allocator boundary.

Why Python and Ruby miss 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 shell visible.

Ruby's GC.stat counts Ruby RVALUE slots. ObjectSpace.memsize_of(obj) includes native C allocations only when rb_data_type_t.function.dsize is implemented.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.