{"id":4350,"date":"2020-06-16T17:18:30","date_gmt":"2020-06-16T09:18:30","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4350"},"modified":"2020-06-16T17:18:30","modified_gmt":"2020-06-16T09:18:30","slug":"tcmalloc%e7%9a%84%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4350","title":{"rendered":"tcmalloc\u7684\u5de5\u4f5c\u539f\u7406"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><\/div>\n<div class=\"article-info-box\">\n<div class=\"operating\"><a href=\"https:\/\/blog.csdn.net\/sj349781478\/article\/details\/78340340\">\u539f\u6587\u5730\u5740<\/a><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix\">\n<div id=\"content_views\" class=\"htmledit_views\">\n<p><strong>\u539f\u7406<\/strong><\/p>\n<p>tcmalloc\u5c31\u662f\u4e00\u4e2a\u5185\u5b58\u5206\u914d\u5668\uff0c\u7ba1\u7406\u5806\u5185\u5b58\uff0c\u4e3b\u8981\u5f71\u54cdmalloc\u548cfree\uff0c\u7528\u4e8e\u964d\u4f4e\u9891\u7e41\u5206\u914d\u3001\u91ca\u653e\u5185\u5b58\u9020\u6210\u7684\u6027\u80fd\u635f\u8017\uff0c\u5e76\u4e14\u6709\u6548\u5730\u63a7\u5236\u5185\u5b58\u788e\u7247\u3002glibc\u4e2d\u7684\u5185\u5b58\u5206\u914d\u5668\u662fptmalloc2\uff0ctcmalloc\u53f7\u79f0\u8981\u6bd4\u5b83\u5feb\u3002\u4e00\u6b21malloc\u548cfree\u64cd\u4f5c\uff0cptmalloc\u9700\u8981300ns\uff0c\u800ctcmalloc\u53ea\u898150ns\u3002\u540c\u65f6tcmalloc\u4e5f\u4f18\u5316\u4e86\u5c0f\u5bf9\u8c61\u7684\u5b58\u50a8\uff0c\u9700\u8981\u66f4\u5c11\u7684\u7a7a\u95f4\u3002tcmalloc\u7279\u522b\u5bf9\u591a\u7ebf\u7a0b\u505a\u4e86\u4f18\u5316\uff0c\u5bf9\u4e8e\u5c0f\u5bf9\u8c61\u7684\u5206\u914d\u57fa\u672c\u4e0a\u662f\u4e0d\u5b58\u5728\u9501\u7ade\u4e89\uff0c\u800c\u5927\u5bf9\u8c61\u4f7f\u7528\u4e86\u7ec6\u7c92\u5ea6\u3001\u9ad8\u6548\u7684\u81ea\u65cb\u9501\uff08spinlock\uff09\u3002\u5206\u914d\u7ed9\u7ebf\u7a0b\u7684\u672c\u5730\u7f13\u5b58\uff0c\u5728\u957f\u65f6\u95f4\u7a7a\u95f2\u7684\u60c5\u51b5\u4e0b\u4f1a\u88ab\u56de\u6536\uff0c\u4f9b\u5176\u4ed6\u7ebf\u7a0b\u4f7f\u7528\uff0c\u8fd9\u6837\u63d0\u9ad8\u4e86\u5728\u591a\u7ebf\u7a0b\u60c5\u51b5\u4e0b\u7684\u5185\u5b58\u5229\u7528\u7387\uff0c\u4e0d\u4f1a\u6d6a\u8d39\u5185\u5b58\uff0c\u800c\u8fd9\u4e00\u70b9ptmalloc2\u662f\u505a\u4e0d\u5230\u7684\u3002<\/p>\n<p><strong>Tcmalloc\u6982\u8ff0<\/strong><\/p>\n<p>tcmalloc\u5c06\u5185\u5b58\u8bf7\u6c42\u5206\u4e3a\u4e24\u7c7b\uff0c\u5927\u5bf9\u8c61\u8bf7\u6c42\u548c\u5c0f\u5bf9\u8c61\u8bf7\u6c42\uff0c\u5927\u5bf9\u8c61\u4e3a&gt;=32K\u7684\u5bf9\u8c61\u3002<\/p>\n<p>tcmalloc\u4f1a\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u5206\u914d\u672c\u5730\u7f13\u5b58\uff0c\u5c0f\u5bf9\u8c61\u8bf7\u6c42\u53ef\u4ee5\u76f4\u63a5\u4ece\u672c\u5730\u7f13\u5b58\u83b7\u53d6\uff0c\u5982\u679c\u6ca1\u6709\u7a7a\u95f2\u5185\u5b58\uff0c\u5219\u4ececentral heap\u4e2d\u4e00\u6b21\u6027\u83b7\u53d6\u4e00\u8fde\u4e32\u5c0f\u5bf9\u8c61\u3002<\/p>\n<p>tcmalloc\u5bf9\u4e8e\u5c0f\u5185\u5b58\uff0c\u63098\u7684\u6574\u6570\u6b21\u500d\u5206\u914d\uff0c\u5bf9\u4e8e\u5927\u5185\u5b58\uff0c\u63094K\u7684\u6574\u6570\u6b21\u500d\u5206\u914d\u3002<\/p>\n<p>\u5f53\u67d0\u4e2a\u7ebf\u7a0b\u7f13\u5b58\u4e2d\u6240\u6709\u5bf9\u8c61\u7684\u603b\u5927\u5c0f\u8d85\u8fc72MB\u7684\u65f6\u5019\uff0c\u4f1a\u8fdb\u884c\u5783\u573e\u6536\u96c6\u3002\u5783\u573e\u6536\u96c6\u9608\u503c\u4f1a\u81ea\u52a8\u6839\u636e\u7ebf\u7a0b\u6570\u91cf\u7684\u589e\u52a0\u800c\u51cf\u5c11\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u56e0\u4e3a\u7a0b\u5e8f\u6709\u5927\u91cf\u7ebf\u7a0b\u800c\u8fc7\u5ea6\u6d6a\u8d39\u5185\u5b58\u3002<\/p>\n<p><strong>Tcmalloc\u4f18\u52bf<\/strong><\/p>\n<p>1 \u901f\u5ea6\u5feb<\/p>\n<p>ptmalloc\u5728\u4e00\u53f02.8GHz\u7684P4\u673a\u5668\u4e0a\uff08\u5bf9\u4e8e\u5c0f\u5bf9\u8c61\uff09\u6267\u884c\u4e00\u6b21malloc\u53cafree\u5927\u7ea6\u9700\u8981300\u7eb3\u79d2\u3002\u800cTCMalloc\u7684\u7248\u672c\u540c\u6837\u7684\u64cd\u4f5c\u5927\u7ea6\u53ea\u9700\u898150\u7eb3\u79d2\u3002<\/p>\n<p>2 \u964d\u4f4e\u4e86\u9501\u4e89\u7528<\/p>\n<p>TCMalloc\u51cf\u5c11\u4e86\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\u7684\u9501\u4e89\u7528\u60c5\u51b5\u3002\u5bf9\u4e8e\u5c0f\u5bf9\u8c61\uff0c\u51e0\u4e4e\u5df2\u7ecf\u8fbe\u5230\u4e86\u96f6\u4e89\u7528\u3002\u5bf9\u4e8e\u5927\u5bf9\u8c61\uff0cTCMalloc\u5c1d\u8bd5\u4f7f\u7528\u7c92\u5ea6\u8f83\u597d\u548c\u6709\u6548\u7684\u81ea\u65cb\u9501\u3002<\/p>\n<p>3 \u8282\u7701\u5185\u5b58<\/p>\n<p>\u5206\u914dN\u4e2a8\u5b57\u8282\u5bf9\u8c61\u4f7f\u7528\u5927\u7ea68N * 1.01\u5b57\u8282\u7684\u7a7a\u95f4\u3002\u800cptmalloc2\u4e2d\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u4f7f\u7528\u4e86\u4e00\u4e2a\u56db\u5b57\u8282\u7684\u5934\u3002<\/p>\n<p>\u5c3d\u7ba1ptmalloc2\u4e5f\u5b9e\u73b0\u4e86per-thread\u7f13\u5b58\uff0c\uff0c\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u7533\u8bf7\u7684\u5185\u5b58\uff0c\u91ca\u653e\u7684\u65f6\u5019\u8fd8\u662f\u5fc5\u987b\u653e\u5230\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u4e2d\uff08\u4e0d\u53ef\u79fb\u52a8\uff09\uff0c\u8fd9\u6837\u53ef\u80fd\u5bfc\u81f4\u5927\u91cf\u5185\u5b58\u6d6a\u8d39\u3002<\/p>\n<p>ptmalloc2 also reduces lock contention by using per-thread arenas but there is a big problem with ptmalloc2&#8217;s use of per-thread arenas. In ptmalloc2 memory can never move from one arena to another. This can lead to huge amounts of wasted space.<\/p>\n<p>tcmalloc\u8de8\u7ebf\u7a0b\u5f52\u8fd8\u5185\u5b58\uff0c\u662f\u56e0\u4e3a\u6240\u6709\u7ebf\u7a0b\u516c\u7528\u4e86\u5e95\u5c42\u7684\u4e00\u4e2a\u5206\u914d\u5668?\uff0c\u6240\u4ee5\u8de8\u7ebf\u7a0b\u5f52\u8fd8\u662f\u65e0\u9700\u52a0\u9501\u7684\u3002<\/p>\n<p><strong>\u4f7f\u7528<\/strong><\/p>\n<p>\u8981\u4f7f\u7528TCMalloc\uff0c\u53ea\u8981\u5c06tcmalloc\u901a\u8fc7\u201c-ltcmalloc\u201d\u94fe\u63a5\u5668\u6807\u5fd7\u63a5\u5165\u4f60\u7684\u5e94\u7528\u5373\u53ef\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u901a\u8fc7LD_PRELOAD\u52a8\u6001\u52a0\u8f7d\uff1a$ LD_PRELOAD=\u201d\/usr\/lib\/libtcmalloc.so\u201d<\/p>\n<p>&nbsp;<\/p>\n<p>To use TCmalloc, just link tcmalloc into your application via the &#8220;-ltcmalloc&#8221; linker flag.<\/p>\n<p>You can use tcmalloc in applications you didn&#8217;t compile yourself, by using LD_PRELOAD:$ LD_PRELOAD=&#8221;\/usr\/lib\/libtcmalloc.so&#8221;<\/p>\n<p>LD_PRELOAD is tricky, and we don&#8217;t necessarily recommend this mode of usage.<\/p>\n<p>&nbsp;<\/p>\n<p>Mysql\u8981\u4f7f\u7528tcmalloc\uff0c\u53ef\u4ee5\u628a<strong>tcmalloc\u52a8\u6001\u5e93\u52a0\u5230mysqld_safe\u4e2d\u542f\u52a8\uff1b<\/strong><\/p>\n<p><strong>\u4e5f\u53ef\u4e5f\u9759\u6001\u7f16\u8bd1<\/strong>\uff0c\u8981\u4f9d\u6b21\u7f16\u8bd1libunwind\uff0ctcmalloc\uff0c\u7136\u540e\u7f16\u8bd1mysql\uff0cconfigure\u4e2d\u52a0\u5165<strong>\u2013with-mysqld-ldflags=-ltcmalloc\u9009\u9879\uff0c\u5177\u4f53\u53ef\u53c2\u7167<\/strong><\/p>\n<p><a href=\"http:\/\/www.itpub.net\/thread-1363139-1-1.html\" rel=\"nofollow\">http:\/\/www.itpub.net\/thread-1363139-1-1.html#<\/a><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p><strong>\u7533\u8bf7\u5185\u5b58<\/strong><\/p>\n<p>1. \u9996\u5148\u6839\u636e\u7533\u8bf7\u7a7a\u95f4\u7684\u5927\u5c0f\u4ece\u5f53\u524d\u7ebf\u7a0b\u7684\u53ef\u7528\u5185\u5b58\u5757\u91cc\u9762\u627e\uff08\u6bcf\u4e2a\u8fdb\u7a0b\u7ef4\u62a4\u4e00\u7ec4\u94fe\u8868\uff0c\u6bcf\u4e2a\u94fe\u8868\u4ee3\u8868\u4e00\u5b9a\u5927\u5c0f\u7684\u53ef\u7528\u7a7a\u95f4\uff09<\/p>\n<p>2. \u5982\u679cstep 1\u6ca1\u6709\u627e\u5230\uff0c\u5219\u5230central list\u91cc\u9762\u67e5\u627e\uff08central list\u8ddf\u7ebf\u7a0b\u5404\u81ea\u7ef4\u62a4\u7684list\u7ed3\u6784\u5f88\u50cf\uff0c\u4e3a\u4e0d\u540c\u7684size\u5404\u81ea\u7ef4\u62a4\u4e00\u7ec4\u53ef\u7528\u7a7a\u95f4\u5217\u8868\uff09<\/p>\n<p>3. \u5982\u679cstep 2 central list\u4e5f\u6ca1\u6709\u627e\u5230\uff0c\u5219\u8ba1\u7b97\u5206\u914dsize\u4e2a\u5b57\u8282\u9700\u8981\u5206\u914d\u591a\u5c11page\uff08\u53d8\u91cf\uff1aclass_to_pages\uff09<\/p>\n<p>4. \u6839\u636epagemap\u67e5\u627epage\u5bf9\u5e94\u7684\u53ef\u7528\u7684span\u5217\u8868\uff0c\u5982\u679c\u627e\u5230\u4e86\uff0c\u5219\u76f4\u63a5\u8fd4\u56despan\uff0ccentral list\u4f1a\u5c06\u8be5span\u5207\u5272\u6210\u5408\u9002\u7684\u5927\u5c0f\u653e\u5165\u5bf9\u5e94\u7684\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4ea4\u7ed9thread cache<\/p>\n<p>5. \u5982\u679cstep 4\u6ca1\u6709\u627e\u5230\u53ef\u7528\u7684span\uff0c\u5219\u5411OS\u76f4\u63a5\u7533\u8bf7\uff0c\u7136\u540e\u6b65\u9aa4\u540cstep 4\u3002<\/p>\n<p>\u6ce8\u610f\u7684\u662ftcmalloc\u5411\u7cfb\u7edf\u7533\u8bf7\u7a7a\u95f4\u6709\u4e09\u79cd\u65b9\u5f0f\uff1asbrk\uff0cmmap\uff0c\/dev\/mem\u6587\u4ef6\uff0c\u9ed8\u8ba4\u662f\u4e09\u79cd\u90fdtry\u7684\uff0c\u4e00\u79cd\u4e0d\u884c\u6362\u53e6\u5916\u4e00\u79cd\u3002<\/p>\n<p>\u6ce8\uff1a\u5927\u5bf9\u8c61\u76f4\u63a5\u4f7f\u7528\u9875\u7ea7\u5206\u914d\u5668\uff08\u4e00\u4e2a\u9875\u662f\u4e00\u4e2a4K\u7684\u5bf9\u9f50\u5185\u5b58\u533a\u57df\uff09\u4ece\u4e2d\u592e\u5806\u76f4\u63a5\u5206\u914d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u7ebf\u7a0b\u91ca\u653e\u8d44\u6e90\uff1a<\/strong><\/p>\n<p>1. \u91ca\u653e\u67d0\u4e2aobject<\/p>\n<p>2. \u627e\u5230\u8be5object\u6240\u5728\u7684span<\/p>\n<p>3. \u5982\u679c\u8be5span\u4e2d\u6240\u6709object\u90fd\u88ab\u91ca\u653e\uff0c\u5219\u91ca\u653e\u8be5span\u5230\u5bf9\u5e94\u7684\u53ef\u7528\u5217\u8868\uff0c\u5728\u91ca\u653e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5c1d\u8bd5\u5c06\u8be5span\u8ddf\u5de6\u53f3spans merge\u6210\u66f4\u5927\u7684span<\/p>\n<p>4. \u5982\u679c\u5f53\u524dthread cache\u7684free \u7a7a\u95f4\u5927\u4e8e\u6307\u5b9a\u9884\u7f6e\uff0c\u5f52\u8fd8\u90e8\u5206\u7a7a\u95f4\u7ed9central list<\/p>\n<p>5. central list\u4e5f\u4f1a\u8bd5\u56fe\u901a\u8fc7\u91ca\u653e\u53ef\u7528span\u5217\u8868\u7684\u6700\u540e\u51e0\u4e2aspan\u6765\u5c06\u4e0d\u7528\u7684\u7a7a\u95f4\u5f52\u8fd8\u7ed9OS<\/p>\n<p>tcmalloc\u5411OS\u7533\u8bf7\/\u91ca\u653e\u8d44\u6e90\u662f\u4ee5span\u4e3a\u5355\u4f4d\u7684\u3002<\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 \u539f\u7406 tcmalloc\u5c31\u662f\u4e00\u4e2a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-4350","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4350"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4350\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}