TCMalloc is Google’s customized implementation of C’s
malloc() and C++’s
operator new used for memory allocation within our C and C++ code. This custom
memory allocation framework is an alternative to the one provided by the C
standard library (on Linux usually through
glibc) and C++ standard library.
TCMalloc is designed to be more efficient at scale than other implementations.
Specifically, TCMalloc provides the following benefits:
TCMalloc may operate in one of two fashions:
NOTE: the “TC” in TCMalloc refers to Thread Caching, which was originally a distinguishing feature of TCMalloc; the name remains as a legacy.
In both cases, these cache implementations allows TCMalloc to avoid requiring locks for most memory allocations and deallocations.
TCMalloc provides APIs for dynamic memory allocation:
malloc() using the C
::operator new using the C++ API. TCMalloc, like most allocation
frameworks, manages this memory better than raw memory requests (such as through
mmap()) by providing several optimizations:
The cache size can also affect performance. The larger the cache, the less any given cache will overflow or get exhausted, and therefore require a lock to get more memory. TCMalloc extensions allow you to modify this cache size, though the default behavior should be preferred in most cases. For more information, consult the TCMalloc Tuning Guide.
Additionally, TCMalloc exposes telemetry about the state of the application’s
MallocExtension. This can be used for gathering profiles of the live
heap, as well as a snapshot taken near the heap’s highwater mark size (a peak
TCMalloc implements the C and C++ dynamic memory API endpoints from the C11, C++11, C++14, and C++17 standards.
From C++, this includes
::operator delete, and array variant functions.
Unlike in the standard implementations, TCMalloc does not throw an exception
when allocations fail, but instead crashes directly. Such behavior can be used
as a performance optimization for move constructors not currently marked
noexcept; such move operations can be allowed to fail directly due to
allocation failures. In Abseil, these
are enabled with
From C, this includes
The TCMalloc API obeys the behavior of C90 DR075 and DR445 which states:
The alignment requirement still applies even if the size is too small for any object requiring the given alignment.
In other words,
Based on the progress of
N2293, we may relax
this alignment in the future.
For more complete information, consult the TCMalloc Reference.