How do you benchmark memory consumption?

2020-07-11 05:23发布

问题:

I would like to know if there is an efficient way to measure the actual memory consumption of a particular C data structure.

The goal being to make benchmarks based on how the memory usage changes after specific operations on those data structures.

I do not seek a way to count the number of objects in use; I do want to know exactly how big the memory usage of an object put under stress can get.

Is there a standard way to do that, either in C code, or from outside? (Some equivalent to the time (1) utility would be a start).

Obviously, I could track down every single pointer and do a sum of all sizeofs. If this is the only way, please do tell me. I wonder whether there is a simpler way. Or maybe a library to do it for me.

回答1:

If you want to monitor the memory usage of the program on a global level you can replace new/delete in C++ or malloc/free in C with your own functions and log the memory usage.



回答2:

On Unix for memory consumption you can use valgrind with the tool Massif (+ any visualization tool), but I don't know if it is suited for your problem since it will give you a detailled view of all the memory consumption of your program.



回答3:

Yep, cnicutar, on Linux you have pmap or maybe even pstat.

On MS there are myriad profiling tools for VStudio depending on your contribution to the MS machine (even free ones for cmd line use). Call me a green horn, I don't have issues with memory leaks.