How to statically link against jemalloc?


#1

It used to be that you could use #![feature(alloc_jemalloc)] to statically link a crate - even if the artifact was dynamic library - against jemalloc as the allocator. Unfortunately, rustc no longer recognizes that feature. Is there a different way to do this now?


#2

The global allocator interface has changed a bit: https://doc.rust-lang.org/nightly/unstable-book/language-features/global-allocator.html. TL;DR:

extern crate alloc_jemalloc;

use alloc_jemalloc::Jemalloc;

#[global_allocator]
static ALLOCATOR = Jemalloc;

#3

Looks like there’s unfortunately no Jemalloc in alloc_jemalloc - it looks like that crate is only implementing the old global allocator strategy: https://github.com/japaric/alloc-jemalloc/blob/master/src/lib.rs


#4

Try https://crates.io/crates/jemallocator


#5

The in-tree alloc-jemalloc implements the new interface: https://github.com/rust-lang/rust/blob/master/src/liballoc_jemalloc/lib.rs


#6

Worked, thanks!


#7

Oh, gotcha. It doesn’t implement Alloc, but rather provides its own global allocator so you just have to do extern crate alloc_jemalloc and it Just Works. Thanks!


#8

It’s in a bit of a state of transition - the magical “just link to me” behavior is the old setup. On a sufficiently new compiler you’ll need to do the tagged static thing. alloc_jemalloc has to implement both right now so things work with the bootstrap compiler.