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
80
votes
2 answers

What are the differences between (and reasons to choose) tcmalloc/jemalloc and memory pools?

tcmalloc/jemalloc are improved memory allocators, and memory pool is also introduced for better memory allocation. So what are the differences between them and how to choose them in my application?
Mickey Shine
  • 12,187
  • 25
  • 96
  • 148
70
votes
8 answers

Will malloc implementations return free-ed memory back to the system?

I have a long-living application with frequent memory allocation-deallocation. Will any malloc implementation return freed memory back to the system? What is, in this respect, the behavior of: ptmalloc 1, 2 (glibc default) or 3 dlmalloc tcmalloc…
osgx
  • 90,338
  • 53
  • 357
  • 513
31
votes
6 answers

C++ memory allocation mechanism performance comparison (tcmalloc vs. jemalloc)

I have an application which allocates lots of memory and I am considering using a better memory allocation mechanism than malloc. My main options are: jemalloc and tcmalloc. Is there any benefits in using any of them over the other? There is a good…
Shayan Pooya
  • 1,049
  • 1
  • 13
  • 22
20
votes
5 answers

How To Use TCMalloc?

Firstly, I want to know how to install TCmalloc in Ubuntu. Then I need a program uses TCmalloc. Then I need a small program to show that TCmalloc is working better than PTmalloc.
sudo
  • 548
  • 1
  • 4
  • 16
12
votes
2 answers

How to trace "tcmalloc : large alloc .... "

my app print several line like: tcmalloc: large alloc 4294488064 bytes == 0x2b968d8000 @ 0x727432 0x727302 0x727a58 0x75a07d 0x574beb 0x585756 0x5575df 0x5717db 0x57108f 0x58078c 0x302b80610a tcmalloc: large alloc 4294488064 bytes ==…
Shawn
  • 1,441
  • 4
  • 22
  • 36
10
votes
1 answer

Using tcmalloc/jemalloc with custom memory pool manager

I would like to use a high performance general purpose allocator like jemalloc/tcmalloc with a memory pool. Is there a guide for doing this? I don't want to use jemalloc/tcmalloc as a drop-in replacement for malloc. I have memory pool that uses…
John Knight
  • 109
  • 1
  • 3
9
votes
2 answers

Is there a custom memory allocator design pattern that does not store metadata in its allocations?

Basically, I need a memory pool for fast allocation of small objects. Ideally, I'd like to replace allocations on both the host, and for memory allocated on GPUs with cudaMalloc. I can write my own, and I will if I have to, but I wouldn't mind…
user2333829
  • 1,301
  • 1
  • 15
  • 25
9
votes
2 answers

tcmalloc: how can I get my malloc calls overridden when compiling statically?

When I use LD_PRELOAD=/usr/local/lib/libtcmalloc.so, all my calls to malloc become tcmalloc calls. However, when I link statically against libtcmalloc, I find that straight malloc is getting called unless I still use the LD_PRELOAD setting. So how…
kdt
  • 27,905
  • 33
  • 92
  • 139
8
votes
2 answers

Linking with libtcmalloc ubuntu

I had installed the package libtcmalloc-minimal0 but when I try to compile my program with flag -ltcmalloc-minimal0 I am getting error /usr/bin/ld: cannot find -ltcmalloc_minimal0 I had checked /usr/lib and the library is there More Info dpkg…
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
7
votes
3 answers

Using tcmalloc in my C++ project

I'm linking my C++ program to tcmalloc with -ltcmalloc_minimal in linux and i have install the ltcmalloc lib with apt-get install libgoogle-perftools-dev. Do i need to add any include file to my project source files to enable tcmalloc in my project?…
user470739
5
votes
1 answer

tcmalloc: large alloc python in Google Colab

I was trying to apply a deep learning algorithm(CNN) in python but after separating training-testing data and transforming time series to image step my Colab Notebook crashed and restarted itself again. It gives an error like "Your session crashed…
5
votes
2 answers

Has anyone been able to integrate tcmalloc on a Windows 64 bit application that uses shared DLL's?

I have a 64 bit Visual Studio 2010 (single threaded) C++ Windows application that I am trying to integrate tcmalloc with and I am running into problems when using any of our dynamically linked dll's . I linked tcmalloc as a static library. tcmalloc…
Bruce
  • 81
  • 1
  • 6
4
votes
1 answer

ptmalloc, tcmalloc, dmalloc, HOARD, or nedmalloc? (on iOS and Android game)

I have a game that is a bit of a memory hog on the iPad from many small allocations. I have a custom game engine written in C++. Has anyone here successfully compiled one of these other allocators on iOS? Were there any gotchas? I'll let people know…
pTymN
  • 41
  • 3
4
votes
1 answer

Tensorflow first epoch is extremely slow (maybe related to pool_allocator)

I am training a model built with TF. At the first epoch, TF is slower than the next epochs by a factor of *100 and I am seeing messages like: I tensorflow/core/common_runtime/gpu/pool_allocator.cc:259] Raising pool_size_limit_ from 958 to 1053 As…
Yuval Atzmon
  • 5,645
  • 3
  • 41
  • 74
4
votes
1 answer

using tcmalloc with glib

I want to test the performance improvement that I could get by using Google's tcmalloc. My program is built using quite a lot of the utilities provided by glib (hashes, lists, arrays, ...). So what I want is basically to make glib to use tcmalloc…
Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56
1
2 3 4 5 6 7