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.

    Code Profiling for time in C++

    Code profiling is a very important aspect of programming. First you must be wondering what is code profiling. You can always google it but in simple words “code profiling” is just measuring of the resources used by your program or small sections of your program.

    Here I will be talking about code profiling for “time”. Dynamically measuring your code for how much time it takes for different input sets is of keen importance when you have to optimise your code. There are many ways you can achieve this,

    • there are many unix tools available to do the job for you.
      • time – just type time while calling your executable file.
        eg: time ./a.out
      • sysstat (to install just type “sudo apt-get install sysstat”) – It has many tools available to check the resources used up in the running processes.
        While your program is running, you can check up the resources used up your program by the commands “iostat -i”, “iostat -c”, “iostat -dx”. if networking is involved you can use “netstat -i”, “netstat -s”. To check the memory usage, i.e., free memory and used memory and memory swapped etc., you can type “free -m”.
      • callgrind – you can download this tool to profile your code.
    • The simple way is to put a small code inside your code to measure the time. Here I’ll tell you how to do that using a small library I have written.

     Its use as simple as writing:

    int func() {
        timeit s(“func()”);
        // your code here.
    }
    

    It is based on a simple concept that when a object goes out of its scope, its destructor is called. So to time a code snippet, you just have to make an object of the type “timeit” having the same scope as the code snippet. In the above example it will print to the standard error:
         
               func()       3.21554ms

     The object simply times the call between its construction and destruction. If its difficult to maintain the scope of the object by using curly brackets, then you can also use the “new” and “delete” to manually set the scope. The code is rather simple and here it goes:

    #include <iostream>
    #include <ctime>
    
    class timeit {
        char const *name;
        timespec t_start;
    
        public:
            
            timeit(char const *temp): name(temp) {
                clock_gettime(CLOCK_REALTIME, &t_start);
            }
            
            ~timeit() {
                timespec t_end;
                clock_gettime(CLOCK_REALTIME, &t_end);
                
                double dur = 1e3 * (t_end.tv_sec - t_start.tv_sec) + 1e-6 * (t_end.tv_nsec - t_start.tv_nsec);
                std::cerr << name << '\t' << std::fixed << dur << " ms\n";
            }
    };
    

    Just include this this code in your program and timeit 😉