What is the best way to measure the memory used by a C++ program or a block in a C++ program. The measurement code should thereby be part of the code and it should not be measured from outside. I know of the difficulty of that task, so it does not have to be 100% accurate but at least give me a good impression of the memory usage.
Share
Measuring at the block level will be difficult (at best) unless you’re willing to explicitly add instrumentation directly to the code under test.
I wouldn’t start with overloads of
newanddeleteat the class level to try to do this. Instead, I’d use overloads of::operator newand::operator delete. That’s basically the tip of the funnel (so to speak) — all the other dynamic memory management eventually comes down to calling those (and most do so fairly directly). As such, they will generally do the most to tell you about dynamic memory usage of the program as a whole.The main time you’d need to deal with overloads of
newanddeletefor an individual class would be if they’re already overloaded so they’re managing a separate pool, and you care about how much of that pool is in use at a given time. In that case, you’d (just about) need to add instrumentation directly to them, to get something like a high-water mark on their memory usage during a given interval.