views:

374

answers:

4

During the last few days I've gained some information about memory allocators other than the standard malloc(). There are some implementations that seem to be much better than malloc() for applications with many threads. For example it seems that tcmalloc and ptmalloc have better performance.

I have a C++ application that uses both malloc and new operators in many places. I thought replacing them with something like ptmalloc may improve its performance. But I wonder how does the new operator act when used in C++ application that runs on Linux? Does it use the standard behavior of malloc or something else?

What is the best way to replace the new memory allocator with the old one in the code? Is there any way to override the behavior or new and malloc or do I need to replace all the calls to them one by one?

A: 

I think over-riding is definitely possible. You can just link your applciation with the new implementation library you want and all calls to new, malloc will be overriden. I have not done that. But i am guessing its possible because, when using valgrind, it uses its own memory allocators to track the memory usage statistics of an application and produces result in the end. So if it can, definitely there should be a way.

Check this link. It contains information about smartheap library which is of similar kind!

Aviator
+2  A: 

From the TCMalloc documentation:

To use TCmalloc, just link tcmalloc into your application via the "-ltcmalloc" linker flag. You can use tcmalloc in applications you didn't compile yourself, by using LD_PRELOAD:

$ LD_PRELOAD="/usr/lib/libtcmalloc.so"

ptmalloc seems to be similar (but if you're on Linux, you're likely already using it because it's part of the GNU C library).

I would expect operator new to call malloc, but you can easily check for yourself by setting a breakpoint on malloc, then calling new. If your new doesn't call malloc, you can redefine it so that it does.

Martin B
You skipped the part where they say: LD_PRELOAD is tricky, and we don't necessarily recommend this mode of usage.
rpg
That's right, I did -- because I didn't want to quote any more than the essentials. The detailed documentation is behind the link.
Martin B
+1  A: 

If your program is multi-threaded, then the Hoard allocator is highly regarded. I personally don't think it's worth the bother on Linux though, as glibc's ptmalloc is already pretty good.

alex tingle
The code i'm talking about uses too many new and malloc calls. ptmalloc actually gets invokes when the code is compiled for Linux?
O. Askari
Yes, if you are working on Linux, you are already using ptmalloc.
alex tingle
A: 

How much time does your app spend in memory allocation?

AlexEzh