{"id":201394,"date":"2025-05-10T11:34:15","date_gmt":"2025-05-10T03:34:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201394\/"},"modified":"2025-05-10T11:34:15","modified_gmt":"2025-05-10T03:34:15","slug":"%e6%b5%85%e8%b0%88redis%e9%87%87%e7%94%a8%e4%b8%8d%e5%90%8c%e5%86%85%e5%ad%98%e5%88%86%e9%85%8d%e5%99%a8tcmalloc%e5%92%8cjemalloc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201394\/","title":{"rendered":"\u6d45\u8c08redis\u91c7\u7528\u4e0d\u540c\u5185\u5b58\u5206\u914d\u5668tcmalloc\u548cjemalloc"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u6d45\u8c08redis\u91c7\u7528\u4e0d\u540c\u5185\u5b58\u5206\u914d\u5668tcmalloc\u548cjemalloc<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u6d45\u8c08redis\u91c7\u7528\u4e0d\u540c\u5185\u5b58\u5206\u914d\u5668tcmalloc\u548cjemalloc\u300b\uff0c\u8986\u76d6\u4e86\u6570\u636e\u5e93\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>\u6211\u4eec\u77e5\u9053Redis\u5e76\u6ca1\u6709\u81ea\u5df1\u5b9e\u73b0\u5185\u5b58\u6c60\uff0c\u6ca1\u6709\u5728\u6807\u51c6\u7684\u7cfb\u7edf\u5185\u5b58\u5206\u914d\u5668\u4e0a\u518d\u52a0\u4e0a\u81ea\u5df1\u7684\u4e1c\u897f\u3002\u6240\u4ee5\u7cfb\u7edf\u5185\u5b58\u5206\u914d\u5668\u7684\u6027\u80fd\u53ca\u788e\u7247\u7387\u4f1a\u5bf9Redis\u9020\u6210\u4e00\u4e9b\u6027\u80fd\u4e0a\u7684\u5f71\u54cd\u3002<\/p>\n<p>\u5728Redis\u7684 zmalloc.c \u6e90\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>\n\/* Double expansion needed for stringification of macro values. *\/ \n#define __xstr(s) __str(s) \n#define __str(s) #s \n#if defined(USE_TCMALLOC) \n#define ZMALLOC_LIB (\"tcmalloc-\" __xstr(TC_VERSION_MAJOR) \".\" __xstr(TC_VERSION_MINOR)) \n#include \n   \n     \n#if (TC_VERSION_MAJOR == 1 &amp;&amp; TC_VERSION_MINOR &gt;= 6) || (TC_VERSION_MAJOR &gt; 1) \n#define HAVE_MALLOC_SIZE 1 \n#define zmalloc_size(p) tc_malloc_size(p) \n#else \n#error \"Newer version of tcmalloc required\" \n#endif \n#elif defined(USE_JEMALLOC) \n#define ZMALLOC_LIB (\"jemalloc-\" __xstr(JEMALLOC_VERSION_MAJOR) \".\" __xstr(JEMALLOC_VERSION_MINOR) \".\" __xstr(JEMALLOC_VERSION_BUGFIX)) \n#include \n    \n      #if (JEMALLOC_VERSION_MAJOR == 2 &amp;&amp; JEMALLOC_VERSION_MINOR &gt;= 1) || (JEMALLOC_VERSION_MAJOR &gt; 2) #define HAVE_MALLOC_SIZE 1 #define zmalloc_size(p) je_malloc_usable_size(p) #else #error \"Newer version of jemalloc required\" #endif #elif defined(__APPLE__) #include \n     \n       #define HAVE_MALLOC_SIZE 1 #define zmalloc_size(p) malloc_size(p) #endif #ifndef ZMALLOC_LIB #define ZMALLOC_LIB \"libc\" #endif \n     \n    \n   <\/pre>\n<p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cRedis\u5728\u7f16\u8bd1\u65f6\uff0c\u4f1a\u5148\u5224\u65ad\u662f\u5426\u4f7f\u7528tcmalloc\uff0c\u5982\u679c\u662f\uff0c\u4f1a\u7528tcmalloc\u5bf9\u5e94\u7684\u51fd\u6570\u66ff\u6362\u6389\u6807\u51c6\u7684libc\u4e2d\u7684\u51fd\u6570\u5b9e\u73b0\u3002\u5176\u6b21\u4f1a\u5224\u65adjemalloc\u662f\u5426\u4f7f\u5f97\uff0c\u6700\u540e\u5982\u679c\u90fd\u6ca1\u6709\u4f7f\u7528\u624d\u4f1a\u7528\u6807\u51c6\u7684libc\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u51fd\u6570\u3002<\/p>\n<p>\u800c\u5728\u6700\u65b0\u76842.4.4\u7248\u672c\u4e2d\uff0cjemalloc\u5df2\u7ecf\u4f5c\u4e3a\u6e90\u7801\u5305\u7684\u4e00\u90e8\u5206\u5305\u542b\u5728\u6e90\u7801\u5305\u4e2d\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u88ab\u4f7f\u7528\u3002\u800c\u5982\u679c\u4f60\u8981\u4f7f\u7528tcmalloc\u7684\u8bdd\uff0c\u662f\u9700\u8981\u81ea\u5df1\u5b89\u88c5\u7684\u3002<\/p>\n<p>\u4e0b\u9762\u7b80\u5355\u8bf4\u4e00\u4e0b\u5982\u4f55\u5b89\u88c5tcmalloc\u5305\uff0ctcmalloc\u662fgoogle-proftools\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u6211\u4eec\u5b9e\u9645\u4e0a\u9700\u8981\u5b89\u88c5google-proftools\u3002\u5982\u679c\u4f60\u662f\u572864\u4f4d\u673a\u5668\u4e0a\u8fdb\u884c\u5b89\u88c5\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5176\u4f9d\u8d56\u7684libunwind\u5e93\u3002<\/p>\n<p>wget http:\/\/download.savannah.gnu.org\/releases\/libunwind\/libunwind-0.99-alpha.tar.gz tar zxvf libunwind-0.99-alpha.tar.gz cd libunwind-0.99-alpha\/ CFLAGS=-fPIC .\/configure make CFLAGS=-fPIC make CFLAGS=-fPIC install<\/p>\n<p>\u7136\u540e\u518d\u8fdb\u884cgoogle-preftools\u7684\u5b89\u88c5\uff1a<\/p>\n<p>wget http:\/\/google-perftools.googlecode.com\/files\/google-perftools-1.8.1.tar.gz tar zxvf google-perftools-1.8.1.tar.gz cd google-perftools-1.8.1\/ .\/configure &#8211;disable-cpu-profiler &#8211;disable-heap-profiler &#8211;disable-heap-checker &#8211;disable-debugalloc &#8211;enable-minimal make &amp;&amp; make install sudo echo &#8220;\/usr\/local\/lib&#8221; &gt; \/etc\/ld.so.conf.d\/usr_local_lib.conf #\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u6587\u4ef6\uff0c\u81ea\u5df1\u5efa\u4e00\u4e2a sudo \/sbin\/ldconfig<\/p>\n<p>\u7136\u540e\u518d\u8fdb\u884cRedis\u7684\u5b89\u88c5\uff0c\u5728make\u65f6\u6307\u5b9a\u76f8\u5e94\u7684\u53c2\u6570\u4ee5\u542f\u7528tcmalloc<\/p>\n<p>$ curl -O http:\/\/redis.googlecode.com\/files\/redis-2.4.4.tar.gz $ tar xzvf redis-2.4.4.tar.gz $ cd redis-2.4.4 $ make USE_TCMALLOC=yes FORCE_LIBC_MALLOC=yes $ sudo make install<\/p>\n<p>\u518d\u542f\u52a8Redis\u540e\u901a\u8fc7info\u547d\u4ee4\u5c31\u80fd\u770b\u5230\u4f7f\u7528\u7684\u5185\u5b58\u5206\u914d\u5668\u4e86\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>\u4e0b\u9762\u56de\u5230\u672c\u6587\u7684\u4e3b\u9898\uff0c\u5bf9\u4e8etcmalloc\uff0cjemalloc\u548clibc\u5bf9\u5e94\u7684\u4e09\u4e2a\u5185\u5b58\u5206\u914d\u5668\u3002\u5176\u6027\u80fd\u548c\u788e\u7247\u7387\u5982\u4f55\u5462\uff1f<\/strong><\/span><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u6d4b\u8bd5\u7ed3\u679c\uff0c\u4f7f\u7528Redis\u81ea\u5e26\u7684redis-benchmark\u5199\u5165\u7b49\u91cf\u6570\u636e\u8fdb\u884c\u6d4b\u8bd5\uff0c\u6570\u636e\u6458\u81ea\u91c7\u7528\u4e0d\u540c\u5206\u914d\u5668\u65f6Redis info\u4fe1\u606f\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u91c7\u7528tcmalloc\u65f6\u788e\u7247\u7387\u662f\u6700\u4f4e\u7684\uff0c\u4e3a1.01\uff0cjemalloc\u4e3a1.02\uff0c\u800clibc\u7684\u5206\u914d\u5668\u788e\u7247\u7387\u4e3a1.31\uff0c\u5982\u4e0b\u6240\u672a\uff1a<\/p>\n<p>used_memory:708391440 used_menory_human:675.57M used_memory_rss:715169792 used_memory_peak:708814040 used_memory_peak_human:675.98M mem_fragmentation_ratio:1.01mem_allocator:tcmalloc-1.7<\/p>\n<p>used_memory:708381168 used_menory_human:675.56M used_memory_rss:723587072 used_memory_peak:708803768 used_memory_peak_human:675.97M mem_fragmentation_ratio:1.02mem_allocator:jemalloc-2.2.1<\/p>\n<p>used_memory:869000400 used_menory_human:828.74M used_memory_rss:1136689152 used_memory_peak:868992208 used_memory_peak_human:828.74M mem_fragmentation_ratio:1.31mem_allocator:libc<\/p>\n<p><span style=\"background-color: #ccffcc\"><strong>\u4e0a\u9762\u7684\u6d4b\u8bd5\u6570\u636e\u90fd\u662f\u5c0f\u6570\u636e\uff0c\u4e5f\u5c31\u662f\u8bf4\u5355\u6761\u6570\u636e\u5e76\u4e0d\u5927\uff0c\u4e0b\u9762\u6211\u4eec\u5c1d\u8bd5\u8bbe\u7f6ebenchmark\u7684-d\u53c2\u6570\uff0c\u5c06value\u503c\u8c03\u6574\u4e3a1k\u5927\u5c0f\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53d1\u751f\u4e86\u4e00\u4e9b\u53d8\u5316\uff1a<\/strong><\/span><\/p>\n<p>used_memory:830573680 used_memory_human:792.10M used_memory_rss:849068032 used_memory_peak:831436048 used_memory_peak_human:792.92M mem_fragmentation_ratio:1.02mem_allocator:tcmalloc-1.7<\/p>\n<p>used_memory:915911024 used_memory_human:873.48M used_memory_rss:927047680 used_memory_peak:916773392 used_memory_peak_human:874.30M mem_fragmentation_ratio:1.01mem_allocator:jemalloc-2.2.1<\/p>\n<p>used_memory:771963304 used_memory_human:736.20M used_memory_rss:800583680 used_memory_peak:772784056 used_memory_peak_human:736.98M mem_fragmentation_ratio:1.04mem_allocator:libc<\/p>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5728\u5206\u914d\u5927\u5757\u5185\u5b58\u548c\u5c0f\u5757\u5185\u5b58\u4e0a\uff0c\u51e0\u79cd\u5206\u914d\u5668\u7684\u788e\u7247\u7387\u5dee\u8ddd\u8fd8\u662f\u6bd4\u8f83\u5927\u7684\uff0c\u5927\u5bb6\u5728\u4f7f\u7528Redis\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u5c3d\u91cf\u7528\u81ea\u5df1\u771f\u5b9e\u7684\u6570\u636e\u53bb\u505a\u6d4b\u8bd5\uff0c\u4ee5\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u6570\u636e\u7684\u5206\u914d\u5668\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d45\u8c08redis\u91c7\u7528\u4e0d\u540c\u5185\u5b58\u5206\u914d\u5668&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201394","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201394"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201394\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}