Difficulty: Advanced
What does `mem_fragmentation_ratio` in `INFO memory` mean, how do you interpret high and low values, and how do you fix fragmentation?
mem_fragmentation_ratio is used_memory_rss / used_memory: the physical memory the operating system has given the Redis process divided by the memory Redis believes it has allocated for data. Around 1.0 to 1.5 is normal. A value well above 1.5 means a lot of memory is wasted in fragmented allocator pages, and a value below 1.0 usually means part of Redis has been swapped to disk, which is far worse.
Redis allocates through jemalloc, which groups allocations into size classes inside larger pages. When you delete or shrink many keys, the freed slots are scattered across pages. jemalloc can reuse them for new allocations of similar size, but it cannot hand a page back to the OS while even one live allocation remains in it. So:
used_memory drops after deletes,used_memory_rss stays high,Typical triggers: a large cache that evicted or expired many keys, workloads where value sizes change over time (small sessions replaced by large JSON blobs), and big batch deletions after a peak.
| Ratio | Meaning | Action |
|---|---|---|
| < 1.0 | RSS smaller than allocated: memory is swapped out | Urgent: add RAM, reduce data, disable or reduce swap |
| 1.0 to 1.5 | Healthy, normal allocator overhead | Nothing |
| > 1.5 | Significant fragmentation | Enable active defrag or plan a restart/failover |
Two caveats:
mem_fragmentation_bytes too; a ratio of 3 on 5 MB of data is irrelevant.INFO memory output breaks it down into allocator_frag_ratio (fragmentation inside jemalloc, which active defrag can fix) and allocator_rss_ratio / rss_overhead_ratio (pages not yet returned or non-allocator memory). MEMORY DOCTOR gives a human-readable diagnosis.CONFIG SET activedefrag yes. Redis moves live values into fresh allocations in small background steps. Tune with active-defrag-ignore-bytes (default 100mb), active-defrag-threshold-lower (default 10 percent), active-defrag-threshold-upper and the CPU bounds active-defrag-cycle-min / active-defrag-cycle-max.MEMORY PURGE asks jemalloc to release dirty pages it is holding; it helps with page retention, not true fragmentation.maxmemory is compared against used_memory, not RSS. With a ratio of 1.6, a maxmemory 10gb instance can use about 16 GB of real RAM and get killed by the OOM killer on a 16 GB VM. Leave headroom for fragmentation and for copy-on-write during RDB/AOF rewrites.
redis-cli INFO memory | grep -E 'used_memory:|used_memory_rss:|mem_fragmentation|allocator_frag'
redis-cli MEMORY DOCTOR
# Turn on active defragmentation at runtime
redis-cli CONFIG SET activedefrag yes
redis-cli CONFIG SET active-defrag-ignore-bytes 200mb
redis-cli CONFIG SET active-defrag-threshold-lower 15
# Watch progress
redis-cli INFO stats | grep active_defrag
Here roughly 4.5 GB is wasted. Active defrag starts only once both the ignore-bytes and lower-threshold conditions are met, and it uses bounded CPU.
mem_fragmentation_ratio, used_memory_rss, jemalloc, Active defragmentation, Swapping