Follow this guide to Build and Install tcmalloc on UNIX systems like Ubuntu, RHEL, OpenSUSE and others. For this, we will get the source code of tcmalloc, build and install it and load required environment variables.
We need to follow the following steps:
- Get tcmalloc source code
- Build and install tcmalloc
- Load environment variables
In short, the steps are:
git clone https://github.com/google/tcmalloc.git cd tcmalloc bazel test //tcmalloc/... export LD_PRELOAD="/usr/lib64/libtcmalloc.so.4"
Get tcmalloc source code
You can clone the source of tcmallc from the official GitHub repository:
git clone https://github.com/google/tcmalloc.git
If you want to build a specific version of tcmalloc, we can checkout to a previous commit (like b6f38bf):
cd tcmalloc git checkout b6f38bf
Build and install tcmalloc
Once we go into the tcmalloc code base, we can build the library using the following command:
bazel test //tcmalloc/...
If you do not have bazel, you can follow this guide to build and install bazel.
This will build shared library of tcmalloc and generate the files libtcmalloc.so.4
Load environment variables
We need to set the environment variable LD_PRELOAD to point to the SO file of tcmalloc. Usually, the tcmalloc library may be installed in /usr/lib64.
Once set, all critical calls like malloc will go to tcmalloc. You need no extra step to use tcmalloc in your code. Build, install and enjoy using tcmalloc.