Build and Install jemalloc on UNIX

Follow this guide to Build and Install jemalloc on UNIX systems like Ubuntu, RHEL, OpenSUSE and others. For this, we will get the source code of jemalloc, build and install it and load required environment variables.

We need to follow the following steps:

  • Get jemalloc source code
  • Build and install jemalloc
  • Load environment variables

In short, the steps are:

git clone https://github.com/jemalloc/jemalloc.git
cd jemalloc
./configure
make
make install
export LD_PRELOAD="/usr/lib64/libjemalloc.so.4"

Get jemalloc source code

You can clone the source of jemalloc from the official GitHub repository:

git clone https://github.com/jemalloc/jemalloc.git

If you want to build a specific version of jemalloc, we can checkout to a previous commit (like stable-3):

cd jemalloc
git checkout stable-3

Build and install jemalloc

Once we go into the jemalloc code base, we can build the library using the following command:

./configure
make
make install

If you do not have bazel, you can follow this guide to build and install bazel.

This will build shared library of jemalloc and generate the files libjemalloc.so.4

Load environment variables

We need to set the environment variable LD_PRELOAD to point to the SO file of jemalloc. Usually, the jemalloc library may be installed in /usr/lib64.

export LD_PRELOAD="/usr/lib64/libjemalloc.so.4"

Once set, all critical calls like malloc will go to tcmalloc. You need no extra step to use jemalloc in your code. Build, install and enjoy using jemalloc.