Ben Burns
Ben Burns

Reputation: 15216

How can I track the frequency and volume of memory access in a C++ application during memory profiling?

I'm attempting to reduce the memory footprint of a C++ application. Over time this application's use of memory has grown due to developers creating new, duplicate representations of data in memory for various purposes.

I'm would like to determine how frequently these duplicitous representations of data are accessed so that I can decide whether or not to make them short-lived and create-on-access in order to reduce peak heap size.

So my question is - what is the best way to track not only the size and volume of memory allocations, but also the frequency and volume of accesses to heap memory? I know that all basic memory profilers handle allocation info - correlating that to memory accesses is what I'm interested in.

An ideal answer would be platform independent, as this application runs on Windows, Linux, iOS, and Android. However, I'll accept answers which work on any of those platforms and for any processor architecture commonly used by those platforms, as we don't have platform-specific behaviour which should impact this sort of thing.

Upvotes: 1

Views: 443

Answers (2)

Mike Dunlavey
Mike Dunlavey

Reputation: 40689

Two points:

1) If you are looking for memory leaks (which can be very slow) the way to do that is to use one of the methods for seeing what memory blocks have not been freed when the program finishes. That allows you to figure out where they came from, and why they were not freed.

2) If it is a matter of excessive memory allocation (and freeing) I've found that it is accompanied by a large fraction of time spent doing that. So it is not just a memory issue, it is a performance issue, and those are easy to find. Here's an example.

Notice, this is a little different from what you asked. You asked how to track the memory allocations, so that you could find the ones that could be eliminated. What this technique does is find them directly, without going through the tracking part. The way that works is that memory allocation and freeing is computationally expensive, so it tends to account for a large fraction of cycles, so random-time samples easily expose it.

Upvotes: 1

MartinVeronneau
MartinVeronneau

Reputation: 1306

Like it was commented, your question is very broad.

I can't answer it in a specific manner, but I'll assume that you have access to the source code, you can compile it with gcc, and your plateform supports Valgrind. If my assumptions are false, please update your question, as the following is a crude tutorial on Valgrind's massif, and that was not what was asked for.

  1. Install Valgrind
  2. Compile your program with -g and -O0
  3. Run your program with valgrind --tool=massif your.exe
  4. Once the execution is completed, the massif tool will have created a file named massif.out.[PID]
  5. Run the command ms_print massif.out.[PID]

This will produce a graph showing the memory consumption and detailed information about all the allocation points in the program, including the point of peak memory allocation.

If you want to track the access to memory, you can use the DHAT tool (see this link for detailed instructions) :

  1. As with massif, compile your program with -g and -O0
  2. Run your program with valgrind --tool=exp-dhat your.exe

Upvotes: 3

Related Questions