7

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?

8
  • 4
    Have you look at valgrind ? Commented Feb 25, 2015 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. Commented Feb 25, 2015 at 16:00
  • 1
    This page 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. Commented Feb 25, 2015 at 16:02
  • 1
    What kernel version/glibc version/distro do you have? I would recommend you to try SystemTap, but it will require glibc debuginfo and kernel 3.5+ (rhel kernels may have special patches on lowest versions). Commented Feb 25, 2015 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. Commented Feb 25, 2015 at 21:23

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you do this I think you should also overload operator delete, since operator new is not required to use malloc (though for most systems it seems it does use malloc).
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

1 Comment

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.
0

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.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.