The default allocator may sometimes fail to release memory promptly. It is recommended to use jemallocator instead.
jemallocator is a library linked with the jemalloc memory allocator, providing the Jemalloc
unit type that implements the allocator API and can be set as #[global_allocator]
.
tikv-jemallocator
is the successor to jemallocator
. These two crates are identical except for their names. For new projects, it is recommended to use the tikv-xxx
version.
The jemalloc support ecosystem consists of the following crates:
Jemalloc
type, which implements the GlobalAlloc
and Alloc
traits.mallctl*()
function family and MALLCTL NAMESPACE
).To use tikv-jemallocator, add it as a dependency:
To set tikv_jemallocator::Jemalloc
as the global allocator, add the following code to your project:
That's it! Once this static variable is defined, jemalloc will be used for all memory allocations requested by Rust code in the same program.
jemalloc is a general-purpose malloc implementation that focuses on:
It is particularly useful in the following scenarios:
jemalloc does not support the MSVC target environment and is therefore unavailable when using the MSVC toolchain on Windows. This is why the cfg(not(target_env = "msvc"))
condition is included in the example code.