How to Compile FFmpeg with Jemalloc or TCMalloc

This guide explains how to compile FFmpeg using high-performance memory allocators such as jemalloc or tcmalloc instead of the standard GNU C Library (glibc) allocator. By default, FFmpeg relies on system memory allocation, which can lead to heap fragmentation and suboptimal multi-threading performance under heavy transcoding workloads. Replacing the default allocator with jemalloc or tcmalloc reduces lock contention, optimizes heap allocation across multiple threads, and lowers overall memory consumption. The steps below detail how to install dependencies, configure FFmpeg build flags, and verify the resulting binary.

Prerequisites

Before building FFmpeg, install the necessary compilation toolchain and the development libraries for your chosen allocator on a Debian or Ubuntu system.

For jemalloc:

sudo apt update
sudo apt install -y build-essential yasm nasm pkg-config git \
    libjemalloc-dev

For tcmalloc (part of Google Performance Tools):

sudo apt update
sudo apt install -y build-essential yasm nasm pkg-config git \
    libgoogle-perftools-dev

Clone the official FFmpeg repository:

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

Method 1: Building FFmpeg with jemalloc

Jemalloc emphasizes fragmentation avoidance and scalable concurrency. To link FFmpeg against jemalloc, pass -ljemalloc through the --extra-libs configure option.

  1. Configure the build:

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

    (Note: You can add additional library flags such as --enable-libx264 or --enable-libx265 depending on your codec requirements.)

  2. Compile and install:

    make -j$(nproc)
    sudo make install

Method 2: Building FFmpeg with tcmalloc

TCMalloc (Thread-Caching Malloc) provides thread-local caches for small objects to speed up allocations in highly threaded applications. You can link either the full tcmalloc library or the lightweight tcmalloc_minimal library (which omits heap checking and profiling features).

  1. Configure the build:

    ./configure \
        --prefix=/usr/local \
        --enable-gpl \
        --enable-nonfree \
        --extra-libs="-ltcmalloc_minimal"
  2. Compile and install:

    make -j$(nproc)
    sudo make install

Verifying the Allocator Linkage

To ensure that FFmpeg is properly linked to your custom allocator, inspect the dynamic library dependencies using ldd:

For jemalloc:

ldd /usr/local/bin/ffmpeg | grep jemalloc

Expected output contains: libjemalloc.so => /lib/x86_64-linux-gnu/libjemalloc.so.2

For tcmalloc:

ldd /usr/local/bin/ffmpeg | grep tcmalloc

Expected output contains: libtcmalloc_minimal.so => /lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4


Alternative: Runtime Injection via LD_PRELOAD

If recompiling FFmpeg is not feasible, you can force existing FFmpeg binaries to use an alternate allocator at runtime using the LD_PRELOAD environment variable:

# Using jemalloc
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so ffmpeg -i input.mp4 output.mkv

# Using tcmalloc
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so ffmpeg -i input.mp4 output.mkv

While runtime preloading works without recompilation, compiling FFmpeg with --extra-libs ensures the allocator is consistently loaded across production environments, system services, and automated encoding pipelines.