当Redis中清理了大量的Key之后原先Redis申请的内存(used_memory_rss)将继续持有而不会释放,此时查看内存信息将会看到存在大量的内存碎片。那么,Redis的内存碎片可以清理么,该如何清理呢?

翻看了Redis的相关资料发现,Redis4版本之后开始支持内存碎片的清理,于是进行了一次测试,内容如下:

1.  搭建Redis

搭建一个Redis,版本为4.0.14.搭建步骤参考历史博文或微信公众号,步骤相对简单,没有太多幺蛾子,很快便可以搭建成功。

2.  插入一堆Key,使其内存占用很大

可以批量写一个循环,插入大量key。

3.  删除90%以上的key

循环删除key或在创建key时设置过期时间,待key删除或过期之后,可以查看内存的情况。

127.0.0.1:> info memory
# Memory
used_memory:
used_memory_human:130.69M
used_memory_rss:
used_memory_rss_human:.90G
used_memory_peak:
used_memory_peak_human:.26G
used_memory_peak_perc:1.13%
used_memory_overhead:
used_memory_startup:
used_memory_dataset:
used_memory_dataset_perc:98.03%
total_system_memory:
total_system_memory_human:.70G
used_memory_lua:
used_memory_lua_human:.00K
maxmemory:
maxmemory_human:.18G
maxmemory_policy:noeviction
mem_fragmentation_ratio:85.42
mem_allocator:jemalloc-4.0.
active_defrag_running:
lazyfree_pending_objects:

可以发现实际使用内存为130.69M,而Redis申请的内存为10.90G,碎片率mem_fragmentation_ratio为85.42,相当高了

4.  清理内存碎片

默认情况下自动清理碎片的参数是关闭的,可以按如下命令查看

127.0.0.1:> config get activedefrag
) "activedefrag"
) "no"

启动自动清理内存碎片

127.0.0.1:> config set  activedefrag yes
OK

开启后再查看内存信息

127.0.0.1:> info memory
# Memory
used_memory:
used_memory_human:131.64M
used_memory_rss:
used_memory_rss_human:.71G
used_memory_peak:
used_memory_peak_human:.26G
used_memory_peak_perc:1.14%
used_memory_overhead:
used_memory_startup:
used_memory_dataset:
used_memory_dataset_perc:97.84%
total_system_memory:
total_system_memory_human:.70G
used_memory_lua:
used_memory_lua_human:.00K
maxmemory:
maxmemory_human:.18G
maxmemory_policy:noeviction
mem_fragmentation_ratio:36.61
mem_allocator:jemalloc-4.0.
active_defrag_running:
lazyfree_pending_objects:

此时redis占用的内存used_memory_rss已降低至4.71G了,内存碎片为36.61

5. 查看内存分配情况

此时也可以查看内存分配情况,其中重要的指标是查看bins里的util,此时可以发现当前最大的已达到0.998(1除外)

127.0.0.1:> memory malloc-stats
___ Begin jemalloc statistics ___
Version: 4.0.--ge9192eacf8935e29fc62fddc2701f7942b1cc02c
Assertions disabled
Run-time option settings:
opt.abort: false
opt.lg_chunk:
opt.dss: "secondary"
opt.narenas:
opt.lg_dirty_mult: (arenas.lg_dirty_mult: )
opt.stats_print: false
opt.junk: "false"
opt.quarantine:
opt.redzone: false
opt.zero: false
opt.tcache: true
opt.lg_tcache_max:
CPUs:
Arenas:
Pointer size:
Quantum size:
Page size:
Min active:dirty page ratio per arena: :
Maximum thread-cached size class:
Chunk size: (^)
Allocated: , active: , metadata: , resident: , mapped:
Current active ceiling: arenas[]:
assigned threads:
dss allocation precedence: secondary
min active:dirty page ratio: :
dirty pages: : active:dirty, sweeps, madvises, purged
allocated nmalloc ndalloc nrequests
small:
large:
huge:
total:
active:
mapped:
metadata: mapped: , allocated:
bins: size ind allocated nmalloc ndalloc nrequests curregs curruns regs pgs util nfills nflushes newruns reruns
0.720
0.435
0.152
0.195
0.019
0.148
0.006
0.175
0.711
0.875
0.996
0.982
0.997
0.882
0.990
0.987
0.775
0.881
0.955
0.982
0.964
0.989
0.930
0.995
0.984
0.994
0.982 0.994
0.991 0.998
0.994
0.991 0.992
large: size ind allocated nmalloc ndalloc nrequests curruns huge: size ind allocated nmalloc ndalloc nrequests curhchunks ---
--- End jemalloc statistics ---

6.  手动清理

其实还可以手动清理,可以采用如下命令

127.0.0.1:> memory purge
OK

7. 相关参数配置说明

内存清理相关参数如下,可以使用config get的方式查看对应的值

# 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 10 # 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

至此,Redis4.0.14版本的内存碎片清理就测试完成了。

最新文章

  1. bzoj 1391
  2. 报错注入分析之(count()、rand()、group by)分析,被大佬称为floor报错注入
  3. spring源码学习之路---AOP初探(六)
  4. Linux“Bash”漏洞大爆发
  5. 笔记——Function类型 及其 call、apply方法
  6. C# 学习之旅(1)
  7. mysql 锁表查询及其处理
  8. CentOS7 下安装telnet服务
  9. HTML学习(五)链接
  10. java生成首字母拼音简码的总结
  11. Python中的单例模式
  12. IT轮子系列(一)——DropDownList 的绑定,你秒懂了吗
  13. Grid move
  14. Retrieve id of record just inserted into a Java DB (Derby) database
  15. js 正则判断字符串下划线的长度
  16. 2019 wannafly winter camp
  17. 重庆市队选拔 CQOI2015 解题报告
  18. 【JS】jQuery中将数组转换成字符串join()和push()使用
  19. configparser模块读写ini配置文件
  20. SSH内存泄露及Spring Quartz问题

热门文章

  1. 【CSS3】纯CSS3制作页面切换效果
  2. springboot2.04与activiti 6.0集成
  3. java io流与序列化反序列化
  4. SQL2008 R2安装完成后开启services服务指引和 sa账号启用、数据类型
  5. Python--day40--主线程和子线程代码讲解
  6. WPF 托盘显示
  7. JS的防抖与节流学习笔记
  8. (摘录)ISO C++ Lambda表达式
  9. slim中的请求URI
  10. IntelliJ IDEA+springboot+jdbctemplet+easyui+maven+oracle搭建简易开发框架(一)