Expand description
A drop-in global allocator wrapper around the TCMalloc allocator. TCMalloc is a general-purpose, performance-oriented allocator built by Google.
§Usage
ⓘ
use tcmalloc_better::TCMalloc;
#[global_allocator]
static GLOBAL: TCMalloc = TCMalloc;
fn main() {
TCMalloc::process_background_actions_thread();
// Rest of main
}
§Caveats
This library is intended for use on Linux (x86_64, aarch64). For applications requiring support on a broader range of platforms, consider using malloc-best-effort, which automatically selects the best allocator based on the target platform.
§Feature flags
std
(enabled by default) — enable stdextension
(enabled by default) — TCMalloc extension APInuma_aware
— Enable NUMA-aware allocation. Need to useTCMALLOC_NUMA_AWARE
environment variable at runtime. See TCMalloc commentsdeprecated_perthread
— Use deprecated per-thread caching. Newer systems with Linux kernel version >= 4.18 should not use this.legacy_locking
— Use legacy locking backend (which is not TLB-aware) See TCMalloc docsrealloc
— Use TCMalloc’s realloc instead of defaultGlobalAlloc::realloc
. Caveats: All memory deallocations will use the slower unsizedTCMallocInternalDeleteAligned
(which accesses slow page map). Enable this feature only if you frequently use realloc operations which resides in same size class or samekPageSize
-sized blocks, and only after performance measurements or benchmarking. Rust standard library uses realloc of such conditions mainly for manually shrinking operations. Reallocation for growing operation increments size twice, so it will not be the same size class anyway in such case.
§Logical Page Sizes (These features are mutually exclusive).
Larger page size leads to greater performance in expense to increased fragmentation. See TCMalloc tuning guide:
8k_pages
(enabled by default) — 8k pages32k_pages
— Large pages256k_pages
— 256k pagessmall_but_slow
— 4k pages, option for memory constrained systems
§Madvise Transparent Hugepages control (Use no more than one value)
By default, if none of these features are enabled, madvise hugepages will be enabled:
disable_madv_hugepage_always
— Disable madvise hugepagesdisable_madv_hugepage_by_var
— Disable madvise hugepages via environment variableTCMALLOC_DISABLE_MADV_HUGEPAGE
at runtime. This variable can be set to0
or1
to enable or disable madvise hugepages respectively.
Structs§
- TCMalloc
- A memory allocator that can be registered as the standard library’s default
through the
#[global_allocator]
attribute.