Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best solution to replace a new memory allocator in an existing code?

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?

like image 336
red.clover Avatar asked Oct 05 '09 12:10

red.clover


1 Answers

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.

like image 103
Martin B Avatar answered Sep 27 '22 18:09

Martin B