{"id":114,"date":"2023-06-09T19:19:26","date_gmt":"2023-06-09T11:19:26","guid":{"rendered":"https:\/\/yanghanwen.cn\/?p=114"},"modified":"2023-06-09T19:19:26","modified_gmt":"2023-06-09T11:19:26","slug":"lua-%e4%b8%ad%e7%9a%84%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/yanghanwen.cn\/?p=114","title":{"rendered":"lua \u4e2d\u7684\u5185\u5b58\u7ba1\u7406"},"content":{"rendered":"<ul>\n<li><a href=\"#gc\">GC<\/a>\n<ul>\n<li><a href=\"#\u57fa\u672c\u6d41\u7a0b\">\u57fa\u672c\u6d41\u7a0b<\/a><\/li>\n<li><a href=\"#\u65b9\u6cd5\">\u65b9\u6cd5<\/a><\/li>\n<li><a href=\"#\u5229\u7528-gc-\u68c0\u67e5\u5185\u5b58\">\u5229\u7528 gc \u68c0\u67e5\u5185\u5b58<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#\u5f15\u7528\u7c7b\u578b\">\u5f15\u7528\u7c7b\u578b<\/a>\n<ul>\n<li><a href=\"#\u548c\u503c\u7c7b\u578b\u6bd4\u8f83\">\u548c\u503c\u7c7b\u578b\u6bd4\u8f83<\/a><\/li>\n<li><a href=\"#\u5185\u5b58\u5360\u7528\">\u5185\u5b58\u5360\u7528<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#weak-table\">weak table<\/a><\/li>\n<li><a href=\"#\u5185\u5b58\u7684\u8fde\u7eed\u6027\">\u5185\u5b58\u7684\u8fde\u7eed\u6027<\/a><\/li>\n<li><a href=\"#next\">next<\/a><\/li>\n<li><a href=\"#len\">len<\/a><\/li>\n<\/ul>\n<h1>GC<\/h1>\n<h2>\u57fa\u672c\u6d41\u7a0b<\/h2>\n<p><a href=\"https:\/\/www.lua.org\/manual\/5.3\/manual.html#2.5\">Garbage Collection<\/a><br \/>\nlua \u662f oop \u8bed\u8a00\uff0c\u62e5\u6709 GC \u673a\u5236<br \/>\n\u5b9e\u73b0\u65b9\u5f0f\u662f\u57fa\u4e8e\u5f15\u7528\u6807\u8bb0\u7684(<code>incremental mark-and-sweep collector.<\/code>)<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u4f1a\u4f7f\u7528 lua \u5185\u5b58\u7684 <code>reference<\/code>\uff0c\u90fd\u53ef\u4ee5\u501f\u52a9 <code>nil<\/code> \u6765\u6e05\u9664\u5f15\u7528\u6807\u8bb0\uff0c<br \/>\n\u5f53\u67d0\u4e2a <code>reference<\/code> \u4e0d\u518d\u88ab\u4efb\u4f55\u5176\u4ed6 <code>reference<\/code> \u5f15\u7528\u65f6\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u4f1a\u4f5c\u4e3a <code>dead objects<\/code> \u7b49\u5f85\u91ca\u653e\uff0c<br \/>\nGC \u5c06\u5b9a\u671f\u8fdb\u884c\u6536\u96c6<\/p>\n<p>GC \u6536\u96c6\u7684\u5468\u671f\u7531 \u4e24\u4e2a\u53c2\u6570\u51b3\u5b9a\uff0c\u4e24\u4e2a\u53c2\u6570\u90fd\u662f\u767e\u5206\u6bd4\uff0c\u6bd4\u5982 100 \u5c31\u4ee3\u8868 1\u500d\uff1a<\/p>\n<ul>\n<li>garbage-collector pause\uff1a\u8868\u793aGC\u5468\u671f\u7ed3\u675f\u8fc7\u540e\uff0c\u7b49\u5f85\u5185\u5b58\u5145\u6ee1\u5230\u4ec0\u4e48\u7a0b\u5ea6\u624d\u518d\u6b21\u6536\u96c6\uff1b<\/li>\n<li>garbage-collector step multiplier\uff1a\u8868\u793aGC\u6536\u96c6\u7684\u901f\u5ea6\uff0c\u5e76\u4e14\u4e0d\u80fd\u586b\u592a\u6162\uff0c\u5426\u5219GC\u5468\u671f\u6c38\u8fdc\u7ed3\u675f\u4e0d\u4e86\uff1b\u4f46\u8c03\u592a\u9ad8\u4f1a\u5f71\u54cd\u6027\u80fd<\/li>\n<\/ul>\n<p>\u4e24\u4e2a\u53c2\u6570\u90fd\u9700\u8981\u901a\u8fc7 <code>collectgarbage<\/code> \u65b9\u6cd5\u53bb\u6539\uff08\u5728 C\u8bed\u8a00\u91cc\u662f lua_gc\uff09\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u6539<\/p>\n<h2>\u65b9\u6cd5<\/h2>\n<p>lua \u5185\u5b58\u5206\u914d\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u81ea\u52a8\u7684\uff0c\u4f60\u9700\u8981\u91ca\u653e\u5185\u5b58\u5c31\u5f97\u628a <code>reference(\u5f15\u7528)<\/code> \u6807\u8bb0\u4e3a <code>nil<\/code>\uff0c\u7136\u540e\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u4f1a\u4f5c\u4e3a <code>dead objects<\/code> \u7b49\u5f85\u91ca\u653e<br \/>\n\u76f8\u5bf9\u7684\uff0c\u5168\u5c40\u53d8\u91cf\u56e0\u4e3a lua \u4e0d\u786e\u5b9a\u4f60\u4ec0\u4e48\u65f6\u5019\u7528\uff0c\u6240\u4ee5\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u4e00\u76f4\u4e0d\u4f1a\u91ca\u653e\u7684<\/p>\n<p>\u5982\u679c\u4e0d\u501f\u52a9\u5185\u5b58\u5206\u6790\u4e4b\u7c7b\u7684\u5de5\u5177\uff0c\u4e0d\u5bb9\u6613\u4e0d\u51fa\u6548\u679c\uff0c\u9700\u8981\u4f60\u81ea\u5df1\u5206\u6790\u7ed3\u679c<\/p>\n<p>\u624b\u52a8GC\u53ef\u4ee5\u8c03\u7528<a href=\"https:\/\/www.lua.org\/manual\/5.3\/manual.html#pdf-collectgarbage\"> <code>collectgarbage()<\/code><\/a> \uff0c<br \/>\n\u5176\u53c2\u6570\u662f\u4e00\u4e9b\u72b6\u6001string\uff1a<\/p>\n<ul>\n<li>&#8220;collect&#8221;\uff1a\u9ed8\u8ba4\u7b56\u7565\uff0c\u8fdb\u884c\u5b8c\u6574 GC<\/li>\n<li>&#8220;stop&#8221;\uff1a\u505c\u6b62\u81ea\u52a8\u7684CG\uff0c\u76f4\u5230\u8c03\u7528&#8221;restart&#8221;<\/li>\n<li>&#8220;restart&#8221;\uff1a\u91cd\u542f\u81ea\u52a8GC<\/li>\n<li>&#8220;count&#8221;\uff1a\u8fd4\u56de\u5f53\u524d Lua \u5360\u7528\u7684\u603b\u5185\u5b58\uff08\u5355\u4f4d\u662fKB\uff0c\u6240\u4ee5\u53ef\u80fd\u6709\u5c0f\u6570\uff0c\u8981\u4e58\u4ee51024\uff09<\/li>\n<li>&#8220;step&#8221;\uff1a\u6a21\u62df GC \u6b65\u9aa4\uff0c\u9700\u8981\u8ffd\u52a0\u4e00\u4e2a\u6570\u503c\u53c2\u6570\uff0c\u8868\u793a\u5f53 Lua \u5df2\u7ecf\u6709\u7533\u8bf7\u5230\u6307\u5b9a\u6570\u989d\uff08\u5355\u4f4dKB\uff09\u5185\u5b58\u540e\u624d\u8fdb\u884cGC\uff0c\u8fd4\u56de true|false \u8868\u793aGC\u7684\u6536\u96c6\u5faa\u73af\u662f\u5426\u7ed3\u675f<\/li>\n<li>&#8220;setpause&#8221;:\u8bbe\u7f6e pause \u53c2\u6570\uff08\u53c2\u8003\u524d\u9762\u4ecb\u7ecd\uff09<\/li>\n<li>&#8220;setstepmul&#8221;:\u8bbe\u7f6e mul \u53c2\u6570\uff08\u53c2\u8003\u524d\u9762\u4ecb\u7ecd\uff09<\/li>\n<li>&#8220;isrunning&#8221;: \u68c0\u67e5 GC \u662f\u5426\u5728\u8fd0\u884c\u4e2d<\/li>\n<\/ul>\n<h2>\u5229\u7528 gc \u68c0\u67e5\u5185\u5b58<\/h2>\n<p>\u4f60\u53ef\u4ee5\u501f\u52a9 count \u53c2\u6570\u6765\u7c97\u7565\u5730\u68c0\u67e5\u5185\u5b58<\/p>\n<pre><code class=\"language-LUA\">collectgarbage(\"stop\") -- \u5148\u505c\u6b62\u81ea\u52a8GC\nlocal memorySize = collectgarbage(\"count\") -- \u8bb0\u5f55\u5148\u524d\u72b6\u6001\nlocal tb = {}\nlocal newMemorySize = collectgarbage(\"count\") -- \u8bb0\u5f55\u65b0\u7684\u72b6\u6001\nprint((newMemorySize-memorySize) * 1024)\n\n-&gt; 56<\/code><\/pre>\n<p>\u6bd4\u5982\u8fd9\u91cc table \u8fd4\u56de\u662f 56 \u5b57\u8282\uff0c\u4f46\u5b9e\u9645\u4e0a\u7684\u5927\u5c0f\u8ddf lua \u7684\u7248\u672c\u6709\u5173\uff0c\u6240\u4ee5\u4ec5\u4f9b\u53c2\u8003<br \/>\n\u4f46\u5b66\u4e60\u9636\u6bb5\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u8fd9\u4e2a\u673a\u5236\u6765\u8fdb\u884c\u5185\u5b58\u539f\u7406\u7684\u63a2\u7d22<\/p>\n<h1>\u5f15\u7528\u7c7b\u578b<\/h1>\n<h2>\u548c\u503c\u7c7b\u578b\u6bd4\u8f83<\/h2>\n<p>\u5f15\u7528\u7c7b\u578b\u7c7b\u4f3c\u4e8e\u4f20\u7edf OOP \u8bed\u8a00\u4e2d\u7684\u6982\u5ff5\uff0c\u5728 lua \u91cc\u4e5f\u79f0\u4e3a <code>\u5bf9\u8c61\u7c7b\u578b(object)<\/code><br \/>\n\u548c\u4f20\u7edfOOP\u4e00\u6837\uff0c<\/p>\n<ul>\n<li>\u5f15\u7528\u7c7b\u578b\u53ea\u662f\u4f20\u9012\u6307\u9488<\/li>\n<li>\u5f15\u7528\u7c7b\u578b\u9000\u51fa\u8bed\u6cd5\u57df\u7684\u65f6\u5019\u4e0d\u4f1a\u7acb\u5373\u91ca\u653e\uff0c\u9700\u8981\u7b49\u5f85GC<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u7528\u524d\u9762 GC \u7684\u65b9\u6cd5\u6d4b\u8bd5<\/p>\n<pre><code class=\"language-LUA\">collectgarbage(\"stop\")\nlocal gc_count = function()\n    return collectgarbage(\"count\")\nend\nlocal memory = gc_count()\nlocal tb \nfor i=1,10 do\n    tb = {} -- \u91cd\u590d\u7533\u8bf7 10\u6b21 table \u7684\u5185\u5b58\uff0c\u4f1a\u53d1\u73b0\u6700\u7ec8\u7ed3\u679c\u662f 10\u500d\u5185\u5b58(\u4f8b\u5982\u5355\u4e2a table 56B \u5c31\u5f97\u5230 560B)\n    -- tb = 1 -- \u800c\u5982\u679c\u5168\u90e8\u90fd\u53ea\u662f\u8d4b\u503c\u4e00\u4e2a\u6570\u5b57\uff0c\u5219\u4e0d\u4f1a\u4ea7\u751f GC\nend\nprint((gc_count() - memory) * 1024)<\/code><\/pre>\n<p>\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c <code>string<\/code> \u867d\u7136\u4e5f\u53ef\u4ee5\u88ab GC \u6536\u96c6\uff0c\u4f46\u662f\u6bd4\u8f83\u7279\u6b8a<br \/>\ntable \u7b49\u5bf9\u8c61\u5728\u521b\u5efa\u65f6\uff0c\u90fd\u662f\u660e\u786e\u88ab\u6307\u5b9a\u4e3a \u5f15\u7528\u7c7b\u578b\uff0c\u5e76\u4e14\u544a\u77e5\u8c03\u7528\u8005\u5185\u5b58\u5730\u5740\uff0c<br \/>\n\u800c <code>string<\/code> \u5728\u4e00\u822c\u60c5\u51b5\u4e0b\u662f\u4e0d\u5360 GC \u7684\uff0c\u53ea\u6709\u4f7f\u7528\u4e86 <code>..<\/code> \u8fd9\u79cd\u8fde\u63a5\u7b26\u624d\u4f1a\u89e6\u53d1 GC \u6536\u96c6\uff0c<br \/>\n\u56e0\u6b64\u4e00\u822c\u90fd\u8ba4\u4e3a string \u662f\u503c\u7c7b\u578b <sup id=\"fnref1:REF1\"><a href=\"#fn:REF1\" class=\"footnote-ref\">1<\/a><\/sup><\/p>\n<h2>\u5185\u5b58\u5360\u7528<\/h2>\n<p>table \u548c function \u7684\u6bcf\u6b21\u521b\u5efa\uff0c\u90fd\u4f1a\u7533\u8bf7\u5185\u5b58\uff0c<br \/>\n\u5f53\u5f15\u7528\u8ba1\u6570\u4e3a0\u65f6\uff0c\u4f1a\u88ab GC \u6536\u96c6<\/p>\n<p>\u5177\u4f53\u6570\u989d\u5982\u679c\u4f9d\u8d56 <code>collectgarbage()<\/code> \u53bb\u5206\u6790\uff0c\u662f\u6bd4\u8f83\u56f0\u96be\u7684<br \/>\n\u6d4b\u8bd5\u65f6\u53ef\u4ee5\u501f\u52a9\u6bd4\u8f83\u5938\u5f20\u7684\u6570\u636e\u6765\u62b5\u6d88\u6389\u7ec6\u5fae\u7684\u8bef\u5dee\uff0c<\/p>\n<pre><code class=\"language-LUA\">collectgarbage(\"stop\")\nlocal function gc_count()\n    return collectgarbage(\"count\")\nend\nlocal function gc_print(num)\n    print(string.format(\"%s B\", num*1024))\nend\ncollectgarbage() -- \u5148\u6536\u96c6\u4e00\u6b21\u4fdd\u8bc1\u6ca1\u6709\u5176\u4ed6\u6761\u4ef6\u5e72\u6270\n\n-- \u4e3a\u4e86\u8ffd\u6c42\u5c3d\u91cf\u7cbe\u786e\uff0c\u4e0d\u80fd\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d print\uff0c\u56e0\u4e3a print \u672c\u8eab\u4f1a\u4ea7\u751fGC\nlocal memory = gc_count() -- \u8fd9\u4e2a\u8bed\u53e5\u672c\u8eab\uff0c\u6216\u8005\u63a5\u6536\u503c\u7684\u65f6\u5019\uff0c\u7591\u4f3c\u4f1a\u4ea7\u751f 48B \u7684GC\n\n-- \u5c40\u90e8\u53d8\u91cf\u5728\u9000\u51fa\u8bed\u6cd5\u57df\u4ee5\u540e\uff0c\u4f1a\u4e22\u5931\u5f15\u7528\uff0c\u89c6\u4e3a dead object\n-- \u4f46\u5b83\u5e76\u4e0d\u50cf\u503c\u7c7b\u578b\u4e00\u6837\u7acb\u523b\u88ab\u91ca\u653e\nfor i=1, 50 do\n    local tb = {}\nend\n\n-- \u5916\u90e8\u5c40\u90e8\u53d8\u91cf\uff0c\u56e0\u4e3a\u5728\u8be5 chunk \u4e2d\u4ecd\u7136\u4fdd\u6301\u7740\u5f15\u7528\uff0c\u6240\u4ee5\u4e0d\u4f1a\u88ab GC \u6389\nlocal outerTable = {}\nfor i=1, 100 do\n    outerTable[i] = {}\nend\n\n-- outerTable = nil --\u53ef\u4ee5\u5206\u4e24\u6b21\u6267\u884c\uff0c\u7b2c\u4e8c\u6b21\u89e3\u5f00\u8fd9\u4e2a\u6ce8\u91ca\n\n-- \u5c1d\u8bd5\u8ba1\u7b97\u5927\u81f4\u7684\u603b\u5185\u5b58\nlocal memory2 = gc_count()\ngc_print(memory2 - memory)\n\n-- \u5c1d\u8bd5\u5728\u6536\u96c6\u5783\u573e\u540e\u8ba1\u7b97\u603b\u5185\u5b58\ncollectgarbage()\nlocal memory3 = gc_count()\ngc_print(memory3 - memory)<\/code><\/pre>\n<p>\u4ece\u7ed3\u8bba\u4e0a\u6765\u8bf4\uff1a<\/p>\n<ul>\n<li>table \u7684\u521b\u5efa\u8bed\u53e5 <code>table={}<\/code> \u4f1a\u5206\u914d\u65b0\u7684\u5185\u5b58\uff08\u65e0\u8bba\u5728\u4ec0\u4e48\u5730\u65b9\u3001\u4ec0\u4e48\u4f5c\u7528\u57df\uff09<\/li>\n<li>\u5f53\u9000\u51fa\u5c40\u90e8\u4f5c\u7528\u57df\uff0c\u4f5c\u4e3a\u5c40\u90e8\u53d8\u91cf\u7684 table \u5f15\u7528\u4e22\u5931\uff0c\u4f1a\u5728\u7a0d\u540e\u8fdb\u884c GC \u65f6\u4f5c\u4e3a\u6536\u96c6\u76ee\u6807<\/li>\n<li>\u540c\u7406\uff0c\u5f53\u6307\u5411 table \u7684\u5f15\u7528\uff08outerTable\uff09\u88ab\u6807\u8bb0\u4e3a nil \u4f7f\u5f97\u5f15\u7528\u4e22\u5931\uff0c\u4e5f\u4f1a\u5728\u7a0d\u540e\u89e6\u53d1 GC<\/li>\n<\/ul>\n<p>\u5982\u679c\u5728\u4f7f\u7528\u5b8c\u4e4b\u540e\u7acb\u523b\u5c06\u5e95\u5c42\u7684\u7684\u5f15\u7528\u7f6e\u4e3a nil \u53ef\u4ee5\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u51cf\u7f13 GC \u538b\u529b\uff08\u5e45\u5ea6\u5f88\u5c0f\uff09<br \/>\n\u800c function \u7684\u521b\u5efa\u4e5f\u662f\u7c7b\u4f3c\u7684\u9053\u7406\uff0c\u5c06\u793a\u4f8b\u4e2d\u7684\uff1a<\/p>\n<pre><code class=\"language-LUA\">local tb = {}<\/code><\/pre>\n<p>\u66ff\u6362\u4e3a<\/p>\n<pre><code class=\"language-LUA\">local fun = function()        \nend<\/code><\/pre>\n<p>\u8dd1\u4e00\u4e0b\u5c31\u80fd\u770b\u51fa\u6548\u679c<\/p>\n<h1>weak table<\/h1>\n<p>\u4e3a\u4e86\u652f\u6301\u7528\u6237\u81ea\u5df1\u7ba1\u7406 GC\uff0clua \u62e5\u6709 <code>weak reference<\/code> \u673a\u5236\uff0c\u4e5f\u5c31\u662f\u5f31\u5f15\u7528\uff0c<br \/>\n\u5f31\u5f15\u7528\u548c <code>strong reference \u5f3a\u5f15\u7528<\/code> \u76f8\u5bf9\u5e94\uff0c<\/p>\n<p>\u5728 \u5f3a\u5f15\u7528 \u7684\u73af\u5883\u4e0b\u6267\u884c\u8fd9\u6837\u7684\u4ee3\u7801<\/p>\n<pre><code class=\"language-LUA\">local tb = {}\nfor i=1,10 do\n    tb[{}] = i\nend\n\ncollectgarbage()\nfor k, v in pairs(tb) do\n    print(v)\nend\n\n-&gt; 1,2 ... 10<\/code><\/pre>\n<p>\u5f53\u4f60\u521b\u5efa\u82e5\u5e72\u4e2a table\uff0c\u4ed6\u4eec\u6bcf\u4e2a\u90fd\u62e5\u6709\u65b0\u7684\u5730\u5740\uff0clua \u9ed8\u8ba4\u7684\u5f3a\u5f15\u7528\u673a\u5236\u4f1a\u628a\u8fd9\u4e9b\u5730\u5740\u89c6\u4e3a\u5177\u4f53\u7684\u503c\uff0c\u8ba9 table \u548c\u8fd9\u4e9b\u5730\u5740\u4fdd\u6301\u5f15\u7528\u5173\u7cfb<br \/>\n\u5728\u4f8b\u5b50\u4e2d\u521b\u5efa\u4e86\u8bb8\u591a\u5c40\u90e8\u53d8\u91cf\u7684 table\uff0c\u7406\u8bba\u4e0a\u9000\u51fa\u4f5c\u7528\u57df\u4ee5\u540e\u8fd9\u4e9btable\u5c31\u201c\u65e0\u4e3b\u4e86\u201d\uff0c<br \/>\n\u4f46\u5b9e\u9645\u4e0a\u8fd9\u4e9b \u201c\u65e0\u4e3b\u7684\u201d table \u76ee\u524d\u4ecd\u7136\u88ab tb \u6301\u6709\uff0c\u53ea\u662f\u6211\u4eec\u5199\u903b\u8f91\u7684\u4eba\u8ba4\u4e3a\u5b83\u4e22\u5931\u4e86\u5f15\u7528\uff0c\u800clua\u4e0d\u8fd9\u4e48\u8ba4\u4e3a\uff0c<br \/>\nGC \u4e5f\u4e0d\u8ba4\u4e3a\u8fd9\u4e9b\u4e1c\u897f\u8be5\u88ab\u91ca\u653e\uff0c\u6240\u4ee5\u540e\u7eed\u6d41\u7a0b\u91cc\uff0ctb\u4ecd\u7136\u80fd\u904d\u5386\u5230\u8fd9\u4e9b\u201c\u5165\u53e3\u201d<\/p>\n<p>\u5982\u679c\u7528\u6237\u521b\u5efa\u4e86\u4e00\u5957\u5185\u5b58\u7ba1\u7406\u673a\u5236\uff0c\u6301\u6709\u6240\u6709 lua \u865a\u62df\u673a\u4e2d\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5168\u5c40\u7684\u8d85\u7ea7 table \u5c31\u65f6\u523b\u4fdd\u6301\u7740\u5bf9\u4e8e\u6240\u6709\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u4f7f\u5f97 GC \u76f4\u63a5\u505c\u6446\uff0c<br \/>\n\u4e3a\u4e86\u89e3\u51b3\u8fd9\u79cd\u95ee\u9898\uff0c lua \u5141\u8bb8\u4f60\u8bbe\u7f6e weak table \u5c5e\u6027<\/p>\n<p>weak table \u7528 metatable \u7684\u5143\u65b9\u6cd5 __mode \u89e6\u53d1\uff0c\u53ef\u4ee5\u53c2\u8003 <a href=\"https:\/\/www.lua.org\/pil\/17.html\">\u539f\u6587\u6863<\/a><br \/>\n\u5b83\u7684\u503c\u5e94\u8be5\u662f\uff08\u5305\u542b\uff09 &#8220;k&#8221; \u6216\u8005 &#8220;v&#8221;\uff0c\u5206\u522b\u8868\u793a\uff1a<\/p>\n<ul>\n<li>&#8220;k&#8221;\uff1a\u5c06 table \u7684 key \u8bbe\u4e3a weak<\/li>\n<li>&#8220;v&#8221;\uff1a\u5c06 table \u7684 value \u8bbe\u4e3a weak<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u5c06\u521a\u624d\u4f8b\u5b50\u4e2d table \u7684 key \u8bbe\u4e3a weak\uff0c\u8868\u793a\u8fd9\u4e2a table \u5e76\u4e0d\u60f3\u548c\u76ee\u6807\u4ea7\u751f\u6b63\u5e38\u7684 \u201c\u5f3a\u5f15\u7528\u201d \u5173\u7cfb\uff1a<\/p>\n<pre><code class=\"language-LUA\">local tb = {}\nsetmetatable(tb, {__mode=\"k\"}) -- \u65b0\u589e\nfor i=1,10 do\n    tb[{}] = i\nend\n\ncollectgarbage()\nfor k, v in pairs(tb) do\n    print(v)\nend\n\n-&gt; <\/code><\/pre>\n<p>\u8fd9\u65f6\uff0c\u90a3\u4e9b\u672c\u5e94\u8be5\u56e0\u4e3a\u9000\u51fa\u4f5c\u7528\u57df\u800c\u91ca\u653e\u7684 \u5c40\u90e8\u7684table\uff0c\u65e0\u89c6\u4e86\u5916\u5c42 tb \u7684\u5f15\u7528\u5173\u7cfb\uff0c\u88ab GC \u89c6\u4e3a\u53ef\u6536\u96c6\u7684\u76ee\u6807\u89e6\u53d1\u4e86\u56de\u6536<\/p>\n<p>\u540c\u6837\u7684\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8bbe\u7f6e value\uff0c\u6216\u8005 key \u548c value \u4e00\u8d77<\/p>\n<pre><code class=\"language-LUA\">local tb = {}\nsetmetatable(tb, {__mode=\"v\"})\n\n-- \u53ea\u6709 value \u662f\u5f31\u5f15\u7528\uff0c\u800c key \u4fdd\u6301\u5f3a\u5f15\u7528\uff0c\u4f7f\u5f97 GC \u4e0d\u4f1a\u81ea\u52a8\u91ca\u653e \nfor i=1,10 do\n    tb[{}] = i\nend\n\ncollectgarbage()\nfor k, v in pairs(tb) do\n    print(k, v)\nend<\/code><\/pre>\n<p>\u5982\u679c\u540c\u65f6\u6307\u5b9a key \u548c value \u662f\u5f31\u5f15\u7528\uff0c\u53ea\u9700\u8981\u8ba9\u5b57\u7b26\u4e32\u5305\u542b &#8216;k&#8217; &#8216;v&#8217; \u4e24\u4e2a\u5b57\u7b26<\/p>\n<pre><code class=\"language-LUA\">setmetatable(tb, {__mode=\"kv\"})<\/code><\/pre>\n<h1>\u5185\u5b58\u7684\u8fde\u7eed\u6027<\/h1>\n<h1>next<\/h1>\n<pre><code>TODO<\/code><\/pre>\n<h1>len<\/h1>\n<p>len \u64cd\u4f5c\u7b26\u8868\u8fbe\u4e3a <code>#<\/code>\uff0c<br \/>\n\u4f8b\u5982 <code>print(#\"123\")<\/code> \u5c31\u4f1a\u8fd4\u56de3\uff0c\u8868\u793a\u53d6\u5b57\u7b26\u4e32\u7684 byte \u6570\u91cf\uff08\u5982\u679c1\u5b57\u8282\u4ee3\u88681\u5b57\u7b26\uff0c\u90a3\u4e48\u5c31\u7b49\u4ef7\u4e8e\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6570\u91cf\uff09<br \/>\nlua \u5728\u5224\u5b9a\u957f\u5ea6\u7684\u65f6\u5019\uff0c\u662f\u7528\u7684\u7c7b\u4f3c\u4e8e\u4e8c\u5206\u67e5\u627e\u7684\u7b97\u6cd5\uff0c\u56e0\u6b64\u5bf9\u4e8e\u5bb9\u5668\u7684\u5185\u5bb9\u6709\u4e00\u5b9a\u9650\u5236<\/p>\n<ul>\n<li>\u64cd\u4f5c\u65f6\uff0c\u8fd4\u56de\u7684\u503c\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a <code>\u8fb9\u754c(border)<\/code> \u4f4d\u7f6e\uff0c\u5b83\u9700\u8981\u6ee1\u8db3\uff1a\n<ul>\n<li><code>(border == 0 or t[border] ~= nil) and<\/code><\/li>\n<li><code>(t[border + 1] == nil or border == math.maxinteger)<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\u4e5f\u5c31\u662f\uff1a\n<ul>\n<li>\u60c5\u51b51\uff1a\u8fb9\u754c\u4e3a0<\/li>\n<li>\u60c5\u51b52\uff1a\u8fb9\u754c\u4e3atable\u4e2d\u7684\u67d0\u4e2a\u4f4d\u7f6e\uff0c\u6ee1\u8db3\uff1a\n<ul>\n<li>\u5f53\u524d\u4f4d\u7f6e\u6709\u503c<\/li>\n<li>\u4e0b\u4e2a\u4f4d\u7f6e\u662f nil<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd9\u4e2a\u8fb9\u754c\u503c\u5e76\u4e0d\u4e00\u5b9a\u662f table \u7684\u7ed3\u5c3e<br \/>\n\u5982\u679c\u4f60\u8fdb\u884c\u53d6\u957f\u5ea6\u7684\u5bb9\u5668\u5e76\u4e0d\u662f \u8fde\u7eed\u7684\u5bb9\u5668 \u2014\u2014 <code>\u5e8f\u5217(sequence)<\/code>\uff0c<br \/>\n\u5373\u5bb9\u5668\u88ab\u4e00\u4e9b <code>nil<\/code> \u503c\u622a\u65ad\uff0c\u4ea7\u751f\u4e86\u82e5\u5e72 \u5b50\u5e8f\u5217\uff0c<br \/>\n\u8fd9\u4e9b\u9020\u6210\u622a\u65ad\u540e\u679c\u7684 nil \u503c\u88ab\u79f0\u4e3a <code>\u6d1e(hole)<\/code><\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code>{nil, 20, 30, nil, nil, 60, nil}\n               1\n      2                 2\n\n-&gt; 6<\/code><\/pre>\n<p>\u5982\u56fe\uff0c\u5f53\u4e8c\u5206\u8ba9\u6700\u540e\u4e09\u4e2a\u5143\u7d20\u6210\u4e3a\u5b50\u5e8f\u5217\uff0c\u65e0\u6cd5\u7ee7\u7eed\u4e8c\u5206\uff1b<br \/>\n\u800c\u6700\u540e\u4e00\u4f4d\u662f nil\uff0c\u5bfc\u81f4\u63d0\u524d\u5224\u5b9a\u7ed3\u675f<br \/>\n\u4ece\u7ed3\u679c\u4e0a\u770b\uff1a<strong># \u5904\u7406\u65f6\u4f1a\u5ffd\u7565 table \u672b\u5c3e\u7684 nil<\/strong><\/p>\n<p>\u5b98\u65b9\u5bf9\u4e8e array size \u7684\u5efa\u8bae\u662f\uff0c\u4f7f\u7528 getn \u548c setn\uff0c\u4f46\u8fd9\u4e24\u4e2a\u51fd\u6570\u5df2\u7ecf\u79fb\u9664<br \/>\n\u4e00\u822c\u6765\u8bb2\u5982\u679c\u51fa\u4e8e\u51c6\u786e\u6027\u8003\u8651\uff0c\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u4e00\u5957\u53bb\u7ef4\u62a4<\/p>\n<div class=\"footnotes\">\n<hr>\n<ol>\n<li id=\"fn:REF1\"><a href=\"https:\/\/www.lua.org\/pil\/17.html\">https:\/\/www.lua.org\/pil\/17.html<\/a> These are implementation details. Thus, from the programmer&#8217;s point of view, strings are values, not objects. Therefore, like a number or a boolean, a string is not removed from weak tables (unless its associated value is collected).&nbsp;<a href=\"#fnref1:REF1\" rev=\"footnote\" class=\"footnote-backref\">\u21a9<\/a><\/li>\n<\/ol>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd Lua \u4e2d\u7684 GC \u548c\u5f15\u7528\u7c7b\u578b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[8],"_links":{"self":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts\/114"}],"collection":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=114"}],"version-history":[{"count":1,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":116,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=\/wp\/v2\/posts\/114\/revisions\/116"}],"wp:attachment":[{"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanghanwen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}