{"id":252398,"date":"2022-09-19T09:05:00","date_gmt":"2022-09-19T01:05:00","guid":{"rendered":"https:\/\/gulass.cn\/?p=252398"},"modified":"2022-09-14T22:05:44","modified_gmt":"2022-09-14T14:05:44","slug":"redis-memory","status":"publish","type":"post","link":"https:\/\/gulass.cn\/redis-memory.html","title":{"rendered":"Redis \u7684\u6570\u636e\u88ab\u5220\u9664\u8fd8\u5360\u5185\u5b58\u5417?"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u5982\u679c\u4f60\u53d1\u73b0\u660e\u660e Redis \u5b58\u50a8\u6570\u636e\u7684\u5185\u5b58\u5360\u7528\u8fdc\u5c0f\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7ed9 Redis \u7684\u5185\u5b58\uff0c\u800c\u53c8\u65e0\u6cd5\u4fdd\u5b58\u6570\u636e\uff0c\u90a3\u53ef\u80fd\u51fa\u73b0\u5927\u91cf\u5185\u5b58\u788e\u7247\u4e86\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/p>\n \u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u7ed9 Redis \u7684\u5185\u5b58\u6709 6GB\uff0c\u901a\u8fc7\u6307\u6807 used_memory_human \u53d1\u73b0\u5b58\u50a8\u6570\u636e\u53ea\u4f7f\u7528\u4e86 4GB\uff0c\u4e3a\u4f55\u4f1a\u8fd9\u6837\uff1f\u4e3a\u4f55\u65e0\u6cd5\u4fdd\u5b58\u6570\u636e\uff1f<\/p>\n \u901a\u8fc7 CONFIG SET maxmemory 100mb\u6216\u8005\u5728 redis.conf \u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e maxmemory 100mb Redis \u5185\u5b58\u5360\u7528\u9650\u5236\u3002\u5f53\u8fbe\u5230\u5185\u5b58\u6700\u5927\u503c\uff0c\u4f1a\u89e6\u53d1\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\u5220\u9664\u6570\u636e\u3002<\/p>\n \u9664\u6b64\u4e4b\u5916\uff0c\u5f53 key \u8fbe\u5230\u8fc7\u671f\u65f6\u95f4\uff0cRedis \u4f1a\u6709\u4ee5\u4e0b\u4e24\u79cd\u5220\u9664\u8fc7\u671f\u6570\u636e\u7684\u7b56\u7565\uff1a<\/p>\n \u540e\u53f0\u5b9a\u65f6\u4efb\u52a1\u9009\u53d6\u90e8\u5206\u6570\u636e\u5220\u9664\uff1b \u5047\u8bbe Redis \u5b9e\u4f8b\u4fdd\u5b58\u4e86 5GB \u7684\u6570\u636e\uff0c\u73b0\u5728\u5220\u9664\u4e86 2GB \u6570\u636e\uff0cRedis \u8fdb\u7a0b\u5360\u7528\u7684\u5185\u5b58\u4e00\u5b9a\u4f1a\u964d\u4f4e\u4e48\uff1f\uff08\u4e5f\u53eb\u505a RSS\uff0c\u8fdb\u7a0b\u6d88\u8017\u5185\u5b58\u9875\u6570\uff09\u3002<\/p>\n \u7b54\u6848\u662f\uff1a\u53ef\u80fd\u4f9d\u7136\u5360\u7528\u4e86\u5927\u7ea6 5GB \u7684\u5185\u5b58\uff0c\u5373\u4f7f Redis \u7684\u6570\u636e\u53ea\u5360\u7528\u4e86 3GB \u5de6\u53f3\u3002<\/p>\n \u5927\u5bb6\u4e00\u5b9a\u8981\u8bbe\u7f6emaxmemory\uff0c\u5426\u5219 Redis \u4f1a\u7ee7\u7eed\u4e3a\u65b0\u5199\u5165\u7684\u6570\u636e\u5206\u914d\u5185\u5b58\uff0c\u65e0\u6cd5\u5206\u914d\u5c31\u4f1a\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u62a5\u9519\uff0c\u5f53\u7136\u4e0d\u4f1a\u5bfc\u81f4\u5b95\u673a\u3002<\/p>\n \u91ca\u653e\u7684\u5185\u5b58\u53bb\u54ea\u4e86<\/strong><\/div>\n \u660e\u660e\u5220\u9664\u4e86\u6570\u636e\uff0c\u4f7f\u7528 top \u547d\u4ee4\u67e5\u770b\uff0c\u4e3a\u4f55\u8fd8\u662f\u5360\u7528\u4e86\u90a3\u4e48\u591a\u5185\u5b58\uff1f<\/p>\n \u5185\u5b58\u90fd\u53bb\u54ea\u4e86\uff1f\u4f7f\u7528 info memory \u547d\u4ee4\u83b7\u53d6 Redis \u5185\u5b58\u76f8\u5173\u6307\u6807\uff0c\u6211\u5217\u4e3e\u4e86\u51e0\u4e2a\u91cd\u8981\u7684\u6570\u636e\uff1a<\/p>\n 127.0.0.1:6379> info memory\r\n# Memory\r\nused_memory:1132832 \/\/ Redis \u5b58\u50a8\u6570\u636e\u5360\u7528\u7684\u5185\u5b58\u91cf\r\nused_memory_human:1.08M \/\/ \u4eba\u7c7b\u53ef\u8bfb\u5f62\u5f0f\u8fd4\u56de\u5185\u5b58\u603b\u91cf\r\nused_memory_rss:2977792 \/\/ \u64cd\u4f5c\u7cfb\u7edf\u89d2\u5ea6\uff0c\u8fdb\u7a0b\u5360\u7528\u7684\u7269\u7406\u603b\u5185\u5b58\r\nused_memory_rss_human:2.84M \/\/ used_memory_rss \u53ef\u8bfb\u6027\u6a21\u5f0f\u5c55\u793a\r\nused_memory_peak:1183808 \/\/ \u5185\u5b58\u4f7f\u7528\u7684\u6700\u5927\u503c\uff0c\u8868\u793a used_memory \u7684\u5cf0\u503c\r\nused_memory_peak_human:1.13M \/\/ \u4ee5\u53ef\u8bfb\u7684\u683c\u5f0f\u8fd4\u56de used_memory_peak\u7684\u503c\r\nused_memory_lua:37888 \/\/ Lua \u5f15\u64ce\u6240\u6d88\u8017\u7684\u5185\u5b58\u5927\u5c0f\u3002\r\nused_memory_lua_human:37.00K\r\nmaxmemory:2147483648 \/\/ \u80fd\u4f7f\u7528\u7684\u6700\u5927\u5185\u5b58\u503c\uff0c\u5b57\u8282\u4e3a\u5355\u4f4d\u3002\r\nmaxmemory_human:2.00G \/\/ \u53ef\u8bfb\u5f62\u5f0f\r\nmaxmemory_policy:noeviction \/\/ \u5185\u5b58\u6dd8\u6c70\u7b56\u7565\r\n\r\n\/\/ used_memory_rss \/ used_memory \u7684\u6bd4\u503c\uff0c\u4ee3\u8868\u5185\u5b58\u788e\u7247\u7387\r\nmem_fragmentation_ratio:2.79<\/pre>\n \n\u5185\u5b58\u788e\u7247\u3002<\/li>\n <\/p>\n Redis \u81ea\u8eab\u7a7a\u8fdb\u7a0b\u5360\u7528\u7684\u5185\u5b58\u5f88\u5c0f\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\uff0c\u5bf9\u8c61\u5185\u5b58\u662f\u5360\u6bd4\u6700\u5927\u7684\u4e00\u5757\uff0c\u91cc\u9762\u5b58\u50a8\u7740\u6240\u6709\u7684\u6570\u636e\u3002<\/p>\n \u7f13\u51b2\u533a\u5185\u5b58\u5728\u5927\u6d41\u91cf\u573a\u666f\u5bb9\u6613\u5931\u63a7\uff0c\u9020\u6210 Redis \u5185\u5b58\u4e0d\u7a33\u5b9a\uff0c\u9700\u8981\u91cd\u70b9\u5173\u6ce8\u3002<\/p>\n \u5185\u5b58\u788e\u7247\u8fc7\u5927\u4f1a\u5bfc\u81f4\u660e\u660e\u6709\u7a7a\u95f4\u53ef\u7528\uff0c\u4f46\u662f\u5374\u65e0\u6cd5\u5b58\u50a8\u6570\u636e\u3002<\/p>\n \u788e\u7247 = used_memory_rss \u5b9e\u9645\u4f7f\u7528\u7684\u7269\u7406\u5185\u5b58\uff08RSS \u503c\uff09\u9664\u4ee5 used_memory \u5b9e\u9645\u5b58\u50a8\u6570\u636e\u5185\u5b58\u3002<\/p>\n \u4ec0\u4e48\u662f\u5185\u5b58\u788e\u7247<\/strong><\/div>\n \u5185\u5b58\u788e\u7247\u4f1a\u9020\u6210\u660e\u660e\u6709\u5185\u5b58\u7a7a\u95f4\u7a7a\u95f2\uff0c\u53ef\u662f\u5374\u65e0\u6cd5\u5b58\u50a8\u6570\u636e\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4f60\u8ddf\u6f02\u4eae\u5c0f\u59d0\u59d0\u53bb\u7535\u5f71\u9662\u770b\u7535\u5f71\uff0c\u80af\u5b9a\u60f3\u8fde\u5728\u4e00\u5757\u3002<\/p>\n \u5047\u8bbe\u73b0\u5728\u6709 8 \u4e2a\u5ea7\u4f4d\uff0c\u5df2\u7ecf\u5356\u51fa\u4e86 4 \u5f20\u7968\uff0c\u8fd8\u6709 4 \u5f20\u53ef\u4ee5\u4e70\u3002\u53ef\u662f\u597d\u5de7\u4e0d\u5de7\uff0c\u4e70\u7968\u7684\u4eba\u5f88\u5947\u8469\uff0c\u5206\u522b\u95f4\u9694\u4e00\u4e2a\u5ea7\u4f4d\u4e70\u7968\u3002<\/p>\n \u5373\u4f7f\u8fd8\u6709 4 \u4e2a\u5ea7\u4f4d\u7a7a\u95f2\uff0c\u53ef\u662f\u4f60\u5374\u4e70\u4e0d\u5230\u4e24\u4e2a\u5ea7\u4f4d\u8fde\u5728\u4e00\u5757\u7684\u7968\uff0c\u539a\u793c\u87f9\uff01<\/p>\n <\/p>\n \u5185\u5b58\u788e\u7247\u5f62\u6210\u539f\u56e0<\/strong><\/div>\n \u5185\u5b58\u788e\u7247\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\u5462\uff1f<\/p>\n \u4e3b\u8981\u6709\u4e24\u4e2a\u539f\u56e0\uff1a<\/p>\n \u63a5\u4e0b\u6765\u6211\u5206\u522b\u63a2\u8ba8\u5b9e\u9645\u53d1\u751f\u7684\u539f\u56e0\u2026\u2026<\/p>\n \u5185\u5b58\u5206\u914d\u5668\u7684\u5206\u914d\u7b56\u7565<\/strong><\/div>\n Redis \u9ed8\u8ba4\u7684\u5185\u5b58\u5206\u914d\u5668\u91c7\u7528 jemalloc\uff0c\u53ef\u9009\u7684\u5206\u914d\u5668\u8fd8\u6709\uff1aglibc\u3001tcmalloc\u3002<\/p>\n \u5185\u5b58\u5206\u914d\u5668\u5e76\u4e0d\u80fd\u505a\u5230\u6309\u9700\u5206\u914d\uff0c\u800c\u662f\u91c7\u7528\u56fa\u5b9a\u8303\u56f4\u7684\u5185\u5b58\u5757\u8fdb\u884c\u5206\u914d\u3002<\/p>\n \u4f8b\u5982 8 \u5b57\u8282\u300116 \u5b57\u8282\u2026..\uff0c2 KB\uff0c4KB\uff0c\u5f53\u7533\u8bf7\u5185\u5b58\u6700\u8fd1\u63a5\u67d0\u4e2a\u56fa\u5b9a\u503c\u7684\u65f6\u5019\uff0cjemalloc \u4f1a\u7ed9\u5b83\u5206\u914d\u6700\u63a5\u8fd1\u56fa\u5b9a\u503c\u5927\u5c0f\u7684\u7a7a\u95f4\u3002<\/p>\n \u8fd9\u6837\u5c31\u4f1a\u51fa\u73b0\u5185\u5b58\u788e\u7247\uff0c\u6bd4\u5982\u7a0b\u5e8f\u53ea\u9700\u8981 1.5 KB\uff0c\u5185\u5b58\u5206\u914d\u5668\u4f1a\u5206\u914d 2KB \u7a7a\u95f4\uff0c\u90a3\u4e48\u8fd9 0.5KB \u5c31\u662f\u788e\u7247\u3002<\/p>\n \u200b\u8fd9\u4e48\u505a\u7684\u76ee\u7684\u662f\u51cf\u5c11\u5185\u5b58\u5206\u914d\u6b21\u6570\uff0c\u6bd4\u5982\u7533\u8bf7 22 \u5b57\u8282\u7684\u7a7a\u95f4\u4fdd\u5b58\u6570\u636e\uff0cjemalloc \u5c31\u4f1a\u5206\u914d 32 \u5b57\u8282\uff0c\u5982\u679c\u540e\u8fb9\u8fd8\u8981\u5199\u5165 10 \u5b57\u8282\uff0c\u5c31\u4e0d\u9700\u8981\u518d\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u7a7a\u95f4\u4e86\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e4b\u524d\u7533\u8bf7\u7684 32 \u5b57\u8282\u3002<\/p>\n \u5220\u9664 key \u7684\u65f6\u5019\uff0cRedis \u5e76\u4e0d\u4f1a\u7acb\u9a6c\u628a\u5185\u5b58\u5f52\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u51fa\u73b0\u8fd9\u4e2a\u60c5\u51b5\u662f\u56e0\u4e3a\u5e95\u5c42\u5185\u5b58\u5206\u914d\u5668\u7ba1\u7406\u5bfc\u81f4\uff0c\u6bd4\u5982\u5927\u591a\u6570\u5df2\u7ecf\u5220\u9664\u7684 key \u4f9d\u7136\u4e0e\u5176\u4ed6\u6709\u6548\u7684 key \u5206\u914d\u5728\u540c\u4e00\u4e2a\u5185\u5b58\u9875\u4e2d\u3002<\/p>\n \u53e6\u5916\uff0c\u5206\u914d\u5668\u4e3a\u4e86\u590d\u7528\u7a7a\u95f2\u7684\u5185\u5b58\u5757\uff0c\u539f\u6709 5GB \u7684\u6570\u636e\u4e2d\u5220\u9664\u4e86 2 GB \u540e\uff0c\u5f53\u518d\u6b21\u6dfb\u52a0\u6570\u636e\u5230\u5b9e\u4f8b\u4e2d\uff0cRedis \u7684 RSS \u4f1a\u4fdd\u6301\u7a33\u5b9a\uff0c\u4e0d\u4f1a\u589e\u957f\u592a\u591a\u3002<\/p>\n \u56e0\u4e3a\u5185\u5b58\u5206\u914d\u5668\u57fa\u672c\u4e0a\u590d\u7528\u4e86\u4e4b\u524d\u5220\u9664\u91ca\u653e\u51fa\u6765\u7684 2GB \u5185\u5b58\u3002\u200b<\/p>\n \u952e\u503c\u5bf9\u5927\u5c0f\u4e0d\u4e00\u6837\u548c\u5220\u6539\u64cd\u4f5c<\/strong><\/div>\n \u7531\u4e8e\u5185\u5b58\u5206\u914d\u5668\u662f\u6309\u7167\u56fa\u5b9a\u5927\u5c0f\u5206\u914d\u5185\u5b58\uff0c\u6240\u4ee5\u901a\u5e38\u5206\u914d\u7684\u5185\u5b58\u7a7a\u95f4\u6bd4\u5b9e\u9645\u6570\u636e\u5360\u7528\u7684\u5927\u5c0f\u591a\u4e00\u4e9b\uff0c\u4f1a\u9020\u6210\u788e\u7247\uff0c\u964d\u4f4e\u5185\u5b58\u7684\u5b58\u50a8\u6548\u7387\u3002<\/p>\n \u53e6\u5916\uff0c\u952e\u503c\u5bf9\u7684\u9891\u7e41\u4fee\u6539\u548c\u5220\u9664\uff0c\u5bfc\u81f4\u5185\u5b58\u7a7a\u95f4\u7684\u6269\u5bb9\u548c\u91ca\u653e\uff0c\u6bd4\u5982\u539f\u672c\u5360\u7528 32 \u5b57\u8282\u7684\u5b57\u7b26\u4e32\uff0c\u73b0\u5728\u4fee\u6539\u4e3a\u5360\u7528 20 \u5b57\u8282\u7684\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u91ca\u653e\u51fa\u7684 12 \u5b57\u8282\u5c31\u662f\u7a7a\u95f2\u7a7a\u95f4\u3002<\/p>\n \u5982\u679c\u4e0b\u4e00\u4e2a\u6570\u636e\u5b58\u50a8\u8bf7\u6c42\u9700\u8981\u7533\u8bf7 13 \u5b57\u8282\u7684\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u521a\u521a\u91ca\u653e\u7684 12 \u5b57\u8282\u7a7a\u95f4\u65e0\u6cd5\u4f7f\u7528\uff0c\u5bfc\u81f4\u788e\u7247\u3002<\/p>\n \u788e\u7247\u6700\u5927\u7684\u95ee\u9898\uff1a\u7a7a\u95f4\u603b\u91cf\u8db3\u591f\u5927\uff0c\u4f46\u662f\u8fd9\u4e9b\u5185\u5b58\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u53ef\u80fd\u5927\u81f4\u65e0\u6cd5\u5b58\u50a8\u6570\u636e\u3002<\/p>\n \u5185\u5b58\u788e\u7247\u89e3\u51b3\u4e4b\u9053<\/strong><\/div>\n \u90a3\u8be5\u5982\u4f55\u89e3\u51b3\u5462\uff1f<\/p>\n \u9996\u5148\u8981\u786e\u5b9a\u662f\u5426\u53d1\u751f\u4e86\u5185\u5b58\u788e\u7247\uff0c\u91cd\u70b9\u5173\u6ce8\u524d\u9762 INFO memory \u547d\u4ee4\u63d0\u793a\u7684 mem_fragmentation_ratio \u6307\u6807\uff0c\u8868\u793a\u5185\u5b58\u788e\u7247\u7387\uff1a<\/p>\n mem_fragmentation_ratio = used_memory_rss\/ used_memory<\/pre>\n |