tikv-jemallocator
Drop-in jemalloc global allocator for Rust programs that need lower fragmentation and faster multi-threaded allocation
Repository Health
Technical Analysis
tikv-jemallocator provides a Jemalloc unit type that implements Rust’s GlobalAlloc trait, letting any binary swap its default system allocator for jemalloc with a single #[global_allocator] static. It is the successor to the original jemallocator crate, maintained by the TiKV project and used in production by TiKV, Redis-compatible services, and other latency-sensitive Rust systems that need predictable allocation behavior under heavy concurrent load.
The crate is part of a small family (tikv-jemalloc-sys for raw bindings, tikv-jemallocator for the global allocator, tikv-jemalloc-ctl for runtime introspection) that together expose jemalloc’s battle-tested allocation strategy to Rust with minimal ceremony — no custom allocator code to write, just link and go.
What You Get
- A
Jemalloczero-sized type implementingGlobalAllocand (optionally) the unstableAlloctrait - Static linking against a vendored, version-pinned jemalloc build via
tikv-jemalloc-sys - Cargo feature flags for profiling, stats collection, background threads, and debug builds
- Cross-platform support across Linux (x86_64, aarch64, powerpc64le) and macOS (including Apple Silicon)
- Companion
tikv-jemalloc-ctlcrate for reading live allocator statistics once installed
Common Use Cases
- Replacing the default allocator in long-running server binaries (databases, proxies, caches) to reduce memory fragmentation over time
- Reducing lock contention and improving throughput for heavily multi-threaded allocation workloads
- Enabling jemalloc’s heap profiling (
profilingfeature) to debug memory growth in production Rust services - Matching the allocator used by the TiKV/TiDB ecosystem when building compatible or adjacent tooling
Under The Hood
Architecture The crate is a thin Rust wrapper: jemallocator/src/lib.rs defines a zero-sized Jemalloc struct that forwards alloc/dealloc/realloc calls to the C mallctl/je_malloc family exposed by the sibling tikv-jemalloc-sys crate, which vendors and builds jemalloc’s C source via a build.rs script; a companion jemallocator-global crate offers a convenience macro, and jemalloc-ctl layers a typed, namespaced Rust API over jemalloc’s mallctl introspection interface for stats and tuning at runtime.
Tech Stack Pure Rust workspace (5 crates: jemallocator, jemallocator-global, jemalloc-ctl, jemalloc-sys, test-dylib) with a small amount of C (0.46%) and shell (1.77%) for the vendored jemalloc build; no external Rust dependencies beyond libc and small build helpers, keeping the dependency surface minimal for a systems-level crate.
Code Quality Test coverage relies on jemalloc’s own C test suite plus Rust integration tests in test-dylib and per-crate tests/ directories; CI (ci/ scripts, GitHub Actions) exercises multiple target platforms and Rust channels (stable/beta/nightly), which is appropriate rigor for an allocator where platform-specific memory-safety bugs are the primary risk.
API Design The public API is deliberately minimal — a single static Jemalloc value to declare as #[global_allocator] — which is about as low-friction as an FFI-backed allocator crate can be; feature flags (profiling, stats, debug) are documented in the README and Cargo.toml, though users needing per-arena tuning must drop down to tikv-jemalloc-ctl and jemalloc’s raw mallctl naming, which carries a steeper learning curve than pure-Rust libraries.
Used by 3 apps in this directory
InfluxDB
Databases · Analytics
Open-source time-series database built for real-time ingest, fast SQL queries, and embedded Python automation — powered by Apache Arrow and Parquet.
Laminar
AI Development · Monitoring
Open-source observability platform purpose-built for AI agents — trace, evaluate, debug, and monitor at scale with SQL access and real-time replay.
OpenObserve
Monitoring · Analytics · Devops
Open source observability platform for logs, metrics, traces, and real user monitoring — delivering 140x lower storage costs than Elasticsearch with a single binary you can run in under 2 minutes.