6

I'd like to count number of memory allocation calls (malloc, calloc, new ...) in my program. The program actively uses STL containers. The main purpose is to count memory allocations inside all these containers. I will use this information to optimize performance later. My program is written on C++ and runs under Linux. Are there any tools to do it?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Sandro
  • 2,707
  • 2
  • 20
  • 30
  • 4
    Have you look at valgrind ? – Jarod42 Feb 25 '15 at 16:00
  • 1
    You can use valgrind if you want to count just the total number of bytes allocated. If you want to count the number of `malloc` calls then the simplest way would be to write a wrapper for it - see [this question](http://stackoverflow.com/a/262481/3791842). – Daniel Kleinstein Feb 25 '15 at 16:00
  • 1
    [This page](http://www.gnu.org/savannah-checkouts/gnu/libc/manual/html_node/Hooks-for-Malloc.html) has a useful example (specific to `glibc`) that prints information out each time `malloc` is called. You can use similar code to just increment a global counter. – Daniel Kleinstein Feb 25 '15 at 16:02
  • 1
    What kernel version/glibc version/distro do you have? I would recommend you to try [SystemTap](https://sourceware.org/systemtap/), but it will require glibc debuginfo and kernel 3.5+ (rhel kernels may have special patches on lowest versions). – myaut Feb 25 '15 at 17:43
  • I looked at valgrind. It seems it just shows total number of allocated bytes. But maybe there is some specific tool that can show the number of alocations. – Sandro Feb 25 '15 at 21:23
  • Thank you Daniel, I think overriding malloc can help. – Sandro Feb 25 '15 at 21:28
  • Thank you myaut unfortinately my kernel is older than 3.5 – Sandro Feb 25 '15 at 21:31
  • Long shot - what if you derived from `std::allocator###` that gathers the data regarding memory allocations and deallocations and then provide those allocators to all containers you use in your program? Not the most elegant and the easiest solution, but it may work. – Fureeish Nov 04 '19 at 13:15

3 Answers3

1

You can overload operator new:

#include <stdlib.h>

int numOfHeapAllocations = 0;

void* operator new(size_t size)
{
    numOfHeapAllocations++;
    return malloc(size);
}

Just put it the first thing in your main file.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Hachem
  • 11
  • 1
0

You could redefine the operators you want to count.

Example: How to use operator new to count number of times of dynamic memory allocation

Community
  • 1
  • 1
JordanBean
  • 1,949
  • 21
  • 28
  • I think just overloading operator "new" can not help me (I think standard stl allocators usualy allocate memory with malloc and than use placement new to put object there. But there is also a link to memory allocation hooks, probably I can try to use them. – Sandro Feb 25 '15 at 21:34
-1

Here's something I whipped up in my C++ development environment for linux. you might need to change iostream to stdio.h and cstdlib to stdlib.h (and possibly std to match windows name spaces) to make it work in windows.

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>


static char **ms;
using namespace std;

class frag{

    public:
    int xyz;

};

int numsegments(char** segs){
    return strlen((const char*)segs)/sizeof(char*);
}

void randomfunction1(){
    ms[numsegments(ms)]=(char*)malloc(100);
}

void randomfunction2(){
    ms[numsegments(ms)]=(char*)calloc(1,100);
}

void randomfunction3(){
    ms[numsegments(ms)]=(char*)new frag(); //storing class as flat data in memory
    int segct=numsegments(ms); // get segments
    frag* init=(frag*)ms[--segct]; // subtract 1 from segment count before reading
    init->xyz=1; // set variable in class
}

int main(int argc, char *argv[]){
    ms=(char**)calloc(1,1000); //store up to 1000 indexes
    printf("Segments used so far: %d\n",numsegments(ms));
    randomfunction1();
    randomfunction2();
    randomfunction3();
    int total=numsegments(ms);
    printf("Segments used total: %d\n",numsegments(ms));
    int lastsegment=total-1;
    frag* y=(frag*)ms[lastsegment]; //convert to class
    printf("xyz in class = %d\n",y->xyz);
    for (int i=0;i<total;i++){free(ms[i]);} //free all segments
    free(ms);
    return 0;
}

I understand its complex but the basic idea of this program is to allocate a chunk of memory to store pointers to memory fragments then allocate whatever for each fragment then use strlen() to quickly count the fragments. I know you count them as allocations, but I call them fragments.

  • Sorry I don't understand your idea. The main problem is to count how many realocations were done inside stl containers (vector, string, etc..). How your solution can help to do it? – Sandro Feb 25 '15 at 21:21