track C++ memory allocations

Use Valgrind and its tool Massif. Its example output (a part of it):

99.48% (20,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->49.74% (10,000B) 0x804841A: main (example.c:20)
| 
->39.79% (8,000B) 0x80483C2: g (example.c:5)
| ->19.90% (4,000B) 0x80483E2: f (example.c:11)
| | ->19.90% (4,000B) 0x8048431: main (example.c:23)
| |   
| ->19.90% (4,000B) 0x8048436: main (example.c:25)
|   
->09.95% (2,000B) 0x80483DA: f (example.c:10)
  ->09.95% (2,000B) 0x8048431: main (example.c:23)

So, you will get detailed information:

  • WHO allocated the memory (functions: g(), f(), and main() in above example); you also get complete backtrace leading to allocating function,
  • to WHICH data structure the memory did go (no data structures in above example),
  • WHEN it happened,
  • what PERCENTAGE of all allocated memory it is (g: 39.7%, f: 9.95%, main: 49.7%).

Here is Massif manual

You can track heap allocation as well as stack allocation (turned off by default).

PS. I just read that you’re on Windows. I will leave the answer though, because it gives a picture of what you can get from a possible tool.

Leave a Comment