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 main.rs.

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

At the beginning of main.rs:

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

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

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

 $ ldd heapdemo
	linux-vdso.so.1 (0x00007ffc3c17d000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f8352cb6000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8352c93000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8352b44000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8352b3e000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f835294c000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f835375f000)

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

$ echo $MALLOC_CONF
prof:true,prof_active:true,prof_final:true,prof_leak:true,lg_prof_interval:30,lg_prof_sample:0

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/libjemalloc.so

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

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.