{"id":2079,"date":"2023-04-01T21:48:45","date_gmt":"2023-04-01T13:48:45","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2079"},"modified":"2023-04-06T16:00:09","modified_gmt":"2023-04-06T08:00:09","slug":"playing-redis-deleted-two-million-keys-why-is-the-memory-still-not-released","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/playing-redis-deleted-two-million-keys-why-is-the-memory-still-not-released\/","title":{"rendered":"\u73a9\u8f6cRedis-\u5220\u9664\u4e86\u4e24\u767e\u4e07key\uff0c\u4e3a\u4ec0\u4e48\u5185\u5b58\u4f9d\u65e7\u672a\u91ca\u653e\uff1f"},"content":{"rendered":"<h2>\u80cc\u666f<\/h2>\n<p>\u516c\u53f8\u67d0\u4e1a\u52a1\u4f7f\u7528\u7684Redis\u96c6\u7fa4\u662f\u81ea\u5efa\u7684\uff0c\u524d\u6bb5\u65f6\u95f4\u8ba1\u5212\u5c06\u81ea\u5efaRedis\u96c6\u7fa4\u8fc1\u79fb\u5230\u8d2d\u4e70\u7684\u963f\u91cc\u4e91\u96c6\u7fa4\u3002<\/p>\n<p>\u8001\u96c6\u7fa4\u5171\u6709 350W key\uff0c\u5360\u7528\u5185\u5b58 8.8 G\uff0cDTS\u8fc1\u79fb\u524d\u5206\u6790\u53d1\u73b0\u6709\u8fd1\u4e24\u767e\u4e07\u7684key\u65e0\u9700\u8fc1\u79fb\uff0c\u4e8e\u662f\u63d0\u524d\u5220\u9664\u4e86\u8fd9\u4e24\u767e\u4e07key\u3002<\/p>\n<p>\u5220\u9664key\u540e\u53d1\u73b0redis\u5185\u5b58\u7adf\u7136\u51e0\u4e4e\u65e0\u53d8\u5316\uff0c350W key\u5220\u9664\u4e86\u4e24\u767e\u4e07\uff0c\u600e\u4e48\u4e5f\u5f97\u91ca\u653e\u51e0G\u5185\u5b58\u5427\u3002\u96be\u9053\u5220\u9664\u5931\u8d25\u4e86\uff1f\u901a\u8fc7\u6bd4\u5bf9\u6570\u636e\u53d1\u73b0\uff0c\u8ba1\u5212\u88ab\u5220\u9664\u7684\u6570\u636e\u786e\u5b9e\u5df2\u7ecf\u5220\u9664\u4e86\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u5220\u9664\u4e86\u4e24\u767e\u4e07key\uff0c\u5185\u5b58\u672a\u91ca\u653e\u5462\uff1f\u8fd9\u4e2a\u95ee\u9898\u4e00\u76f4\u56f0\u6270\u7740\u6211\uff0c\u901a\u8fc7\u67e5\u9605\u8d44\u6599\u7ec8\u4e8e\u5f04\u660e\u767d\u4e86\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u5982\u4f55\u67e5\u770bRedis\u5185\u5b58\u6570\u636e<\/h2>\n<h3>info memory<\/h3>\n<p>\u8fdb\u5165Redis\u547d\u4ee4\u884c\u754c\u9762\u540e\uff0c\u4f7f\u7528<code>info memory<\/code>\u547d\u4ee4\uff08\u96c6\u7fa4\u4f7f\u7528<code>cluster info<\/code>\u547d\u4ee4\uff09 \u5373\u53ef\u67e5\u770b\u5f53\u524dRedis\u76f8\u5173\u5185\u5b58\u4fe1\u606f\uff0c\u90e8\u5206\u4fe1\u606f\u5c55\u793a\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; info memory\n# Memory\n\n# Redis \u4fdd\u5b58\u6570\u636e\u7533\u8bf7\u7684\u5185\u5b58\u7a7a\u95f4\nused_memory:9469412118\nused_memory_human:8.82G\n\n# \u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7ed9 Redis \u8fdb\u7a0b\u7684\u5185\u5b58\u7a7a\u95f4\nused_memory_rss:11351138316\nused_memory_rss_human:10.57G\n\n# Redis \u8fdb\u7a0b\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5360\u7528\u7684\u5185\u5b58\u5cf0\u503c\nused_memory_peak:12618222522\nused_memory_peak_human:11.75G\n\n# \u5185\u5b58\u788e\u7247\u7387\uff0cused_memory_rss \/ used_memory\nmem_fragmentation_ratio:1.20\n\n# Redis \u6700\u5927\u53ef\u7528\u5185\u5b58\uff0c0\u8868\u793a\u4e0d\u9650\u5236\nmaxmemory:0\nmaxmemory_human:0B\n\n# \u5185\u5b58\u5206\u914d\u5668\nmem_allocator:jemalloc-5.1.0<\/code><\/pre>\n<p>\u6211\u4eec\u6765\u89e3\u91ca\u4e0b\u6bcf\u4e2a\u5c5e\u6027\u7684\u542b\u4e49\uff1a<\/p>\n<p><strong>used_memory<\/strong>\uff1aRedis \u4fdd\u5b58\u6570\u636e\u7533\u8bf7\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5305\u542bRedis\u8fdb\u7a0b\u53ca\u6570\u636e\u5360\u7528\u5185\u5b58\uff0c\u5355\u4f4d Byte<\/p>\n<p><strong>used_memory_rss<\/strong>\uff1a\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7ed9 Redis \u8fdb\u7a0b\u7684\u5185\u5b58\u7a7a\u95f4\uff08\u5305\u542b\u5185\u5b58\u788e\u7247\u5360\u7528\u7684\u7a7a\u95f4\uff09\uff0c\u662f\u4ece\u64cd\u4f5c\u7cfb\u7edf\u89d2\u5ea6\u770b\u7684\u6570\u636e\uff1b\u6b64\u6570\u636e\u7ed3\u679c\u7ea6\u7b49\u4e8etop\u3001ps\u547d\u4ee4\u770b\u5230\u7684\u6570\u636e\u7ed3\u679c<\/p>\n<p><strong>used_memory_peak<\/strong>\uff1aRedis \u8fdb\u7a0b\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5360\u7528\u7684\u5185\u5b58\u5cf0\u503c\uff0c<code>used_memory_peak &gt;= used_memory<\/code><\/p>\n<p><strong>maxmemory<\/strong>\uff1aRedis \u6700\u5927\u53ef\u7528\u5185\u5b58\uff0c0\u8868\u793a\u4e0d\u9650\u5236\u3002\u53ef\u4ee5\u65b9\u4fbf\u7684\u5b9e\u73b0\u5bf9\u4e00\u53f0\u670d\u52a1\u5668\u90e8\u7f72\u591a\u4e2aRedis\u8fdb\u7a0b\u7684\u5185\u5b58\u63a7\u5236\uff1b\u9632\u6b62\u6240\u7528\u5185\u5b58\u8d85\u8fc7\u670d\u52a1\u5668\u7269\u7406\u5185\u5b58\uff1b\u4fbf\u4e8e\u6570\u636e\u8d85\u51fa\u5185\u5b58\u9650\u5236\u65f6\u6267\u884cLRU\u7b49\u5220\u9664\u7b56\u7565<\/p>\n<p><strong>XXX_human<\/strong>\uff1a\u8868\u793a\u4ee5\u53ef\u8bfb\u7684\u65b9\u5f0f\u8fd4\u56deXXX<\/p>\n<p><strong>mem_fragmentation_ratio<\/strong>\uff1a\u5185\u5b58\u788e\u7247\u7387\uff0c<code>used_memory_rss \/ used_memory<\/code>\u3002\u5927\u4e8e1\u7684\u90e8\u5206\u4e3aredis\u788e\u7247\u5360\u7528\u7684\u5927\u5c0f\uff0c\u5efa\u8bae\u503c \u5927\u4e8e 1 \u4f46\u5c0f\u4e8e 1.5\uff0c\u5927\u4e8e1.5\u8bf4\u660e\u788e\u7247\u8fc7\u591a\u9700\u8981\u6e05\u7406\u4e86\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b<code>used_memory_rss<\/code>\u662f\u5927\u4e8e<code>used_memory<\/code>\u7684\uff1b\u4f46\u4e5f\u6709\u4f8b\u5916\uff0c\u5f53<code>used_memory_rss<\/code>\u5c0f\u4e8e<code>used_memory<\/code>\u65f6\uff0c\u8bf4\u660e\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7ed9Redis\u8fdb\u7a0b\u7684\u6570\u636e\uff0c\u4e0d\u8db3\u4ee5\u6ee1\u8db3\u5b9e\u9645\u5b58\u50a8\u6570\u636e\u7684\u9700\u6c42\uff0c\u6b64\u65f6Redis\u90e8\u5206\u5185\u5b58\u6570\u636e\u4f1a\u8f6c\u6362\u5230Swap\u4e2d\uff0c\u968f\u4e4b\u5f15\u53d1\u7684\u95ee\u9898\u662f\uff0c\u5f53Redis\u8bbf\u95eeSwap\u4e2d\u7684\u6570\u636e\u65f6\uff0c\u6027\u80fd\u4f1a\u4e0b\u964d \u3002<\/p>\n<h2>memory xxx \u6307\u4ee4<\/h2>\n<h3>memory doctor<\/h3>\n<p>\u5217\u51fa Redis \u670d\u52a1\u5668\u9047\u5230\u7684\u4e0d\u540c\u7c7b\u578b\u7684\u5185\u5b58\u76f8\u5173\u95ee\u9898\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u89e3\u51b3\u5efa\u8bae<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; memory doctor\nHi Sam, I can&#039;t find any memory issue in your instance. I can only account for what occurs on this base.<\/code><\/pre>\n<h3>memory malloc-stats<\/h3>\n<p>\u63d0\u4f9b\u5185\u5b58\u5206\u914d\u60c5\u51b5\u7684\u5185\u90e8\u7edf\u8ba1\u62a5\u8868\uff08\u76ee\u524d\u53ea\u652f\u6301<code>jemalloc<\/code>\u5185\u5b58\u5206\u914d\u5668)\u3002\u901a\u8fc7\u8be5\u547d\u4ee4\u53ef\u4ee5\u770b\u51fa<code>jemalloc<\/code>\u5bf9\u4e8e\u6240\u6709\u5bf9\u8c61\u8fdb\u884c\u5206\u914d\u65f6\uff0c\u5404\u5177\u4f53\u5206\u533a\u7684\u5185\u5b58\u8be6\u7ec6\u72b6\u51b5\u3002<\/p>\n<p><code>memory malloc-stats<\/code>\u547d\u4ee4\u8fd4\u56de\u4fe1\u606f\u8f83\u591a\uff0c\u6b64\u5904\u6682\u4e0d\u505a\u8be6\u7ec6\u5206\u6790\uff0c\u611f\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u6267\u884c\u547d\u4ee4\u5206\u6790\u3002<\/p>\n<h3>memory purge<\/h3>\n<p>\u624b\u52a8\u6574\u7406\u5185\u5b58\u788e\u7247\uff0c\u4f1a\u963b\u585e\u4e3b\u8fdb\u7a0b\u3002<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; memory purge\nOK<\/code><\/pre>\n<h3>memory stats<\/h3>\n<p>\u4ee5\u6570\u7ec4\u5f62\u5f0f\u8fd4\u56de\u670d\u52a1\u5668\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1a<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; memory stats\n 1) &quot;peak.allocated&quot;\n 2) (integer) 905331384\n 3) &quot;total.allocated&quot;\n 4) (integer) 905330152\n 5) &quot;startup.allocated&quot;\n 6) (integer) 791160\n 7) &quot;replication.backlog&quot;\n 8) (integer) 0\n 9) &quot;clients.slaves&quot;\n10) (integer) 0\n11) &quot;clients.normal&quot;\n12) (integer) 49694\n13) &quot;aof.buffer&quot;\n14) (integer) 0\n15) &quot;lua.caches&quot;\n16) (integer) 0\n17) &quot;db.0&quot;\n18) 1) &quot;overhead.hashtable.main&quot;\n    2) (integer) 7888\n    3) &quot;overhead.hashtable.expires&quot;\n    4) (integer) 32\n19) &quot;db.1&quot;\n20) 1) &quot;overhead.hashtable.main&quot;\n    2) (integer) 304\n    3) &quot;overhead.hashtable.expires&quot;\n    4) (integer) 32\n21) &quot;overhead.total&quot;\n22) (integer) 849110\n23) &quot;keys.count&quot;\n24) (integer) 152\n25) &quot;keys.bytes-per-key&quot;\n26) (integer) 5950914\n27) &quot;dataset.bytes&quot;\n28) (integer) 904481042\n29) &quot;dataset.percentage&quot;\n30) &quot;99.99359130859375&quot;\n31) &quot;peak.percentage&quot;\n32) &quot;99.999870300292969&quot;\n33) &quot;allocator.allocated&quot;\n34) (integer) 905598528\n35) &quot;allocator.active&quot;\n36) (integer) 905961472\n37) &quot;allocator.resident&quot;\n38) (integer) 910348288\n39) &quot;allocator-fragmentation.ratio&quot;\n40) &quot;1.0004007816314697&quot;\n41) &quot;allocator-fragmentation.bytes&quot;\n42) (integer) 362944\n43) &quot;allocator-rss.ratio&quot;\n44) &quot;1.0048421621322632&quot;\n45) &quot;allocator-rss.bytes&quot;\n46) (integer) 4386816\n47) &quot;rss-overhead.ratio&quot;\n48) &quot;0.0086568007245659828&quot;\n49) &quot;rss-overhead.bytes&quot;\n50) (integer) -902467584\n51) &quot;fragmentation&quot;\n52) &quot;0.0087051792070269585&quot;\n53) &quot;fragmentation.bytes&quot;\n54) (integer) -897408432<\/code><\/pre>\n<h3>memory usage<\/h3>\n<p>\u8fd4\u56de\u4e00\u4e2akey\u548c\u5b83\u503c\u5728\u5185\u5b58\u4e2d\u5360\u7528\u7684\u5b57\u8282\u6570(\u5305\u542bredis\u7ba1\u7406\u8be5key\u6240\u5360\u7528\u7684\u5185\u5b58)\uff1a<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; memory usage key1\n(integer) 46<\/code><\/pre>\n<h3>memory help<\/h3>\n<p>memory\u76f8\u5173\u547d\u4ee4\u7684\u5e2e\u52a9\u4fe1\u606f\uff1b<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; memory  help\n1) MEMORY &lt;subcommand&gt; arg arg ... arg. Subcommands are:\n2) DOCTOR - Return memory problems reports.\n3) MALLOC-STATS -- Return internal statistics report from the memory allocator.\n4) PURGE -- Attempt to purge dirty pages for reclamation by the allocator.\n5) STATS -- Return information about the memory usage of the server.\n6) USAGE &lt;key&gt; [SAMPLES &lt;count&gt;] -- Return memory in bytes used by &lt;key&gt; and its value. Nested values are sampled up to &lt;count&gt; times (default: 5).<\/code><\/pre>\n<h2>\u5185\u5b58\u4e3a\u4f55\u4e0d\u91ca\u653e<\/h2>\n<p>Redis\u6709\u81ea\u5df1\u7684\u5185\u5b58\u5206\u914d\u5668\uff0c\u5f53\u6570\u636e\u5220\u9664\u540e\uff0c\u91ca\u653e\u7684\u5185\u5b58\u7a7a\u95f4\u7531Redis\u81ea\u5df1\u7684\u5185\u5b58\u5206\u914d\u5668\u7ba1\u7406\uff0c\u5e76\u6ca1\u6709\u7acb\u5373\u5c06\u5185\u5b58\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u6240\u4ee5\u5bf9\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u800c\u8a00\uff0c\u4ecd\u7136\u8ba4\u4e3aRedis\u5360\u7528\u4e86\u5185\u5b58\u3002<\/p>\n<p>\u8fd9\u6837\u7684\u597d\u5904\u662f\uff0c\u51cf\u5c11Redis\u5411\u7cfb\u7edf\u7533\u8bf7\u5185\u5b58\u5206\u914d\u7684\u6b21\u6570\uff0c\u63d0\u5347Redis\u81ea\u8eab\u6027\u80fd\u3002<\/p>\n<h3>\u4ec0\u4e48\u662f\u5185\u5b58\u788e\u7247<\/h3>\n<p>\u4e0d\u5c11\u540c\u5b66\u5e94\u8be5\u542c\u8bf4\u8fc7\u78c1\u76d8\u788e\u7247\uff0c\u4f7f\u7528\u8fc7Smart Defrag\u7b49\u8f6f\u4ef6\u6e05\u7406\u8fc7\u78c1\u76d8\u788e\u7247\uff0c\u6e05\u7406\u78c1\u76d8\u788e\u7247\u80fd\u591f\u4f18\u5316\u6587\u4ef6\u7cfb\u7edf\uff0c\u5c06\u96f6\u6563\u7684\u78c1\u76d8\u7a7a\u95f4\u79fb\u52a8\u5408\u5e76\uff0c\u5c06\u9891\u7e41\u4f7f\u7528\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u653e\u7f6e\u5230\u78c1\u76d8\u7684\u901f\u5ea6\u6700\u5feb\u7684\u533a\u57df\uff0c\u4f7f\u8ba1\u7b97\u673a\u80fd\u4ee5\u6700\u9ad8\u901f\u5ea6\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<p>\u5185\u5b58\u788e\u7247\u662f\u7531\u4e8e\u8ba1\u5212\u7533\u8bf7\u7684\u7a7a\u95f4\u6bd4\u7a7a\u95f2\u7684\u8fde\u7eed\u7a7a\u95f4\u5c0f\uff0c\u5bfc\u81f4\u8fd9\u90e8\u5206\u5c0f\u5185\u5b58\u7a7a\u95f4\u65e0\u6cd5\u88ab\u4f7f\u7528\uff0c\u65e0\u6cd5\u88ab\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u5219\u53ef\u79f0\u4e3a\u5185\u5b58\u788e\u7247\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/Redis\u5185\u5b58\u788e\u7247\u793a\u610f\u56fe.png\" alt=\"Redis\u5185\u5b58\u788e\u7247\u793a\u610f\u56fe\" \/><\/p>\n<p>\u5982\u56fe\uff0c9\u5b57\u8282\u7684\u5185\u5b58\u7a7a\u95f4\u793a\u610f\u56fe\uff0c\u5e8f\u53f7\u4e3a1\u30012\u30013\u30015\u30016\u30019\u7684\u5185\u5b58\u7a7a\u95f4\u5df2\u4f7f\u7528\uff0c\u5982\u679c\u73b0\u5728\u8ba1\u5212\u7533\u8bf73\u5b57\u8282\u7684\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u6309\u7167\u73b0\u6709\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u662f\u65e0\u6cd5\u7533\u8bf7\u7684\uff0c\u6b64\u65f6\u5e8f\u53f74\u30017\u30018\u5c31\u662f\u201c\u5185\u5b58\u788e\u7247\u201d\u4e86\u3002<\/p>\n<h3>Redis\u7684\u5185\u5b58\u788e\u7247\u662f\u5982\u4f55\u5f62\u6210\u7684<\/h3>\n<p>Redis\u4ea7\u751f\u5185\u5b58\u788e\u7247\u4e3b\u8981\u7531\u4ee5\u4e0b2\u70b9\u539f\u56e0\u5bfc\u81f4\uff1b<\/p>\n<ul>\n<li>\u5185\u5b58\u5206\u914d\u5668\u673a\u5236<\/li>\n<li>Redis\u6570\u636e\u7684\u4fee\u6539\u548c\u5220\u9664\u5f15\u53d1\u7a7a\u95f4\u7684\u6269\u5bb9\u548c\u91ca\u653e<\/li>\n<\/ul>\n<h4>\u5185\u5b58\u5206\u914d\u5668\u673a\u5236<\/h4>\n<p>Redis\u6709\u51e0\u79cd\u5185\u5b58\u5206\u914d\u5668<code>jemalloc<\/code>\u3001<code>libc<\/code>\u3001<code>tcmalloc<\/code>\uff0c\u9ed8\u8ba4\u4f7f\u7528<code>jemalloc<\/code><\/p>\n<p><code>jemalloc<\/code>\u5185\u5b58\u5206\u914d\u65b9\u5f0f\u4e3a\u6309\u7167\u4e00\u7cfb\u5217\u56fa\u5b9a\u5927\u5c0f\u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff0c<code>jemalloc<\/code>\u6309\u7167\u7533\u8bf7\u7684\u5185\u5b58\u5927\u5c0f\u5206\u914d\u6700\u63a5\u8fd1\u7684\u5185\u5b58\u7a7a\u95f4<\/p>\n<p>\u6bd4\u5982\u7533\u8bf7220\u5b57\u8282\uff0c<code>jemalloc<\/code>\u4f1a\u5206\u914d256\u5b57\u8282\uff0c\u5982\u679c\u8fd8\u8981\u7ee7\u7eed\u5199\u516520\u5b57\u8282\uff0cRedis\u5219\u4e0d\u4f1a\u7ee7\u7eed\u5411\u7cfb\u7edf\u7533\u8bf7\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u4e3a\u5148\u524d\u7533\u8bf7\u7684256\u5b57\u8282\u8fd8\u5269\u4f5936\u5b57\u8282\u53ef\u7528\uff1b\u4f46\u5982\u679c\u6b64\u65f6\u9700\u8981\u7ee7\u7eed\u5199\u516560\u5b57\u8282\uff0c\u5219\u5df2\u5206\u914d\u7a7a\u95f4\u4e0d\u591f\u7528\u4e86\uff0c\u9700\u8981\u518d\u6b21\u5411\u7cfb\u7edf\u7533\u8bf7\u5206\u914d\u5185\u5b58\u7a7a\u95f4<\/p>\n<blockquote>\n<p>\u9ed8\u8ba464\u4f4d\u7cfb\u7edfjemalloc\u7684\u5212\u5206\u65b9\u5f0f\u5982\u4e0b\uff1a<br \/>\nSmall: [8], [16, 32, 48, &#8230;, 128], [192, 256, 320, &#8230;, 512], [768, 1024, 1280, &#8230;, 3840]<br \/>\nLarge: [4 KiB, 8 KiB, 12 KiB, &#8230;, 4072 KiB]<br \/>\nHuge: [4 MiB, 8 MiB, 12 MiB, &#8230;]<\/p>\n<\/blockquote>\n<h4>Redis\u6570\u636e\u7684\u4fee\u6539\u548c\u5220\u9664\u5f15\u53d1\u7a7a\u95f4\u7684\u6269\u5bb9\u548c\u91ca\u653e<\/h4>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff0ckey1\u6269\u5bb9\u65f6\u9700\u65b0\u589e2\u5b57\u8282\uff0c\u4e3a\u4fdd\u8bc1\u5185\u5b58\u7a7a\u95f4\u8fde\u7eed\u6027\uff0ckey2\u53d1\u751f\u8fc1\u79fb\uff1b\u5f53key3\u91ca\u653e\u7a7a\u95f4\u540e\uff0c\u5e8f\u53f7\u4e3a7\u30018\u300114\u300115\u7684\u7a7a\u95f4\u5747\u672a\u4f7f\u7528\u3002<\/p>\n<p>\u5982\u679c\u6b64\u65f6\u6709key\u5e0c\u671b\u7533\u8bf73\u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u867d\u7136\u603b\u5171\u5269\u4f59\u4e864\u5b57\u8282\uff0c\u4f46\u662f\u6ca1\u6709\u8fde\u7eed\u76843\u5b57\u8282\u7a7a\u95f4\uff0c\u6240\u4ee5\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p>\u4e86\u89e3\u4e86\u539f\u7406\u540e\uff0c\u5982\u679c\u6211\u4eec\u60f3\u6d4b\u8bd5\u5185\u5b58\u788e\u7247\u6e05\u7406\uff0c\u5219\u53ef\u4ee5\u63d2\u5165\u5927\u91cfkey\uff0c\u518d\u5220\u9664\u5927\u91cfkey\uff08\u6216\u8005\u63d2\u5165key\u65f6\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff09\uff0c\u4ee5\u6b64\u6765\u6a21\u62df\u9ad8\u5185\u5b58\u788e\u7247\u7387\u573a\u666f\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/Redis\u5185\u5b58\u7a7a\u95f4\u7684\u6269\u5bb9\u548c\u91ca\u653e.png\" alt=\"Redis\u5185\u5b58\u7a7a\u95f4\u7684\u6269\u5bb9\u548c\u91ca\u653e\" \/><\/p>\n<h2>\u5982\u4f55\u91ca\u653e\u5185\u5b58<\/h2>\n<h3>\u91cd\u542fRedis\u91ca\u653e\u5185\u5b58<\/h3>\n<p>\u8fd9\u5e94\u8be5\u662f\u6700\u76f4\u63a5\u6709\u6548\u7684\u65b9\u6cd5\u3002\u4f46\u662f\u751f\u4ea7\u73af\u5883\u4e0d\u662f\u4f60\u60f3\u91cd\u542f\u5c31\u80fd\u91cd\u542f\u7684\u3002\u56e0\u4e3a\u91cd\u542fRedis\u9700\u8981\u8003\u8651\u5f88\u591a\u95ee\u9898\uff0c\u6bd4\u5982\uff1a<\/p>\n<ul>\n<li>\u91cd\u542f\u65f6\u52a0\u8f7d\u6062\u590d\u6570\u636e\u9700\u8981\u65f6\u95f4\uff0c\u5728\u6b64\u671f\u95f4Redis\u5c06\u4e0d\u53ef\u7528<\/li>\n<li>\u786e\u4fdd\u6240\u6709\u7684\u914d\u7f6e\u9879\u53d8\u66f4\u90fd\u66f4\u65b0\u5230<code>redis.conf<\/code>\uff0c\u5426\u5219\u91cd\u542f\u540e\u5728\u7ebf\u4fee\u6539\u7684\u914d\u7f6e\u5c06\u8fd8\u539f<\/li>\n<\/ul>\n<h3>\u4f7f\u7528\u5907\u7528Redis\u5b9e\u4f8b\u540c\u6b65\u8fc1\u79fb<\/h3>\n<p>\u5185\u5b58\u788e\u7247\u7684\u4ea7\u751f\u5f88\u5927\u7a0b\u5ea6\u4e0a\u662f\u56e0\u4e3a\u6570\u636e\u7684\u4fee\u6539\u548c\u5220\u9664\uff0c\u6240\u4ee5\u8ba1\u5212\u6e05\u7406\u5b9e\u4f8bA\u7684\u5185\u5b58\u788e\u7247\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f15\u5165\u5b9e\u4f8bB\uff0c\u5c06\u5b9e\u4f8bA\u7684\u6570\u636e\u5168\u91cf\u540c\u6b65\u5230\u5b9e\u4f8bB\u4e2d\uff0c\u518d\u4f7f\u7528\u5b9e\u4f8bB\u66ff\u6362\u539f\u6709\u7684\u5b9e\u4f8bA\u5bf9\u5916\u63d0\u4f9b\u670d\u52a1\u3002<\/p>\n<p>\u6b64\u65b9\u6cd5\u601d\u8def\u867d\u7136\u662f\u901a\u7684\uff0c\u4f46\u64cd\u4f5c\u6d41\u7a0b\u590d\u6742\u4e14\u98ce\u9669\u8f83\u9ad8\uff0c\u751f\u4ea7\u73af\u5883\u51e0\u4e4e\u4e0d\u4f1a\u91c7\u7528\u3002\u4f46\u5982\u679c\u672c\u5c31\u8ba1\u5212\u8fc1\u79fb\u5b9e\u4f8b\uff0c\u90a3\u5c31\u521a\u597d\u53ef\u4ee5\u91c7\u7528\u6b64\u79cd\u601d\u8def\u3002<\/p>\n<h3>memory purge\u624b\u52a8\u788e\u7247\u6574\u7406<\/h3>\n<p>\u624b\u52a8\u6574\u7406\u5185\u5b58\u788e\u7247\uff0c\u4f1a\u963b\u585e\u4e3b\u8fdb\u7a0b\uff0c\u751f\u4ea7\u73af\u5883\u614e\u7528\u3002<\/p>\n<p><code>memory purge<\/code>\u548c<code>activedefrag<\/code>\u56de\u6536\u7684\u5e76\u4e0d\u662f\u540c\u4e00\u5757\u533a\u57df\u7684\u5185\u5b58\uff0c\u5b83\u7b80\u5355\u7c97\u66b4\u7684\u5c1d\u8bd5\u6e05\u9664\u810f\u9875\u4ee5\u4fbf\u5185\u5b58\u5206\u914d\u5668\u56de\u6536\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u548c<code>activedefrag<\/code>\u914d\u5408\u4f7f\u7528\uff0c<code>memory purge<\/code>\u5728\u6781\u7aef\u60c5\u51b5\u4e0b\u6548\u679c\u8f83\u597d\uff0c<code>activedefrag<\/code>\u5219\u66f4\u5f7b\u5e95\u3002<\/p>\n<h3>\u5f00\u542factivedefrag\u81ea\u52a8\u788e\u7247\u6574\u7406<\/h3>\n<p>\u5728Redis 4.0 \u7248\u672c\u540e\u65b0\u589e\u914d\u7f6e\u9879<code>activedefrag<\/code>\uff08active\uff1a\u4e3b\u52a8\u7684\uff0cdefrag\uff1a\u6574\u7406\u788e\u7247\uff09\uff0c<code>activedefrag<\/code>\u9ed8\u8ba4\u5173\u95ed\uff0c\u8ba1\u5212\u6e05\u7406\u788e\u7247\u65f6\u9700\u624b\u52a8\u5f00\u542f\uff0c\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; config set activedefrag yes<\/code><\/pre>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u76f8\u5173\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u4ee5\u4e0b\u5185\u5bb9\u8282\u9009\u81f3 redis.conf\n\n########################### ACTIVE DEFRAGMENTATION #######################\n\n# 3. Once you experience fragmentation, you can enable this feature when\n#    needed with the command &quot;CONFIG SET activedefrag yes&quot;.\n#\n\n# Enabled active defragmentation\n# activedefrag yes\n\n# Minimum amount of fragmentation waste to start active defrag\n# active-defrag-ignore-bytes 100mb\n\n# Minimum percentage of fragmentation to start active defrag\n# active-defrag-threshold-lower 10\n\n# Maximum percentage of fragmentation at which we use maximum effort\n# active-defrag-threshold-upper 100\n\n# Minimal effort for defrag in CPU percentage\n# active-defrag-cycle-min 5\n\n# Maximal effort for defrag in CPU percentage\n# active-defrag-cycle-max 75\n\n# Maximum number of set\/hash\/zset\/list fields that will be processed from\n# the main dictionary scan\n# active-defrag-max-scan-fields 1000<\/code><\/pre>\n<p><strong>\u5185\u5b58\u788e\u7247\u6574\u7406\u5f00\u5173\uff08\u9700\u540c\u65f6\u6ee1\u8db3\u624d\u6267\u884c\uff09<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>activedefrag<\/strong>\uff1a\u5185\u5b58\u788e\u7247\u6574\u7406\u603b\u5f00\u5173\uff0c\u5f00\u542f\u540e\u624d\u6709\u53ef\u80fd\u6267\u884c\u788e\u7247\u6574\u7406<\/li>\n<li><strong>active-defrag-ignore-bytes<\/strong>\uff1a\u5185\u5b58\u788e\u7247\u7684\u5b57\u8282\u6570\u8fbe\u5230\u6b64\u9600\u503c\uff08\u9ed8\u8ba4100MB\uff09\u65f6\uff0c\u5141\u8bb8\u6574\u7406<\/li>\n<li><strong>active-defrag-threshold-lower<\/strong>\uff1a\u5185\u5b58\u788e\u7247\u7a7a\u95f4\u5360\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7ed9 Redis \u7684\u603b\u7a7a\u95f4\u6bd4\u4f8b\u8fbe\u5230\u6b64\u9600\u503c\uff08\u9ed8\u8ba410%\uff09\u65f6\uff0c\u5141\u8bb8\u6574\u7406<\/li>\n<\/ul>\n<p>\u6b64\u5916\uff0c\u8fd8\u6709\u51e0\u4e2a\u53c2\u6570\u7528\u4e8e<strong>\u63a7\u5236\u5185\u5b58\u788e\u7247\u6574\u7406\u7684\u529b\u5ea6<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>active-defrag-cycle-min<\/strong>\uff1a\u6e05\u7406\u5185\u5b58\u788e\u7247\u5360\u7528 CPU \u65f6\u95f4\u7684\u6bd4\u4f8b\u4e0d\u4f4e\u4e8e\u6b64\u9600\u503c\uff08\u9ed8\u8ba45%\uff09\uff0c\u4fdd\u8bc1\u6e05\u7406\u80fd\u6b63\u5e38\u5f00\u5c55<\/li>\n<li><strong>active-defrag-cycle-max<\/strong>\uff1a\u6e05\u7406\u5185\u5b58\u788e\u7247\u5360\u7528CPU \u65f6\u95f4\u7684\u6bd4\u4f8b\u4e0d\u9ad8\u4e8e\u6b64\u9600\u503c\uff08\u9ed8\u8ba475%\uff09\uff0c\u4e00\u65e6\u8d85\u8fc7\u5219\u505c\u6b62\u6e05\u7406\uff0c\u4ece\u800c\u907f\u514d\u5728\u6e05\u7406\u65f6\uff0c\u5927\u91cf\u7684\u5185\u5b58\u62f7\u8d1d\u963b\u585e Redis\uff0c\u5bfc\u81f4\u5176\u4ed6\u8bf7\u6c42\u5ef6\u8fdf<\/li>\n<\/ul>\n<p><strong>\u5185\u5b58\u788e\u7247\u6574\u7406\u5176\u4ed6\u53c2\u6570<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>active-defrag-threshold-upper<\/strong>\uff1a\u5185\u5b58\u788e\u7247\u7a7a\u95f4\u5360\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7ed9 Redis \u7684\u603b\u7a7a\u95f4\u6bd4\u4f8b\u8fbe\u5230\u6b64\u9600\u503c\uff08\u9ed8\u8ba4100%\uff09\u65f6\uff0c\u5219\u5c3d\u6700\u5927\u52aa\u529b\u6574\u7406<\/li>\n<li><strong>active-defrag-max-scan-fields<\/strong>\uff1a\u788e\u7247\u6574\u7406 \u626b\u63cfset\/hash\/zset\/list\u65f6\uff0c\u4ec5\u5f53 set\/hash\/zset\/list \u7684\u957f\u5ea6\u5c0f\u4e8e\u6b64\u9600\u503c\u65f6\uff0c\u624d\u4f1a\u5c06\u6b64key\u52a0\u5165\u788e\u7247\u6574\u7406<\/li>\n<\/ul>\n<h2>\u751f\u4ea7\u73af\u5883\u6574\u7406\u5185\u5b58\u788e\u7247\u7684\u6ce8\u610f\u4e8b\u9879<\/h2>\n<h3>\u6e05\u7406\u5185\u5b58\u788e\u7247\u7684\u65f6\u673a<\/h3>\n<p>\u901a\u8fc7<code>info memory<\/code>\u547d\u4ee4\u67e5\u770b<code>mem_fragmentation_ratio<\/code>\uff08\u5185\u5b58\u788e\u7247\u7387\uff09\uff0c\u5f53<code>mem_fragmentation_ratio &gt; 1.5<\/code>\u65f6\uff0c\u5efa\u8bae\u5f00\u59cb\u6e05\u7406\u5185\u5b58\u788e\u7247\u3002\u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u8c03\u6574<code>activedefrag<\/code>\u7684\u53c2\u6570\u914d\u7f6e\u4ece\u800c\u8fbe\u5230\u81ea\u52a8\u6e05\u7406\u6548\u679c\u3002<\/p>\n<h3>\u6e05\u7406\u5185\u5b58\u788e\u7247\u524d\u4f60\u5fc5\u987b\u4e86\u89e3\u7684\u4e8b<\/h3>\n<ul>\n<li><strong>memory purge<\/strong>\uff1a\u624b\u52a8\u6574\u7406\u5185\u5b58\u788e\u7247\uff0c\u4f1a\u963b\u585e\u4e3b\u8fdb\u7a0b\uff0c\u751f\u4ea7\u73af\u5883\u614e\u7528\uff0c\u6e05\u7406\u6548\u679c\u548c<code>activedefrag<\/code>\u5e76\u4e0d\u76f8\u540c<\/li>\n<li><strong>activedefrag<\/strong>\uff1a\u81ea\u52a8\u6574\u7406\u5185\u5b58\u788e\u7247\uff0c\u5176\u539f\u7406\u662f\u901a\u8fc7scan\u8fed\u4ee3\u6574\u4e2aRedis\u6570\u636e\uff0c\u901a\u8fc7\u4e00\u7cfb\u5217\u7684\u5185\u5b58\u590d\u5236\u3001\u8f6c\u79fb\u64cd\u4f5c\u5b8c\u6210\u5185\u5b58\u788e\u7247\u6574\u7406\uff0c\u7531\u4e8e\u6b64\u64cd\u4f5c\u4f7f\u7528\u7684\u662f\u4e3b\u7ebf\u7a0b\uff0c\u6545\u4f1a\u5f71\u54cdRedis\u5bf9\u5176\u4ed6\u8bf7\u6c42\u7684\u54cd\u5e94<\/li>\n<\/ul>\n<p>\u5728Redis\u65e5\u5fd7\u4e2d\uff0c\u53ef\u4ee5\u67e5\u770b<code>activedefrag<\/code>\u8017\u65f6\u53ca\u8d44\u6e90\u5360\u7528\u8bb0\u5f55\uff1a<code>Active defrag done in 79214ms<\/code>\u8868\u793a\u8017\u65f6\uff0c\u4f46\u6b64\u8017\u65f6\u5e76\u4e0d\u662f\u963b\u585e\u4e86\u4e3b\u7ebf\u7a0b\u7684\u65f6\u95f4\uff0c\u800c\u662f\u4ece\u5185\u5b58\u788e\u7247\u6574\u7406\u7684\u7b2c\u4e00\u6b21scan\u5230\u6700\u540e\u4e00\u6b21scan\u7684\u65f6\u95f4\u5dee\uff0c\u5728\u6b64\u671f\u95f4\uff0c\u4e3b\u7ebf\u7a0b\u662f\u53ef\u4ee5\u5904\u7406\u5176\u4ed6\u8bf7\u6c42\u7684\u3002<\/p>\n<pre><code class=\"language-bash\">12:M 21 May 12:31:11.210 - Starting active defrag, frag=12%, frag_bytes=381401201, cpu=75%\n12:M 21 May 12:32:30.525 - Active defrag done in 79214ms, reallocated=50, frag=12%, frag_bytes=380061210<\/code><\/pre>\n<blockquote>\n<p>\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/zxiaofan.blog.csdn.net\/article\/details\/108970303\">https:\/\/zxiaofan.blog.csdn.net\/article\/details\/108970303<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u516c\u53f8\u67d0\u4e1a\u52a1\u4f7f\u7528\u7684Redis\u96c6\u7fa4\u662f\u81ea\u5efa\u7684\uff0c\u524d\u6bb5\u65f6\u95f4\u8ba1\u5212\u5c06\u81ea\u5efaRedis\u96c6\u7fa4\u8fc1\u79fb\u5230\u8d2d\u4e70\u7684\u963f\u91cc\u4e91\u96c6\u7fa4\u3002 \u8001\u96c6\u7fa4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[504],"class_list":["post-2079","post","type-post","status-publish","format-standard","hentry","category-redis","tag-hyperloglog"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2079"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2079\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}