分析内存使用和碎片,启用主动碎片整理
Redis内存命令
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 PURGE作用
显示 Redis 为数据分配的内存(used_memory)与操作系统看到的 RSS 之间的差异、碎片率和内存明细;必要时在运行时启用主动碎片整理(active defrag)。
适用场景
RSS 明显大于数据集、批量删除/过期后内存不下降,或出现 OOM 风险时。
注意
在小数据集上 mem_fragmentation_ratio 具有误导性(如 2 MB 数据显示 4.0);绝对值请看 mem_fragmentation_bytes。比值低于 1 表示发生了 swap 而非碎片,会造成延迟。主动碎片整理消耗 CPU;用 active-defrag-cycle-max 加以限制。需要 CONFIG REWRITE 或 redis.conf 才能持久化。