Quick Guide: Fixing Varnish Memory Fragmentation with jemalloc
0. Check Current Memory Allocator (Optional)
Check whether varnishd is already linked against jemalloc (if there is no output, it is using glibc):
1. Install jemalloc
Enable the EPEL repository (if not already enabled) and install jemalloc (for RHEL / CentOS / Rocky / AlmaLinux 8 & 9):
(Note: For Ubuntu/Debian systems, use: apt update && apt install -y libjemalloc2)
Confirm the exact path of the shared library:
(Usually /usr/lib64/libjemalloc.so.2 or .so.1 on RHEL-based systems; /usr/lib/x86_64-linux-gnu/libjemalloc.so.2 on Debian/Ubuntu.)
2. Configure systemd Environment Variables
Create a systemd drop-in override file so that your changes persist across package updates:
Add the following configuration (replace the path with the actual library path from Step 1):
Optional Tuning: If you want jemalloc to purge and return idle memory pages to the OS more aggressively, you can also add MALLOC_CONF (the default configuration is sufficient for most workloads):
3. Restart Varnish and Verify
Reload the systemd configuration and restart the Varnish service:
Verify that jemalloc has been loaded into the cache-main worker process (any matching output confirms it is active):
Monitor the actual physical memory (RSS) usage of Varnish processes:
4. Additional Recommendations
Overview: On RHEL, CentOS, Rocky Linux, and AlmaLinux systems, the default Varnish package is typically compiled without jemalloc and falls back to glibc malloc. Under multi-threaded workloads with frequent LRU cache evictions, glibc tends to retain freed memory inside per-thread arenas rather than returning it to the OS, causing the cache-main process RSS to grow far beyond the configured -s malloc limit. Preloading jemalloc via LD_PRELOAD quickly resolves this fragmentation.
0. Check Current Memory Allocator (Optional)
Check whether varnishd is already linked against jemalloc (if there is no output, it is using glibc):
Code:
ldd $(which varnishd) | grep jemalloc
1. Install jemalloc
Enable the EPEL repository (if not already enabled) and install jemalloc (for RHEL / CentOS / Rocky / AlmaLinux 8 & 9):
Code:
yum install -y epel-release
yum install -y jemalloc
Confirm the exact path of the shared library:
Code:
ls -l /usr/lib64/libjemalloc.so*
2. Configure systemd Environment Variables
Create a systemd drop-in override file so that your changes persist across package updates:
Code:
systemctl edit varnish
Add the following configuration (replace the path with the actual library path from Step 1):
Code:
[Service]
Environment="LD_PRELOAD=/usr/lib64/libjemalloc.so.2"
Optional Tuning: If you want jemalloc to purge and return idle memory pages to the OS more aggressively, you can also add MALLOC_CONF (the default configuration is sufficient for most workloads):
Code:
[Service]
Environment="LD_PRELOAD=/usr/lib64/libjemalloc.so.2"
Environment="MALLOC_CONF=dirty_decay_ms:1000,muzzy_decay_ms:1000"
3. Restart Varnish and Verify
Reload the systemd configuration and restart the Varnish service:
Code:
systemctl daemon-reload
systemctl restart varnish
Verify that jemalloc has been loaded into the cache-main worker process (any matching output confirms it is active):
Code:
grep jemalloc /proc/$(pgrep cache-main)/maps
Monitor the actual physical memory (RSS) usage of Varnish processes:
Code:
ps -C varnishd,cache-main -o pid,ppid,user,comm,vsz,rss,pmem
4. Additional Recommendations
- Cap Transient Storage: Add -s Transient=malloc,512M (adjust size as needed) to your Varnish startup arguments to prevent uncacheable or short-lived objects from consuming unbounded memory.
- Fallback Option (Without jemalloc): In restricted environments where jemalloc cannot be installed, you can reduce glibc arena bloat by adding Environment="MALLOC_ARENA_MAX=4" via systemctl edit varnish, though jemalloc remains the most effective solution.