Trouble incorporating jemalloc

Hello everyone,

Pardon me if this is something basic; I don't have much of a C background and seem to have gotten stuck in getting Rust to use jemalloc.

I'm seeing my Rust program crash when run in a Docker container after hitting the memory limit. Running it through heaptrack didn't show any leaks; so I wanted to explore jemalloc to see if this was due to fragmentation and get more information on what is happening.

I followed the tikv-jemallocator readme and added the dependency to Cargo.toml and set the global allocator in

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = "0.5"

At the beginning of

#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
static GLOBAL: Jemalloc = Jemalloc;

It built successfully, but ldd didn't show the dependency

 $ ldd heapdemo (0x00007ffc3c17d000) => /lib/x86_64-linux-gnu/ (0x00007f8352cb6000) => /lib/x86_64-linux-gnu/ (0x00007f8352c93000) => /lib/x86_64-linux-gnu/ (0x00007f8352b44000) => /lib/x86_64-linux-gnu/ (0x00007f8352b3e000) => /lib/x86_64-linux-gnu/ (0x00007f835294c000)
	/lib64/ (0x00007f835375f000)

I guess it may be statically linked, and proceeded to set the MALLOC_CONF


Running the program didn't produce any extra output from jemalloc, so I'm not really sure whether jemalloc is actually being used or not.

So I tried switching to dynamically linking it so that I could see the dependency in ldd:

$ cat .cargo/config.toml 
rustflags = ["-C", "link-args=-l jemalloc"]

But this made no difference; the ldd output remained same and the program didn't produce any jemalloc output.

Wondering what I might be doing wrong here? Is there any other implied step that I should have done? I tried installing libjemalloc-dev apt install libjemalloc-dev and that too didn't affect anything.

Thank you

Got it,

I just had to set this environment variable before running my program :slight_smile:

export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/

tikv-jemallocator statically links jemalloc. I think you have to use _RJEM_MALLOC_CONF instead of MALLOC_CONF when you use tikv-jemallocator because it sets a prefix by default to prevent conflicts. You can also enable the unprefixed_malloc_on_supported_platforms cargo feature to remove the prefix on all targets except Android, DragonflyBSD, Linux with MUSL libc and macOS.

1 Like