tikv-jemallocator

Drop-in jemalloc global allocator for Rust programs that need lower fragmentation and faster multi-threaded allocation

Library
Cargo
v0.7.0
536stars
MIT/Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
56/100Fair
Development Activity32
Maintenance32
Community72
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
68/100Good
Architecture75
Code Quality70
Innovation65
Learning Curve60

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 Jemalloc zero-sized type implementing GlobalAlloc and (optionally) the unstable Alloc trait
  • 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-ctl crate 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 (profiling feature) 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search