perf: Allocator has a high impact on your Rust programs

209 points Havunenreddit 50 comments

I recently faced an issue where my application was slowly but steadily running out of memory. The VM has 4 CPUS and 16GB ram available and everyday about after ~6hours (time varied) the VM gets stuck.

I initially thought I had memory leak somewhere causing the issue, but after going through everything multiple times. I read about heap fragmentation.

https://preview.redd.it/3u17di6vjnmg1.png?width=1352&format=png&auto=webp&s=7d10f802f09cf153fc6baf6d3bb79f4a5b430b6f

I had seen posts where people claim allocator has impact on your program and that default allocator is bad, but I never imagined it had such a major impact on both memory and CPU usage as well as overall responsivness of the program.

After I tested switching from rust default allocator to jemalloc, I knew immediately the problem was fixed, because the memory usage growth was expanding as expected for the workload.

Jemalloc and mi-malloc both also have profiling and monitoring APIs available.

I ended up with mi-malloc v3 as that seemed to perform better than jemalloc.

Switching allocator is one-liner:

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

This happened on Ubuntu 24.04 server OS, whereas the development was done in Arch Linux...