How to Compile FFmpeg with Jemalloc or Tcmalloc

This article provides a practical, step-by-step guide on how to compile FFmpeg using custom memory allocators like jemalloc or tcmalloc instead of the standard glibc allocator. By integrating these high-performance allocators, you can significantly reduce memory fragmentation and improve execution speed in multi-threaded video encoding and decoding environments.

Why Use Custom Allocators with FFmpeg?

FFmpeg is highly multi-threaded, especially when handling multiple concurrent video streams or complex filtergraphs. The default system allocator (ptmalloc in glibc) can suffer from memory fragmentation and lock contention under high concurrency.


Step 1: Install the Custom Allocator

Before configuring FFmpeg, you must install the development headers and libraries for your chosen allocator.

On Ubuntu/Debian:

For jemalloc:

sudo apt-get update
sudo apt-get install libjemalloc-dev

For tcmalloc:

sudo apt-get update
sudo apt-get install libgoogle-perftools-dev

On CentOS/RHEL/Rocky Linux:

For jemalloc:

sudo dnf install epel-release
sudo dnf install jemalloc-devel

For tcmalloc:

sudo dnf install epel-release
sudo dnf install gperftools-devel

Step 2: Configure FFmpeg

Clone the FFmpeg source repository and navigate to the directory:

git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg

To compile FFmpeg with the custom allocator, you must pass the appropriate compiler and linker flags to the ./configure script using the --extra-libs option. This ensures the allocator is statically or dynamically linked directly into the binary.

Option A: Configuring with Jemalloc

./configure \
  --enable-gpl \
  --enable-nonfree \
  --extra-libs="-ljemalloc"

Option B: Configuring with Tcmalloc

./configure \
  --enable-gpl \
  --enable-nonfree \
  --extra-libs="-ltcmalloc_minimal"

(Note: Using tcmalloc_minimal is recommended for FFmpeg because it contains the memory allocation routines without the overhead of the CPU and heap profilers).


Step 3: Compile and Install

Once the configuration process completes successfully, compile the binaries using all available CPU cores:

make -j$(nproc)
sudo make install

Step 4: Verify the Build

To confirm that FFmpeg successfully linked against your custom allocator, use the ldd command on the compiled binary:

ldd $(which ffmpeg)

Look for the corresponding library in the output: