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.
- Jemalloc (developed by FreeBSD and used by Facebook) excels at reducing fragmentation and scaling across multiple CPU cores.
- Tcmalloc (Thread-Caching Malloc, developed by Google) is optimized for high-concurrency workloads with low overhead per thread.
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-devFor tcmalloc:
sudo apt-get update
sudo apt-get install libgoogle-perftools-devOn CentOS/RHEL/Rocky Linux:
For jemalloc:
sudo dnf install epel-release
sudo dnf install jemalloc-develFor tcmalloc:
sudo dnf install epel-release
sudo dnf install gperftools-develStep 2: Configure FFmpeg
Clone the FFmpeg source repository and navigate to the directory:
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpegTo 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 installStep 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:
- For jemalloc, you should see:
libjemalloc.so => /lib/x86_64-linux-gnu/libjemalloc.so - For tcmalloc, you should see:
libtcmalloc_minimal.so => /lib/x86_64-linux-gnu/libtcmalloc_minimal.so