Analyze memory usage and fragmentation, enable active defrag
RedisMemoryCommand
INFO memory
# Fields to check: used_memory_human, used_memory_rss_human, mem_fragmentation_ratio, mem_fragmentation_bytes, allocator_frag_ratio
MEMORY STATS
# If fragmentation is high and persistent, enable active defrag (requires jemalloc):
CONFIG SET activedefrag yes
CONFIG SET active-defrag-ignore-bytes 100mb
CONFIG SET active-defrag-threshold-lower 10
# Ask the allocator to return free pages to the OS:
MEMORY PURGEWhat it does
Shows the difference between the memory Redis allocates for data (used_memory) and the RSS seen by the OS, the fragmentation ratio and the memory breakdown; enables active defrag at runtime if needed.
When to use it
When RSS is clearly larger than the dataset, memory does not drop after bulk deletes/expiry, or an OOM risk appears.
Caution
mem_fragmentation_ratio is misleading on small datasets (e.g. 4.0 on 2 MB of data); look at mem_fragmentation_bytes for the absolute value. A ratio below 1 means swapping, not fragmentation, and causes latency. Active defrag consumes CPU; cap it with active-defrag-cycle-max. CONFIG REWRITE or redis.conf is needed to make it persistent.