redis4的自动内存整理

  • 2019 年 10 月 4 日
  • 筆記

redis4里面自带了内存整理,可以避免内存碎片率过大的问题。

使用命令: 

config set activedefrag yes

执行完上面的命令后,稍等片刻再查看碎片率:

redis-cli -p 6379 info | grep ratio

我们在线上遇到的问题:

127.0.0.1:7001> config set activedefrag yes

(error) ERR Active defragmentation cannot be enabled: it requires a Redis server compiled with a modified Jemalloc like the one shipped by default with the Redis source distribution

看这个报错提示,貌似是jemalloc的版本与redis需要的不一致

查看下报错机器的jemalloc的版本:

redis-cli -c -p 7000 | grep jemalloc 

mem_allocator:jemalloc-3.6.0

我们的机器的redis-server的版本是4.0.10的,和jemalloc的版本差异较大。

后续: 

    我们换一台全新编译的redis-server机器, 再次执行 config set activedefrag yes  发现不再报错了。 

示例:

127.0.0.1:7001> info memory  # Memory  used_memory:5505886616  used_memory_human:5.13G  used_memory_rss:8171044864  used_memory_rss_human:7.61G  used_memory_peak:7923672456  used_memory_peak_human:7.38G  used_memory_peak_perc:69.49%  used_memory_overhead:99658336  used_memory_startup:1534088  used_memory_dataset:5406228280  used_memory_dataset_perc:98.22%  total_system_memory:135076163584  total_system_memory_human:125.80G  used_memory_lua:37888  used_memory_lua_human:37.00K  maxmemory:10737418240  maxmemory_human:10.00G  maxmemory_policy:volatile-lfu  mem_fragmentation_ratio:1.48  mem_allocator:jemalloc-4.0.3  active_defrag_running:0  lazyfree_pending_objects:0      127.0.0.1:7001> config set activedefrag yes      稍等一会,再看下结果,碎片率恢复了:  127.0.0.1:7001> info memory  # Memory  used_memory:5504344840  used_memory_human:5.13G  used_memory_rss:6649913344  used_memory_rss_human:6.19G  used_memory_peak:7923672456  used_memory_peak_human:7.38G  used_memory_peak_perc:69.47%  used_memory_overhead:99362374  used_memory_startup:1534088  used_memory_dataset:5404982466  used_memory_dataset_perc:98.22%  total_system_memory:135076163584  total_system_memory_human:125.80G  used_memory_lua:37888  used_memory_lua_human:37.00K  maxmemory:10737418240  maxmemory_human:10.00G  maxmemory_policy:volatile-lfu  mem_fragmentation_ratio:1.21  mem_allocator:jemalloc-4.0.3  active_defrag_running:0  lazyfree_pending_objects:0      可以看到  mem_fragmentation_ratio 从1.48 降低到了1.21了

redis4 碎片整理相关的配置说明:

# Enabled active defragmentation  # 碎片整理总开关  # activedefrag yes    # Minimum amount of fragmentation waste to start active defrag  # 内存碎片达到多少的时候开启整理  active-defrag-ignore-bytes 100mb    # Minimum percentage of fragmentation to start active defrag  # 碎片率达到百分之多少开启整理  active-defrag-threshold-lower 30    # Maximum percentage of fragmentation at which we use maximum effort  # 碎片率小余多少百分比开启整理  active-defrag-threshold-upper 100    # Minimal effort for defrag in CPU percentage  active-defrag-cycle-min 25    # Maximal effort for defrag in CPU percentage  active-defrag-cycle-max 75