1

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.

Ben Burns
  • 14,978
  • 4
  • 35
  • 56
  • IIRC valgrind supports this. – πάντα ῥεῖ Jan 11 '16 at 21:56
  • Write it up as an answer with a bit of instruction as to how to get this info and I'll happily upvote and/or accept. – Ben Burns Jan 11 '16 at 21:58
  • Sorry, but I'm more agreeing on: _"Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."_ – πάντα ῥεῖ Jan 11 '16 at 22:03
  • Fair enough - I'll rephrase. – Ben Burns Jan 11 '16 at 22:04
  • @πάντα ῥεῖ - does my edit solve your concern? – Ben Burns Jan 11 '16 at 22:08
  • Not really, you're still primarily asking for a tool. E.g. valgind I have mentioned isn't available for all platforms. If you get more specific about that point, your question might be answerable outside of that constraint I've mentioned. – πάντα ῥεῖ Jan 11 '16 at 22:13
  • _"What is the best way to track not only the size and volume of memory allocations"_ - With a profiling tool (Sorry but this question is too broad) – Captain Obvlious Jan 11 '16 at 22:15
  • Are you worried about stack memory, heap memory, or both? – kchoose2 Jan 11 '16 at 22:31
  • I'm trying to determine how frequently duplicitous representations of data are acccessed so that I can decide whether or not to make them short-lived and create-on-access in order to reduce peak heap size. Will add to question. – Ben Burns Jan 11 '16 at 22:37
  • Hopefully recent edits have addressed concerns regarding this question being too broad. I understand that memory profilers track allocations. I'm specifically looking for a tool or method which will let me track both allocations _and_ accesses efficiently. – Ben Burns Jan 11 '16 at 22:55
  • 1
    @BenBurns _" I'm specifically looking for a tool or method ..."_ which is explicitly off-topic as mentioned. – πάντα ῥεῖ Jan 11 '16 at 23:24

2 Answers2

3

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
MartinVeronneau
  • 1,296
  • 7
  • 24
  • The point that is missing from this answer is the tracking of _accesses_ to memory. I'm trying to determine how frequently duplicitous representations of data are acccessed so that I can decide whether or not to make them short-lived and create-on-access in order to reduce peak heap size. – Ben Burns Jan 11 '16 at 22:35
  • I never used it, but the Valgrind's DHAT tool does that. [link](http://valgrind.org/docs/manual/dh-manual.html) – MartinVeronneau Jan 11 '16 at 22:41
  • if you want to add instructions for DHAT to this answer (or even just a mention of it), I'll accept it. DHAT looks like it will likely give me what I'm after. – Ben Burns Jan 17 '16 at 21:39
1

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.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135