Benchmarking memory footprint of method

So I want to inspect the memory footprint of cloning an object in a for loop, to be specific a git2::Commit. I'm building my own iterator function and in designing a wider aspect of the API I may need to clone the object. Is there a way to write a benchmark test to prove this to myself??

The obvious answer is it is 2*size_of(git2::Commit), but I want to be sure that throughout the iteration (via revwalk) that everything is de-allocated as it should be such that each iteration has a footprint of 2*size_of(git2::Commit) + misc and not N * (2*size_of(git2::Commit) + misc) at the end of the iteration, where N is the number of commits accessible via revwalk

Sorry if this isn't very clear, it is my first post, so I'm happy to clarify anything :slight_smile: I also realise that I'm effectively trying to prove that variable scoping is working during iteration, so this may be a "duh that's how rust works" moment :rofl:

What you need is a memory allocator with instrumentation.

I use allocation-counter for this purpose. It’s not perfect, but it does the job.

Thanks for the quick reply!! I'll give that crate a go! :slight_smile:

hotpath markets itself with the promise of having high-level way to measure functions as you describe, take a look if it could work for you: https://hotpath.rs/

I haven't used it more than once, and for that it worked and was cool to see!