Memory profiling in C++

Code profiling as said in the earlier post is the dynamic analysis of resources used by a program or a small section of it.

Here we will discuss about monitoring the memory during a run of a C++ program. Monitoring the memory greatly helps in optimizing your code. Memory leaks (when memory is not released back to the operating system and the operating system stops the program in the middle because of the over-usage of memory), swapping of data (swapping of data between main memory and disk greatly reduces the performance as disk IO is slow compared to main memory IO), free memory available at any point of time, when and where memory is allocated and freed, inaccessible areas of stack data, usage of cache (proper usage of cache memory can increase your performance), heap memory usages.

NOTE: using memory management tools reduces the performance of the program (it could get 100 times slower :O ). So it should only be done for testing, and development and not during production phase.

first let us see the various tools freely available to work for us:

  • debugger – just use your C++ debugger to keep track of memory leaks, memory allocations step-by-step. But, this process is very slow. Instead of compiling using g++, compile the program using “gdb”.
        gdb file.cpp


  • sysstat – just install this package using the command (sudo apt-get install sysstat).
    Using the command “free” will tell you memory statistics at that point of time. (you can use the -m option to show the memory in megabytes).
  • valgrind – This is the best tool available freely. (to install type: sudo apt-get install valgrind). It has the various subtools:
    • Memcheck – When a program is run under memcheck’s supervision, all reads and writes of memory are checked, and calls to malloc/new/free/delete are intercepted. Memcheck reports errors as soon as they occur, giving the source line number at which it occurred, and also a stack trace of the functions called to reach that line.
    •  Cachegrind -its a cache profiler. Tells you which part code has lead to a cache miss. The number of cache misses, number of instructions executed on each line of code.
    • Massif – performs detailed heap profiling. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations.

    Valgrind can be easily used from the terminal. When calling your program executable just write “valgrind” before the call using the appropriate options, eg:

        valgrind --leak-check=yes <myprog> arg1 arg2

    To know the options and other methods you can refer to its documentation from here.

  • leakfinder – This is a simple GUI application for windows to find leaks in your program with an basic inbuilt code editor.
  • gperftools – this is a tool developed by google for use by developers so that they can create more robust applications. Especially of use to those developing multi-threaded applications in C++ with templates. Includes TCMalloc, heap-checker, heap-profiler and cpu-profiler.
  • dmalloc – This is another tool available on the web.

The concept used in dmalloc is quite a simple one and we can ourselves make a simple library that can keep track of memory allocations and memory release. This concept is based on function and operator overloading. Here we will overload the new/delete operator. Thus, whenever a memory is allocated or freed, we can print the appropriate information. So let’s start,

#include <execinfo.h>   // this is a header file contains the backtrace function

void *caller()
{
      const int target = 3;     // trace three functions back
      void* returnaddresses[target];
      if (backtrace(returnaddresses, target) < target) {
               return NULL;
      }
      return returnaddresses[target-1];
}


void* operator new(size_t size) throw(std::bad_alloc) {
       void* ret = malloc(size);
       if (!ret) throw std::bad_alloc();
       cerr<<"allocate: "<<ret<<" "<<size<<" bytes from "<<caller()<<"\n";
}


void* operator new[] (size_t size) throw(std::bad_alloc) {
       void* ret = malloc(size);
       if (!ret) throw std::bad_alloc();
       cerr<<"allocate: "<<ret<<" "<<size<<" bytes from "<<caller()<<"\n";
}

void operator delete(void* data) {
       free(data);
       cerr<<"free: "<<data<<"\n";
}

void operator delete[] (void* data) {
       free(data);
       cerr<<"free: "<<data<<"\n"; 
}

Just write this code in a file and then you can include this as a header file in your program whenever you want to monitor your memory allocations and who allocates it. There is no need to write any extra code in your program. Similarly, you can overload the malloc/free functions if new/delete does not work for you.

good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *