Questions tagged [tcmalloc]

TCMalloc is a malloc library developed by Google. It is faster than the glibc 2.3 malloc (ptmalloc2), which takes approximately 300ns to execute a malloc/free pair on a 2.8GHz P4 (for small objects). TCMalloc takes approximately 50ns for the same operation pair. It also reduces lock contention for multi-threaded programs. For small objects, there is virtually zero contention. Another benefit is space-efficient representation of small objects.

Introduction

TCMalloc (Thread-Caching malloc) is a (memory allocation) library developed by Google. It is part of the gperftools (Google Performance Tools) project. Other tools in the same project include a heap checker (detecting memory leaks), a heap profiler (getting statistics for memory usage) and a CPU profiler (getting statistics for CPU usage).

Official Introduction by Sanjay Ghemawat

TCMalloc is faster than the glibc 2.3 malloc (available as a separate library called ptmalloc2) and other mallocs that I have tested. ptmalloc2 takes approximately 300 nanoseconds to execute a malloc/free pair on a 2.8 GHz P4 (for small objects). The TCMalloc implementation takes approximately 50 nanoseconds for the same operation pair. Speed is important for a malloc implementation because if malloc is not fast enough, application writers are inclined to write their own custom free lists on top of malloc. This can lead to extra complexity, and more memory usage unless the application writer is very careful to appropriately size the free lists and scavenge idle objects out of the free list.

TCMalloc also reduces lock contention for multi-threaded programs. For small objects, there is virtually zero contention. For large objects, TCMalloc tries to use fine grained and efficient spinlocks. ptmalloc2 also reduces lock contention by using per-thread arenas but there is a big problem with ptmalloc2's use of per-thread arenas. In ptmalloc2 memory can never move from one arena to another. This can lead to huge amounts of wasted space. For example, in one Google application, the first phase would allocate approximately 300MB of memory for its URL canonicalization data structures. When the first phase finished, a second phase would be started in the same address space. If this second phase was assigned a different arena than the one used by the first phase, this phase would not reuse any of the memory left after the first phase and would add another 300MB to the address space. Similar memory blowup problems were also noticed in other applications.

Another benefit of TCMalloc is space-efficient representation of small objects. For example, N 8-byte objects can be allocated while using space approximately 8N * 1.01 bytes. I.e., a one-percent space overhead. ptmalloc2 uses a four-byte header for each object and (I think) rounds up the size to a multiple of 8 bytes and ends up using 16N bytes.

Links

Related Tags

98 questions
1
vote
1 answer

gperftools/tcmalloc 2.8 - running out of memory

We are migrating from gperftools/tcmalloc 2.0 to 2.8. But some of our test cases fail with "out of memory" error. We use Cent OS 6.5. This happens with a posix_memalign() call requesting for 32536008 bytes with 64bit alignment. I didn't use any…
Pramod T
  • 21
  • 3
1
vote
1 answer

Writing an efficient backtrace function

I came across below code for walking backtrace struct stack_frame { struct stack_frame *prev; void *return_addr; } __attribute__((packed)); typedef struct stack_frame stack_frame; __attribute__((noinline, noclone)) void backtrace_from_fp(void…
BalajiKK
  • 11
  • 1
  • 1
1
vote
0 answers

Obtain how much memory is available (roughly) before malloc

Can I obtain how much memory is available (roughly) before a malloc()? I need to adjust some internal variables before doing a malloc()/new because performing a malloc() and then checking and deleting is not feasible. (FYI, I am asking about glibc…
walkerlala
  • 1,599
  • 1
  • 19
  • 32
1
vote
1 answer

Use gperftools' heap profiler to profile libc malloc

I am trying to profile an application to compare its performance using different allocation strategies. I have no trouble profiling it when using tcmalloc but how to profile it using libc's allocation functions ? Indeed, enabling gperftool's heap…
EnzoMolion
  • 949
  • 8
  • 25
1
vote
0 answers

TCMalloc memory leak debugging

I've compiled an application with tcmalloc and used HEAPPROFILE environment variable to get heap files every 10MB or so a new heap file is created and according to the tcmalloc page i can use the pprof tool to compare heap files, and see what are…
user3696279
  • 99
  • 1
  • 5
1
vote
1 answer

why my c++ program uses significant memory when doing tcmalloc heap-checker or heap-profile

CentOS Linux release 7.3.1611 gcc version 4.8.5 20150623 gperftool 2.4-8.el7 1.my c++ program which links -ltcmalloc works fine without HEAPCHECKER or HEAPPROFILE. The memory it used stabilizes at 5M~10M. 2.if I run the program using heap-checker…
lag
  • 19
  • 3
1
vote
1 answer

configure keeps finding tcmalloc. How?

I'm building NWChem on Cray. libtcmalloc_minimal is already added to an archive file by the cc in my Cray environment. In my configure routine, it explicitly appends a second -ltcmalloc_minimal resulting in a multiple definition and a configure…
Levi Barnes
  • 357
  • 3
  • 12
1
vote
0 answers

LNK2005 error when trying to link with tcmalloc

I've trying to compile a project with tcmalloc in Visual Studio 2013. I've compiled .lib file for libtcmalloc from gperftools package and added libtcmalloc.lib into linker input options. When I'm trying to build my project, I get the following…
Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
1
vote
1 answer

segfault on tcmalloc ReleaseToCentralCache

We met with segfault dump often when the traffic comes in very high. Not sure what's the problem here now? Anyone has the experience to share with us? (gdb) bt #0 ... in tcmalloc::ThreadCache::ReleaseToCentralCache(tcmalloc::ThreadCache::FreeList*,…
wenxzhen
  • 75
  • 1
  • 2
  • 12
1
vote
0 answers

tcmalloc: allocation failed -- what is npages?

I've recently run across the error message central_freelist.cc:322 tcmalloc: allocation failed 16384 Looking at the source code, I see that that tcmalloc is reporting "npages". How should I interpret this number? What information does it provide…
Spacemoose
  • 3,856
  • 1
  • 27
  • 48
1
vote
3 answers

tcmalloc not working on ubuntu using -ltcmalloc_minimal

I installed tcmalloc on Ubuntu 14.0 using apt-get install libtcmalloc-minimal4 I did following steps: ln -s libtcmalloc_minimal.so.4.1.2 libtcmalloc_minimal.so linked my executable with -ltcmalloc_minimal After running the code, I can not see any…
komal dedhia
  • 217
  • 1
  • 3
  • 8
1
vote
1 answer

Linking with TCMalloc but the CRT malloc always called

I would like to experiment a bit with TCMalloc on Windows. I have built the VisualStudio solution which is part of the gperftools package I downloaded. But when I run any of the test apps which also came with the download, say…
user1752563
  • 149
  • 9
1
vote
1 answer

Why tcmalloc don't print function name, which provided via dlopen

I have next some project: main.cpp #include #include #include int main() { void* handle = dlopen("./shared_libs/libshared.so", RTLD_LAZY); if (NULL == handle) { std::cerr << "Cannot open library: "…
1
vote
0 answers

Is nginx compilation with jemalloc or tcmalloc memory management more powerful

I'm trying to compile myself NGINX and see that it uses is own memory allocation on top of malloc: ngx_alloc and ngx_calloc. Is it a good idea to replace ngx_alloc and ngx_calloc with jemalloc or tcmalloc memory management ?
1
vote
2 answers

Virtual Memory Statistics per process

I am working on a very wierd Memory leak issue and this resulted into the following problem. I have a process running on my system which increases its Virtual Memory size after a certain operation is made.Now in order to confirm the issue is not a…