I’d like to gauge interest in adding a method sys.getallocatedbytes that returns the total bytes currently allocated across CPython’s allocators (the raw domain plus the active object allocator, pymalloc or mimalloc).
Why? This would be useful to get fine-grained measurement of memory, for example to track import overhead.
How?
- Throughout the program we would keep track of the memory allocated from the raw domain (
_PyMem_Raw*methods) by adding/subtracting to a counter when the respective methods are called (usingmalloc_usable_size()/malloc_size()/_msize()to record the true block size). - Then when
sys.getallocatedbytes()is called we would sum that value with a walk over the active object allocator’s live allocations —pymalloc’s in-use pools, ormimalloc’s heaps