Track C ++ memory allocation

I am looking for a way to track memory allocations in a C ++ program. I'm not interested in memory leaks that most tools seem to be trying to find, but rather create a memory usage profile for the application. An ideal output would be either a large list of function names, or the number of maximum allocated bytes with time or better, a graphical representation of the heap over time. The horizontal axis is time, the empty space of the vertical axis. Each function will receive its own color and draw lines in accordance with the allocated bytes of the heap. Bonus points to identify selected types of objects.

The idea is to find memory bottlenecks / visualize which functions / threads consume the most memory, and should be aimed at further optimization.

I looked briefly at Purify, BoundsChecker, and AQTime, but they don't seem to be like me. Valgrind looks suitable, however I'm on Windows. Memtrack looks promising, but requires significant changes to the source code.

Should my Google skills let me down because it doesn't look like such an unusual request? All the necessary information to create such a tool should be easily accessible from software debugging symbols plus runtime API calls - no?

+47
c ++ memory-management visualization
May 26 '09 at 11:16
source share
9 answers

Monitoring PC memory usage for game development provides an almost perfect example of what I was looking for. It took some time to launch it, but the author of the article was very helpful. Here you can find the source code of the Memtracer tool.

I also came up with many helpful answers to SWENG (Software Engineering mailing list). This thread is called "[Sweng-Gamedev] controlling C ++ memory usage?".

+7
Sep 01 '09 at 14:15
source share

Use Valgrind and its Massif tool. His example output (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 receive detailed information:

  • WHO has allocated memory (functions: g (), f () and main () in the above example); you also get full backtrace leading to the distribution of the function,
  • for the WHICH data structure into which the memory arrived (in the above example there are no data structures),
  • When did it happen,
  • what PERCENTAGE for the entire allocated memory (g: 39.7%, f: 9.95%, main: 49.7%).

Here is the guide for the array

You can track heap distribution as well as stack distribution (disabled by default).

PS. I just read that you are on Windows. I will leave the answer, because it gives an idea of ​​what you can get from a possible tool.

+31
May 26 '09 at 11:55 a.m.
source share

Microsoft has well-documented memory tracking features. However, for some reason they are not very well known in the developer community. These are CRT debugging features. A good starting point would be the CRT Debug Heap features .

Check out the following links for more details.

  1. Heap Status Reporting Features
  2. Track heap allocation requests . This is probably the functionality you are looking for.
+19
May 26 '09 at 11:35 a.m.
source share

For a general C ++ memory tracker, you need to overload the following:

 global operator new global operator new [] global operator delete global operator delete [] any class allocators any in-place allocators 

A complex bit receives useful information, overloaded operators have only size information for allocators and memory pointers for deletion. One answer is to use macros. I know. Nastya. An example is a place in the header that is included from all source files:

 #undef new void *operator new (size_t size, char *file, int line, char *function); // other operators #define new new (__FILE__, __LINE__, __FUNCTION__) 

and create the source file with:

 void *operator new (size_t size, char *file, int line, char *function) { // add tracking code here... return malloc (size); } 

The above only works if you do not have an operator defined in the class. If you have some in the scope of the class, follow these steps:

 #define NEW new (__FILE__, __LINE__, __FUNCTION__) 

and replace "new type" with "NEW type", but this requires a lot of code to be changed.

As a macro, deleting a memory tracker is quite simple, the title becomes:

 #if defined ENABLED_MEMORY_TRACKER #undef new void *operator new (size_t size, char *file, int line, char *function); // other operators #define NEW new (__FILE__, __LINE__, __FUNCTION__) #else #define NEW new #endif 

and implementation file:

 #if defined ENABLED_MEMORY_TRACKER void *operator new (size_t size, char *file, int line, char *function) { // add tracking code here... return malloc (size); } endif 
+14
May 26 '09 at 16:04
source share
+1
May 26 '09 at 11:21 a.m.
source share

On Mac OS X, you can use the Shark code profiling tool for this, IIRC.

0
May 26 '09 at 15:41
source share

In Xcode, you can use the Tools to track distributions, use a virtual machine, and several other parameters. Mostly popular with iOS developers, but worth a try.

0
Oct 22 '15 at 2:37
source share

The "graphical representation of the heap over time" is close to what you are looking for, implemented in the Intel (R) Single Event API , details can be found in this article (its quite large to add it here). Memory allocation over time

Shows a timeline of distributions by block size and allows you to add an extra label to your code to better understand the whole picture.

0
Jan 05 '16 at 11:39
source share

The Visual Studio IDE has built-in support for heap profiling (from 2015), which is perhaps the easiest to get started. It has a graphical representation of heap usage over time and can track distribution by function / method.

heap profiling

CRT also has debugging and profile support, which is more verbose and lower level. You can track data and display results using another tool:

In particular, take a look at _CrtMemCheckpoint and its related functions.

0
Jul 26 '19 at 14:13
source share



All Articles