美文网首页数据库干货铺
Redis内存碎片清理

Redis内存碎片清理

作者: July_geng | 来源:发表于2020-03-21 09:34 被阅读0次

    (因复制格式问题,引用部分的格式可读性不强,建议关注微信公众号【数据库干货铺】读取原文)

    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:6379> info memory # Memory used_memory:137040696 used_memory_human:130.69M used_memory_rss:11705876480 used_memory_rss_human:10.90G used_memory_peak:12091169848 used_memory_peak_human:11.26G used_memory_peak_perc:1.13% used_memory_overhead:3473184 used_memory_startup:786648 used_memory_dataset:133567512 used_memory_dataset_perc:98.03% total_system_memory:16862617600 total_system_memory_human:15.70G used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:12000000000 maxmemory_human:11.18G maxmemory_policy:noeviction mem_fragmentation_ratio:85.42 mem_allocator:jemalloc-4.0.3 active_defrag_running:0 lazyfree_pending_objects:0

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

    4.  清理内存碎片

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

    127.0.0.1:6379>config get activedefrag

    1)"activedefrag"

    2)"no"

    启动自动清理内存碎片

    127.0.0.1:6379>config set activedefrag yes

    OK

    开启后再查看内存信息

    127.0.0.1:6379>info memory

    #Memory

    used_memory:138029408

    used_memory_human:131.64Mused_memory_rss:5052907520used_memory_rss_human:4.71Gused_memory_peak:12091169848used_memory_peak_human:11.26Gused_memory_peak_perc:1.14%used_memory_overhead:3752728used_memory_startup:786648used_memory_dataset:134276680used_memory_dataset_perc:97.84%total_system_memory:16862617600total_system_memory_human:15.70Gused_memory_lua:37888used_memory_lua_human:37.00Kmaxmemory:12000000000maxmemory_human:11.18Gmaxmemory_policy:noevictionmem_fragmentation_ratio:36.61mem_allocator:jemalloc-4.0.3active_defrag_running:0lazyfree_pending_objects:0

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

    5. 查看内存分配情况

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

    127.0.0.1:6379>memory malloc-stats___ Begin jemalloc statistics ___Version:4.0.3-0-ge9192eacf8935e29fc62fddc2701f7942b1cc02cAssertions disabledRun-time option settings:opt.abort:falseopt.lg_chunk:21opt.dss:"secondary"opt.narenas:8opt.lg_dirty_mult:3(arenas.lg_dirty_mult:3)opt.stats_print:falseopt.junk:"false"opt.quarantine:0opt.redzone:falseopt.zero:falseopt.tcache:trueopt.lg_tcache_max:15CPUs:2Arenas:8Pointer size:8Quantum size:8Page size:4096Min active:dirty page ratio per arena:8:1Maximum thread-cached sizeclass:32768Chunk size:2097152(2^21)Allocated:138983464,active:149237760,metadata:133846144,resident:299532288,mapped:5274861568Current active ceiling:153092096arenas[0]:assigned threads:1dss allocation precedence:secondarymin active:dirty page ratio:8:1dirty pages:36435:4043active:dirty,348100sweeps,737670madvises,4686933purged                            allocated      nmalloc      ndalloc    nrequestssmall:511734162794625727800354215995984large:20701184639706763967137682104huge:67108864754753754total:1389834643434407834197820223678842active:149237760mapped:5270667264metadata:mapped:130830336,allocated:115776bins:size ind    allocated      nmalloc      ndalloc    nrequests      curregs      curruns regs pgs  util      nfills    nflushes      newruns      reruns8029524753438418226389369151210.720260025661016169960057021025658377347779164372539225610.43561712648072213625124282459296485329614174754923393435844151230.152102398100732165611083971323800182818031107716725112810.195103188310404400893883128504610151250.01978779110485182410123100852381316838125630.148639070391895661120066576366556336542832006051270.0061132518434130213564744480222190122212068140167695626410.1753863443809347021105808224312032410503213011126405452803915425650.71136308406731255626896912483841173523116051956326121300411612830.87517234224429049828112102571524970944947983780792296925670.996103822070019425291281117305624114823979615009561352433210.98211994192687491114916012510880676589673396186704531932512850.99712036203445279974192138678416315416270210485345286430.882809519909253888522414105078493741493272388263646913712870.9901453421327723765325615177920122377121682108334695441610.987112161809476481025320161587201606871601911395080496106450.775828219932245880638417108288871668688456781282103230.8817364185942691830448185205763118653107032352761162196470.955988520257484912775121936608013907713836213670871591810.982166772120317307758640202963201366741362111387569463153250.9648781193274294119476821754176164055163073154637982621630.98913751195141023285389622507136142840142274104081566193270.93088311929744881232102423460800812408079071715450113410.99511411175382020419321280249676801423751416193997592756481650.984128801916288529741536258064008324082715136396552566830.9941174417875102961083179226507136709317064850310283181670.982814017915440311382048275406725291552651400112641322118604165822642518882560281487360115947115366268877458173850.994150121996914410962307229102297670646703135882233384430.9911044517189175462050358430163430476807763516603045657871111901754295381175409631487424569595684043668119119111917616863569590512032615424014546114425926750971202301450.9981770422138360761483614433119808045380451858331519598230.9948050161482272117597168348243203251832403131084511529470.99165981441680631715819235188006403552103529153629722295229512139103421283552100102403622323207616875950662952181092511196017361379171769122883720398085286052694402961661661318941166325286001433638196403236992368552461013769270.992687915285184221777large:size ind    allocated      nmalloc      ndalloc    nrequests      curruns16384396881287548750677604220480408806409391934812935614324576416881286279625163672828672426021124187416642612132768432818048179371785118430864096044143360063064506306415630645035491524512779525117509151172657344468601605916590159161565536473932162953294729536819204813107208244822882441698304492949125037503450373114688505734404224421942245131072513932162233223022333163840521638400279827882798101966085311796481740173417406229376542293761162116111621262144555242888728708722327680563276801290128912901393216570782782782045875258052052052005242885952428840039940016553606065536060660560617864326103713713710917504620196196196010485766320971522242222242131072064131072032832732811572864650179179179018350086608383830huge:size ind    allocated      nmalloc      ndalloc    nrequests  curhchunks2097152670110110110026214406802632632630314572869086868603670016700919191041943047104545450524288072055555506291456730262626073400327401515150838860875013131301048576076088801258291277013131301468006478022201677721679044402097152080022202516582481022202936012882022203355443283044404194304084022205033164885022205872025686022206710886487671088647671------End jemalloc statistics---

    6.  手动清理

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

    127.0.0.1:6379>memory purge

    OK

    7. 相关参数配置说明

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

    # Enabled active defragmentation # 碎片整理总开关

    # activedefrag yes

    # Minimum amountoffragmentation waste to start active defrag# 内存碎片达到多少的时候开启整理active-defrag-ignore-bytes 100mb# Minimum percentageoffragmentation to start active defrag# 碎片率达到百分之多少开启整理active-defrag-threshold-lower10# Maximum percentageoffragmentation at which we use maximum effort# 碎片率小余多少百分比开启整理active-defrag-threshold-upper100# Minimal effortfordefraginCPU percentageactive-defrag-cycle-min25# Maximal effortfordefraginCPU percentageactive-defrag-cycle-max75

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

    相关文章

      网友评论

        本文标题:Redis内存碎片清理

        本文链接:https://www.haomeiwen.com/subject/vfaeyhtx.html