Databases 9 min read

Understanding Redis Memory Fragmentation and How to Manage It

This article explains why Redis may show high OS‑allocated memory despite low data usage, describes the causes of memory fragmentation, and provides practical steps—including configuration changes and automatic defragmentation settings—to diagnose and mitigate fragmentation for stable performance.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding Redis Memory Fragmentation and How to Manage It

Redis may allocate 6 GB of memory to the OS but only 4 GB is used for stored data, leading to confusion about why data cannot be saved.

Setting CONFIG SET maxmemory 100mb or adding maxmemory 100mb in redis.conf limits Redis memory usage; when the limit is reached, the configured eviction policy removes data.

Redis also deletes expired keys via two strategies: a background task that periodically removes some data, and lazy deletion.

Even after deleting 2 GB of data from a 5 GB dataset, the process RSS may still show around 5 GB because memory is not immediately returned to the OS.

Memory consumption consists of Redis's own overhead, stored objects, buffer memory (client output buffers, replication backlog, AOF buffer), and memory fragmentation.

Fragmentation occurs when the allocator (jemalloc by default) assigns memory in fixed‑size chunks, leading to unused space, and when key sizes vary with frequent updates and deletions.

To detect fragmentation, run INFO memory and check mem_fragmentation_ratio ; a ratio above 1.5 indicates serious fragmentation.

127.0.0.1:6379> info memory
# Memory
used_memory:1132832  // memory used by stored data
used_memory_human:1.08M
used_memory_rss:2977792  // RSS from OS perspective
used_memory_rss_human:2.84M
used_memory_peak:1183808
used_memory_peak_human:1.13M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:2147483648
maxmemory_human:2.00G
maxmemory_policy:noeviction
mem_fragmentation_ratio:2.79

Redis 4.0+ provides automatic defragmentation, which can be enabled with CONFIG SET activedefrag yes and tuned via active-defrag-ignore-bytes , active-defrag-threshold-lower , active-defrag-cycle-min , and active-defrag-cycle-max to balance CPU usage and performance impact.

Restarting Redis clears fragmentation but may cause data loss if persistence is disabled; otherwise RDB/AOF restore is required.

In summary, if Redis reports much higher RSS than used memory and cannot store new data, check fragmentation and consider adjusting maxmemory, eviction policies, or enabling active defragmentation.

performanceMemory ManagementRediscachingdatabasesmemory fragmentation
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.