Skip to content

Instantly share code, notes, and snippets.

@daverigby
Last active June 12, 2024 09:19
Show Gist options
  • Save daverigby/27de931a4b59cc7df588ac22646d6fe2 to your computer and use it in GitHub Desktop.
Save daverigby/27de931a4b59cc7df588ac22646d6fe2 to your computer and use it in GitHub Desktop.
Memory profiling using jemalloc

Based on information from: https://github.com/jemalloc/jemalloc/wiki/Use-Case%3A-Heap-Profiling

Build jemalloc

RedHat & related flavours don't enable profiling in their binaries, so we need to build from source:

wget https://github.com/jemalloc/jemalloc/releases/download/5.3.0/jemalloc-5.3.0.tar.bz2
tar xjf jemalloc-5.3.0.tar.bz2
cd jemalloc-5.3.0
./configure --enable-stats --enable-prof
make -j16
sudo make install

Check install location - on RedHat is /usr/local/lib/libjemalloc.so.2

Setup

If not explicitly linking to jemalloc, it needs to be preloaded:

  • RedHat-based Linux:
export LD_PRELOAD=/usr/local/lib/libjemalloc.so.2

Dump stats (not full profile) at exit

MALLOC_CONF=stats_print:true <my_program>

Profile based on max VSZ

Dump a profile every time a Virtual Memory reaches a new maximum:

MALLOC_CONF="prof:true,prof_prefix:jeprof.out,prof_gdump:true" <my_program>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment