lua weak表与gc


local a = {}
local b = {}
setmetatable(a, b)
--这个 table 中的 vaules 就是 weak
b.__mode = "k" -- now 'a' has weak keys
local t1 = {}
local t2 = {} -- creates first key
a[t1] = 0
a[t2] = 1
--t2 is empty,can be gc
t2 = {}
collectgarbage()
for k, v in pairs(a)
do print(v)
end

0

你可能感兴趣的:(lua)