Measuring number and count of allocations

I would like to do some simple measurements of allocations. Basically create a collection of size N for various data structures, then measure the number of temporary and persistent allocations.

Ideally I would love to have something like criterion, but for memory. But since that does not seem to exist, something where I can just write a small test program that dumps a CSV for gnuplot would do as well.

There used to be a crate https://crates.io/crates/heapsize that at least allowed you to measure the total size of an object. But it is no longer supported and does not work with the current default allocator in any case.

Any recommendations how to accomplish this in current rust?

I don’t know if there’s a crate that already does this, but you could replace the default allocator with one that collects the statistics you want. There’s an example that does this in the alloc::System docs.

1 Like

stats_alloc adds hooks to the global allocator to count allocs and deallocs.

3 Likes

Thanks. That is what I am currently doing. Basically configure jemalloc and then measure per-thread allocations using jemalloc-ctl. Seems to work fine after some twiddling.

Swapping in a new allocator is surprisingly easy. I love rust.

Cool. Will check it out once I am done with playing with jemalloc-ctl.