{"id":564,"date":"2023-09-03T22:20:17","date_gmt":"2023-09-03T14:20:17","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=564"},"modified":"2023-09-04T17:21:33","modified_gmt":"2023-09-04T09:21:33","slug":"%e4%bd%bf%e7%94%a8valgrind%e8%a7%82%e5%af%9fluajit%e8%bf%9b%e7%a8%8b%e5%86%85%e5%ad%98","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=564","title":{"rendered":"\u4f7f\u7528valgrind\u89c2\u5bdfluajit\u8fdb\u7a0b\u5185\u5b58"},"content":{"rendered":"<h1>\u80cc\u666f<\/h1>\n<p>\u6709\u5929\u6211\u4eec\u6267\u884c\u4e86\u7c7b\u4f3c\u7684lua\u4ee3\u7801,\u5f80\u4e00\u4e2atable\u4e2d\u5b58\u5165\u5927\u91cf\u6570\u636e,\u5728\u8fdb\u884c\u591a\u6b21collectgarbage(&#8220;collect&#8221;)\u56de\u6536<\/p>\n<pre><code class=\"line-numbers\">ab = {};\nfor i = 1, 1000000 do ab[#ab + 1] = {string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000)), string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000)), string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000)), string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000)), string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000)), string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000)),[string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000))]= {string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000)), string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000)), string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000)),string.format(\"aaaaa_%d_%d\", i, math.random(1, 1000000))} } end\n\nprint(collectgarbage(\"count\"))\n661211.734375\n\nab = nil\ncollectgarbage(\"collect\");\ncollectgarbage(\"collect\");\ncollectgarbage(\"collect\");\ncollectgarbage(\"collect\");\n\nprint(collectgarbage(\"count\"))\n591.5625\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u591a\u6b21collect\u540ecount\u6570\u91cf\u5df2\u7ecf\u964d\u4e0b,\u800c\u4f7f\u7528linux\u4e0atop\u6307\u4ee4\u770b\u5230RES\u5e76\u672a\u51cf\u5c11,\u4e8e\u662f\u60f3\u8981\u4f7f\u7528valgrind\u89c2\u5bdf\u770bluajit\u5185\u5b58\u7684\u7533\u8bf7\u91ca\u653e\u60c5\u51b5<\/p>\n<h1>\u542f\u52a8\u5e26\u6709vgdb\u6a21\u5f0f\u7684valgrind<\/h1>\n<pre><code class=\"line-numbers\">\u7a97\u53e31\nvalgrind --vgdb=yes --vgdb-error=0 --tool=massif --detailed-freq=1 --stacks=yes   $LD_LIBRARY_PATH\/main -server_id 1\n\n<\/code><\/pre>\n<h1>\u65b0\u7684\u63a7\u5236\u53f0\u4f7f\u7528gdb\u8fde\u63a5valgrind<\/h1>\n<pre><code class=\"line-numbers\">\u7a97\u53e32\ngdb \/home\/cc\/main\ntarget remote | vgdb\n\n<\/code><\/pre>\n<h1>\u5728\u6211\u4eec\u9700\u8981\u4f7f\u7528valgrind\u622a\u53d6\u5185\u5b58\u60c5\u51b5\u7684\u4ee3\u7801\u5904\u6253\u4e0a\u65ad\u70b9<\/h1>\n<pre><code class=\"line-numbers\">\u7a97\u53e32\nb lua_engine.cpp:278\n\n<\/code><\/pre>\n<h1>\u5f00\u59cb\u8fdb\u7a0b<\/h1>\n<pre><code class=\"line-numbers\">\u7a97\u53e32\ncontinue\n\n<\/code><\/pre>\n<h1>\u5f53\u6211\u4eec\u6267\u884c\u4ee3\u7801\u540e,\u65ad\u70b9\u751f\u6548,\u4f7f\u7528\u6307\u4ee4\u622a\u53d6\u5f53\u524d\u955c\u50cf<\/h1>\n<pre><code class=\"line-numbers\">monitor detailed_snapshot snapshot001\n\n<\/code><\/pre>\n<h1>\u4f7f\u7528ms_print \u89e3\u6790\u76f8\u5173\u5185\u5bb9<\/h1>\n<pre><code class=\"line-numbers\">ms_print snapshot001 &gt; snapshot001.txt\n\n<\/code><\/pre>\n<h1>\u8fdb\u7a0b\u9000\u51fa\u5934\u67e5\u770b\u5168\u90e8\u7ed3\u679c<\/h1>\n<pre><code class=\"line-numbers\">    MB\n317.4^                                        ##                              \n     |                                        # @@@@@@@@@@@                   \n     |                                        # @@@@ @ @@@ @@                 \n     |                                       @# @@@@ @ @@@ @                  \n     |                                    @@@@# @@@@ @ @@@ @ @                \n     |                              @@@@@@@@ @# @@@@ @ @@@ @ @@@              \n     |                        @@@@@@@@@ @ @@ @# @@@@ @ @@@ @ @@@@             \n     |                    @@@@@ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@            \n     |                    @@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@@          \n     |                 @@@@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@           \n     |             @@@@@ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@           \n     |          @@@@ @ @ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@           \n     |        @@@@@@ @ @ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@ @         \n     |   @@@@@@ @@@@ @ @ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@ @@@@@     \n     | @@@ @@ @ @@@@ @ @ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@ @@@@@@@@@@\n     | @@@ @@ @ @@@@ @ @ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@ @@@@@@@@@@\n     | @@@ @@ @ @@@@ @ @ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@ @@@@@@@@@@\n     | @@@ @@ @ @@@@ @ @ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@ @@@@@@@@@@\n     | @@@ @@ @ @@@@ @ @ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@ @@@@@@@@@@\n     | @@@ @@ @ @@@@ @ @ @@@@ @ @@@ @@@ @ @@ @# @@@@ @ @@@ @ @@@@@@ @@@@@@@@@@\n   0 +-----------------------------------------------------------------------&gt;Gi\n     0                                                                   2.893\n\n<\/code><\/pre>\n<h1>snapshot\u955c\u50cf1<\/h1>\n<pre><code class=\"line-numbers\">--------------------------------------------------------------------------------\n  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)\n--------------------------------------------------------------------------------\n  0              0                0                0             0            0\n00.00% (0B) (heap allocation functions) malloc\/new\/new[], --alloc-fns, etc.\n\n--------------------------------------------------------------------------------\n  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)\n--------------------------------------------------------------------------------\n  1     44,224,718      105,769,744      105,299,512       449,064       21,168\n99.56% (105,299,512B) (heap allocation functions) malloc\/new\/new[], --alloc-fns, etc.\n-&gt;97.43% (103,056,745B) 0x10701A49: CMemoryPool::AddNewBlob() (mempool.cpp:190)\n-&gt;01.40% (1,477,976B) 0x6670D0C: lj_mem_realloc (lj_gc.c:877)\n<\/code><\/pre>\n<h1>snapshot\u955c\u50cf37<\/h1>\n<pre><code class=\"line-numbers\">--------------------------------------------------------------------------------\n  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)\n--------------------------------------------------------------------------------\n 37  2,290,638,220      300,213,592      270,958,055    29,234,633       20,904\n90.26% (270,958,055B) (heap allocation functions) malloc\/new\/new[], --alloc-fns, etc.\n-&gt;40.91% (122,825,136B) 0x6670D0C: lj_mem_realloc (lj_gc.c:877)\n\n<\/code><\/pre>\n<h1>snapshot\u955c\u50cf52<\/h1>\n<pre><code class=\"line-numbers\">--------------------------------------------------------------------------------\n  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)\n--------------------------------------------------------------------------------\n 52  3,030,866,900      105,535,880      105,451,090        64,046       20,744\n99.92% (105,451,090B) (heap allocation functions) malloc\/new\/new[], --alloc-fns, etc.\n-&gt;97.65% (103,056,745B) 0x10701A49: CMemoryPool::AddNewBlob() (mempool.cpp:190)\n-&gt;02.01% (2,120,128B) 0x6670D0C: lj_mem_realloc (lj_gc.c:877)\n<\/code><\/pre>\n<h1>\u7ed3\u8bba<\/h1>\n<ol>\n<li>\u53ef\u4ee5\u770b\u51fa \u5728\u6267\u884cfor\u5faa\u73af\u7684\u524d\u540e,lj_mem_realloc\u5360\u7528\u7684\u5185\u5b58\u4ece\u4f4e(01.40%)\u5230\u9ad8(40.91%),\u6700\u9ad8\u8d85\u8fc7\u603b\u5185\u5b58\u768440.91%,\u7ecf\u8fc7collectgarbage(&#8220;collect&#8221;);\u4e4b\u540e,lj_mem_realloc\u9501\u5360\u7528\u7684\u5185\u5b58\u53c8\u56de\u5230\u521d\u59cb\u6c34\u5e7302.01%<\/li>\n<li>top\u770b\u5230\u4e0d\u4f1ares\u5360\u7528\u4e0d\u4f1a\u4e0b\u964d\u7684\u95ee\u9898,\u5e94\u8be5\u662f\u7531\u4e8e\n<ul>\n<li>https:\/\/stackoverflow.com\/questions\/47508165\/will-processs-res-memory-drop-after-memory-freed#comment81972425_47508165<\/li>\n<li>https:\/\/stackoverflow.com\/questions\/13232119\/memory-usage-doesnt-decrease-when-free-used<\/li>\n<li>https:\/\/stackoverflow.com\/questions\/55294985\/does-malloc-reserve-more-space-while-allocating-memory<\/li>\n<li>\u5f53\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\u5185\u5b58\u540e, \u8be5\u8fdb\u7a0b\u7684\u5185\u5b58\u5728top\u4e2d\u7684res\u4fbf\u4f1a\u4e0b\u964d<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u6709\u5929\u6211\u4eec\u6267\u884c\u4e86\u7c7b\u4f3c\u7684lua\u4ee3\u7801,\u5f80\u4e00\u4e2atable\u4e2d\u5b58\u5165\u5927\u91cf\u6570\u636e,\u5728\u8fdb\u884c\u591a\u6b21collectgarbage(<a href=\"https:\/\/www.ccagml.com\/?p=564\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u4f7f\u7528valgrind\u89c2\u5bdfluajit\u8fdb\u7a0b\u5185\u5b58<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/564"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=564"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/564\/revisions"}],"predecessor-version":[{"id":565,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/564\/revisions\/565"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}