views:

343

answers:

4

I was wondering which is the default memory allocator in G++ 4.4.1, on Ubuntu 9.1. I am interested in comparing different C++ allocators in a multithreaded environment. And where can I get more information about the default memory allocator?

EDIT: I refer to new and delete operators. The only linking is to rt and pthread

Regards

+1  A: 

G++ will create references to operator new() in libstdc++ that comes with G++. That in turn uses the malloc() defined in the libc that is installed on your system (usually glibc).

Most replacement allocators will point one of them to their implementation, usually they just replace malloc(). For example, you can use Google's TC Malloc by simply preloading their shared library. No changes to the compiled application necessary.

Gene Vincent
A: 

The g++ new operator is indeed implemented using glibc malloc.
Memory allocation itself is then performed using the standard unix system call mmap.

You can get more information about your memory allocator using mallinfo.
See Statistics-of-Malloc and Efficiency-and-Malloc for more information.

Ugo
A: 

From what I can gather from your replies and Google, glibc actually uses ptmalloc2..? Is there a way I can force the application to use a more primitive allocator (again, I want to compare)?

Regards

Cowboy
A: 

You can use the LD_PRELOAD variable to use your own allocator.
LD_PRELOAD="/path/your_lib_exporting_malloc.so"

Running any application dynamically linked with glibc in the same terminal will actually use the malloc implementation you provided.
Many malloc implementation are freely available.
* TLSF
* jemalloc
* dlmalloc (Doug Lea’s malloc, been around forever)
* ptmalloc2 (Doug Lea’s malloc, extended to support per-thread arenas.)
* TCMalloc (Google’s malloc, claims to be 6x faster than ptmalloc2)
* nedmalloc (claims to be faster than tcmalloc)
* Hoard, also claims to be very fast
(source: Josh Haberman's blog)

Ugo