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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With