当前位置:   article > 正文

redis4支持内存碎片清理功能使用

清理手机内存碎片整理

最近看到redis4支持内存碎片清理了, 之前一直期待有这么一个功能, 因为之前遇到内存碎片的解决办法就是重启, 现在终于有了优雅的解决方案.\^o^/, 这个功能其实oranagra 在2017年1月1日已经提交pr了, 相关地址: https://github.com/antirez/redis/pull/3720

版本说明:

  •  Redis 4.0-RC3 以上版本才支持的
  • 需要使用jemalloc作为内存分配器(默认的)

功能介绍:

  • 支持在运行期进行自动内存碎片清理 (config set activedefrag yes)
  • 支持通过命令 memory purge 进行清理(与自动清理区域不同)

功能验证流程:

(1) 首先需要拉取4.0-RC3之后的版本代码, 编译

(2) 启动时限定内存大小为1g并启动lru, 命令如下:

./src/redis-server --maxmemory 1gb --maxmemory-policy allkeys-lru --activedefrag no --port 6383

(3) 构造大量数据并导致lru, 这样可以触发内存碎片, 命令如下:

redis-cli -p 6383 debug populate 7000000 asdf 150

(4) 查看当前的内存使用情况, 会发现有200多万的数据被清理掉了

  1. $ redis-cli -p 6383 info keyspace
  2. # Keyspace
  3. db0:keys=4649543,expires=0,avg_ttl=0

(5) 查看当前的内存碎片率, 这时碎片率(mem_fragmentation_ratio)很高 : 1.54, 意味着54%的内存浪费

  1. $ redis-cli -p 6383 info memory
  2. # Memory
  3. used_memory:1073741736
  4. used_memory_human:1024.00M
  5. used_memory_rss:1650737152
  6. used_memory_rss_human:1.54G
  7. used_memory_peak:1608721680
  8. used_memory_peak_human:1.50G
  9. used_memory_peak_perc:66.75%
  10. used_memory_overhead:253906398
  11. used_memory_startup:766152
  12. used_memory_dataset:819835338
  13. used_memory_dataset_perc:76.41%
  14. total_system_memory:67535904768
  15. total_system_memory_human:62.90G
  16. used_memory_lua:37888
  17. used_memory_lua_human:37.00K
  18. maxmemory:1073741824
  19. maxmemory_human:1.00G
  20. maxmemory_policy:allkeys-lru
  21. mem_fragmentation_ratio:1.54
  22. mem_allocator:jemalloc-4.0.3
  23. active_defrag_running:0
  24. lazyfree_pending_objects:0

(6) 看看内存分配的详细情况, 这个地方看不懂可以看看: 科普文, 关键的是util指标, 指的是内存利用率, 最大的bins内存util是0.661, 说明内存利用率不高

  1. $ echo "`redis-cli -p 6383 memory malloc-stats`"
  2. ___ Begin jemalloc statistics ___
  3. Version: 4.0.3-0-ge9192eacf8935e29fc62fddc2701f7942b1cc02c
  4. Assertions disabled
  5. Run-time option settings:
  6. opt.abort: false
  7. opt.lg_chunk: 21
  8. opt.dss: "secondary"
  9. opt.narenas: 48
  10. opt.lg_dirty_mult: 3 (arenas.lg_dirty_mult: 3)
  11. opt.stats_print: false
  12. opt.junk: "false"
  13. opt.quarantine: 0
  14. opt.redzone: false
  15. opt.zero: false
  16. opt.tcache: true
  17. opt.lg_tcache_max: 15
  18. CPUs: 12
  19. Arenas: 48
  20. Pointer size: 8
  21. Quantum size: 8
  22. Page size: 4096
  23. Min active:dirty page ratio per arena: 8:1
  24. Maximum thread-cached size class: 32768
  25. Chunk size: 2097152 (2^21)
  26. Allocated: 1074509704, active: 1609732096, metadata: 41779072, resident: 1651118080, mapped: 1652555776
  27. Current active ceiling: 1610612736
  28. arenas[0]:
  29. assigned threads: 1
  30. dss allocation precedence: secondary
  31. min active:dirty page ratio: 8:1
  32. dirty pages: 393001:24 active:dirty, 0 sweeps, 0 madvises, 0 purged
  33. allocated nmalloc ndalloc nrequests
  34. small: 1006565256 28412640 9802493 35714594
  35. large: 835584 20 11 20
  36. huge: 67108864 1 0 1
  37. total: 1074509704 28412661 9802504 35714615
  38. active: 1609732096
  39. mapped: 1650458624
  40. metadata: mapped: 40202240, allocated: 491904
  41. bins: size ind allocated nmalloc ndalloc nrequests curregs curruns regs pgs util nfills nflushes newruns reruns
  42. 8 0 1992 319 70 357 249 1 512 1 0.486 7 8 1 0
  43. 16 1 148618896 14110300 4821619 14310175 9288681 55119 256 1 0.658 141103 48825 55119 5
  44. 24 2 112104360 7200400 2529385 7300348 4671015 14064 512 3 0.648 72004 25881 14064 1
  45. 32 3 288 112 103 7003270 9 1 128 1 0.070 3 7 1 0
  46. 40 4 360 109 100 171 9 1 512 5 0.017 3 7 1 0
  47. 48 5 1248 112 86 63 26 1 256 3 0.101 2 5 1 0
  48. 56 6 896 106 90 16 16 1 512 7 0.031 2 6 1 0
  49. 64 7 128 64 62 5 2 1 64 1 0.031 1 3 1 0
  50. 80 8 880 106 95 7 11 1 256 5 0.042 2 4 1 0
  51. 96 9 9120 212 117 97 95 1 128 3 0.742 4 6 2 1
  52. 112 10 336 109 106 2 3 1 256 7 0.011 3 6 3 0
  53. 128 11 640 40 35 4 5 1 32 1 0.156 3 4 2 0
  54. 160 12 740617440 7000148 2371289 7000001 4628859 54688 128 5 0.661 70271 24334 54689 4
  55. 192 13 768 68 64 1 4 1 64 3 0.062 2 4 2 0
  56. 224 14 4683616 100000 79091 99946 20909 781 128 7 0.209 1000 1641 782 0
  57. 256 15 0 16 16 4 0 0 16 1 1 1 3 1 0
  58. 320 16 5120 64 48 16 16 1 64 5 0.250 1 3 1 0
  59. 384 17 768 33 31 2 2 1 32 3 0.062 1 3 1 0
  60. 448 18 28672 64 0 0 64 1 64 7 1 1 0 1 0
  61. 512 19 1024 10 8 4 2 1 8 1 0.250 1 2 2 0
  62. 640 20 0 32 32 1 0 0 32 5 1 1 3 1 0
  63. ---
  64. 896 22 48384 85 31 50 54 2 32 7 0.843 2 3 2 0
  65. 1024 23 3072 10 7 3 3 1 4 1 0.750 1 2 3 0
  66. 1280 24 20480 16 0 0 16 1 16 5 1 1 0 1 0
  67. 1536 25 15360 10 0 0 10 2 8 3 0.625 1 0 2 0
  68. 1792 26 28672 16 0 0 16 1 16 7 1 1 0 1 0
  69. 2048 27 4096 10 8 2 2 1 2 1 1 1 2 5 0
  70. ---
  71. 3584 30 35840 10 0 0 10 2 8 7 0.625 1 0 2 0
  72. ---
  73. 5120 32 250880 49 0 49 49 13 4 5 0.942 0 0 13 0
  74. ---
  75. 8192 35 81920 10 0 0 10 10 1 2 1 1 0 10 0
  76. ---
  77. large: size ind allocated nmalloc ndalloc nrequests curruns
  78. 16384 39 16384 2 1 2 1
  79. 20480 40 40960 2 0 2 2
  80. ---
  81. 32768 43 32768 1 0 1 1
  82. 40960 44 40960 10 9 10 1
  83. ---
  84. 81920 48 81920 1 0 1 1
  85. ---
  86. 131072 51 131072 1 0 1 1
  87. 163840 52 163840 1 0 1 1
  88. ---
  89. 327680 56 327680 1 0 1 1
  90. ---
  91. 1048576 63 0 1 1 1 0
  92. ---
  93. huge: size ind allocated nmalloc ndalloc nrequests curhchunks
  94. ---
  95. 67108864 87 67108864 1 0 1 1
  96. ---
  97. --- End jemalloc statistics ---

(7) 开启自动内存碎片整理

  1. $ redis-cli -p 6383 config set activedefrag yes
  2. OK

(8) 等会儿再看看, 发现内存碎片降低了

  1. $ redis-cli -p 6383 info memory
  2. # Memory
  3. used_memory:1073740712
  4. used_memory_human:1024.00M
  5. used_memory_rss:1253371904
  6. used_memory_rss_human:1.17G
  7. used_memory_peak:1608721680
  8. used_memory_peak_human:1.50G
  9. used_memory_peak_perc:66.74%
  10. used_memory_overhead:253906398
  11. used_memory_startup:766152
  12. used_memory_dataset:819834314
  13. used_memory_dataset_perc:76.41%
  14. total_system_memory:67535904768
  15. total_system_memory_human:62.90G
  16. used_memory_lua:37888
  17. used_memory_lua_human:37.00K
  18. maxmemory:1073741824
  19. maxmemory_human:1.00G
  20. maxmemory_policy:allkeys-lru
  21. mem_fragmentation_ratio:1.17
  22. mem_allocator:jemalloc-4.0.3
  23. active_defrag_running:0
  24. lazyfree_pending_objects:0

(9) 可以再看看内存利用率, 可以看到已经上升到0.82

  1. $ echo "`redis-cli -p 6383 memory malloc-stats`"
  2. ___ Begin jemalloc statistics ___
  3. Version: 4.0.3-0-ge9192eacf8935e29fc62fddc2701f7942b1cc02c
  4. Assertions disabled
  5. Run-time option settings:
  6. opt.abort: false
  7. opt.lg_chunk: 21
  8. opt.dss: "secondary"
  9. opt.narenas: 48
  10. opt.lg_dirty_mult: 3 (arenas.lg_dirty_mult: 3)
  11. opt.stats_print: false
  12. opt.junk: "false"
  13. opt.quarantine: 0
  14. opt.redzone: false
  15. opt.zero: false
  16. opt.tcache: true
  17. opt.lg_tcache_max: 15
  18. CPUs: 12
  19. Arenas: 48
  20. Pointer size: 8
  21. Quantum size: 8
  22. Page size: 4096
  23. Min active:dirty page ratio per arena: 8:1
  24. Maximum thread-cached size class: 32768
  25. Chunk size: 2097152 (2^21)
  26. Allocated: 1074509800, active: 1307602944, metadata: 41779072, resident: 1512247296, mapped: 1652555776
  27. Current active ceiling: 1308622848
  28. arenas[0]:
  29. assigned threads: 1
  30. dss allocation precedence: secondary
  31. min active:dirty page ratio: 8:1
  32. dirty pages: 319239:39882 active:dirty, 4878 sweeps, 6343 madvises, 33915 purged
  33. allocated nmalloc ndalloc nrequests
  34. small: 1006565352 35456589 16846439 45126633
  35. large: 835584 24 15 24
  36. huge: 67108864 1 0 1
  37. total: 1074509800 35456614 16846454 45126658
  38. active: 1307602944
  39. mapped: 1650458624
  40. metadata: mapped: 40202240, allocated: 491904
  41. bins: size ind allocated nmalloc ndalloc nrequests curregs curruns regs pgs util nfills nflushes newruns reruns
  42. 8 0 1992 319 70 357 249 1 512 1 0.486 7 8 1 0
  43. 16 1 148618896 17658482 8369801 17858357 9288681 44332 256 1 0.818 141103 48825 55119 26364
  44. 24 2 112104360 8897298 4226283 8997246 4671015 11525 512 3 0.791 72004 25881 14064 6205
  45. 32 3 384 115 103 9371363 12 1 128 1 0.093 4 7 1 0
  46. 40 4 360 109 100 171 9 1 512 5 0.017 3 7 1 0
  47. 48 5 1248 112 86 63 26 1 256 3 0.101 2 5 1 0
  48. 56 6 896 106 90 16 16 1 512 7 0.031 2 6 1 0
  49. 64 7 128 64 62 5 2 1 64 1 0.031 1 3 1 0
  50. 80 8 880 106 95 7 11 1 256 5 0.042 2 4 1 0
  51. 96 9 9120 212 117 97 95 1 128 3 0.742 4 6 2 1
  52. 112 10 336 109 106 2 3 1 256 7 0.011 3 6 3 0
  53. 128 11 640 40 35 4 5 1 32 1 0.156 3 4 2 0
  54. 160 12 740617440 8788058 4159199 8787911 4628859 44056 128 5 0.820 70271 24334 54689 26488
  55. 192 13 768 68 64 1 4 1 64 3 0.062 2 4 2 0
  56. 224 14 4683616 110956 90047 110902 20909 467 128 7 0.349 1000 1641 782 105
  57. 256 15 0 16 16 4 0 0 16 1 1 1 3 1 0
  58. 320 16 5120 64 48 16 16 1 64 5 0.250 1 3 1 0
  59. 384 17 768 33 31 2 2 1 32 3 0.062 1 3 1 0
  60. 448 18 28672 64 0 0 64 1 64 7 1 1 0 1 0
  61. 512 19 1024 10 8 4 2 1 8 1 0.250 1 2 2 0
  62. 640 20 0 32 32 1 0 0 32 5 1 1 3 1 0
  63. ---
  64. 896 22 48384 85 31 50 54 2 32 7 0.843 2 3 2 0
  65. 1024 23 3072 10 7 3 3 1 4 1 0.750 1 2 3 0
  66. 1280 24 20480 16 0 0 16 1 16 5 1 1 0 1 0
  67. 1536 25 15360 10 0 0 10 2 8 3 0.625 1 0 2 0
  68. 1792 26 28672 16 0 0 16 1 16 7 1 1 0 1 0
  69. 2048 27 4096 10 8 2 2 1 2 1 1 1 2 5 0
  70. ---
  71. 3584 30 35840 10 0 0 10 2 8 7 0.625 1 0 2 0
  72. ---
  73. 5120 32 250880 49 0 49 49 13 4 5 0.942 0 0 13 0
  74. ---
  75. 8192 35 81920 10 0 0 10 10 1 2 1 1 0 10 0
  76. ---
  77. large: size ind allocated nmalloc ndalloc nrequests curruns
  78. 16384 39 16384 2 1 2 1
  79. 20480 40 40960 2 0 2 2
  80. ---
  81. 32768 43 32768 1 0 1 1
  82. 40960 44 40960 14 13 14 1
  83. ---
  84. 81920 48 81920 1 0 1 1
  85. ---
  86. 131072 51 131072 1 0 1 1
  87. 163840 52 163840 1 0 1 1
  88. ---
  89. 327680 56 327680 1 0 1 1
  90. ---
  91. 1048576 63 0 1 1 1 0
  92. ---
  93. huge: size ind allocated nmalloc ndalloc nrequests curhchunks
  94. ---
  95. 67108864 87 67108864 1 0 1 1
  96. ---
  97. --- End jemalloc statistics ---

(10) 别急, 还有一个大招: 手动清理

$ redis-cli -p 6383 memory purge

(11) 再次查看内存使用情况: 发现碎片率降到1.04, 内存利用率到0.998, 内存碎片基本上消灭了^_^

  1. $ redis-cli -p 6383 info memory
  2. # Memory
  3. used_memory:1073740904
  4. used_memory_human:1024.00M
  5. used_memory_rss:1118720000
  6. used_memory_rss_human:1.04G
  7. used_memory_peak:1608721680
  8. used_memory_peak_human:1.50G
  9. used_memory_peak_perc:66.74%
  10. used_memory_overhead:253906398
  11. used_memory_startup:766152
  12. used_memory_dataset:819834506
  13. used_memory_dataset_perc:76.41%
  14. total_system_memory:67535904768
  15. total_system_memory_human:62.90G
  16. used_memory_lua:37888
  17. used_memory_lua_human:37.00K
  18. maxmemory:1073741824
  19. maxmemory_human:1.00G
  20. maxmemory_policy:allkeys-lru
  21. mem_fragmentation_ratio:1.04
  22. mem_allocator:jemalloc-4.0.3
  23. active_defrag_running:0
  24. lazyfree_pending_objects:0
  25. $

配置说明:

  1. # Enabled active defragmentation
  2. # 碎片整理总开关
  3. # activedefrag yes
  4. # Minimum amount of fragmentation waste to start active defrag
  5. # 内存碎片达到多少的时候开启整理
  6. active-defrag-ignore-bytes 100mb
  7. # Minimum percentage of fragmentation to start active defrag
  8. # 碎片率达到百分之多少开启整理
  9. active-defrag-threshold-lower 10
  10. # Maximum percentage of fragmentation at which we use maximum effort
  11. # 碎片率小余多少百分比开启整理
  12. active-defrag-threshold-upper 100
  13. # Minimal effort for defrag in CPU percentage
  14. active-defrag-cycle-min 25
  15. # Maximal effort for defrag in CPU percentage
  16. active-defrag-cycle-max 75

总结: 

从测试的结果看, 效果还是非常不错的, 另外在配置中我们可以看到如下一段声明:

145729_d4PA_871213.png

说明现在这个功能还是实验性质的, 对应的命令在官方文档中都没有看到. 但是它也说经过了压力测试, 而且现在也一年多了, 经受了一些考验, 可以尝试小流量上线观察

TODO redis 内存碎片整理实现

转载于:https://my.oschina.net/watliu/blog/1620666

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号