Weak references to Lua tables

       I was asked a question recently, what is a weak reference to a lua table. I read the fourth edition of Lua Programming before, but I didn’t remember it and couldn’t answer it. After that, I made a simple check and made this record.

       Lua's garbage collection mechanism is when a variable is no longer referenced. When the garbage collection mechanism is triggered, this part of the memory will be recycled. Weak references are a better way to reclaim memory. The following code:

       Before setting a weak reference to the key:

local t = {}
local key1 = { name  = 'key1' }
t[key1] = 1
key1 = nil

for k,v in pairs(t) do
	print(k,v)
	for k1,v1 in pairs(k) do    --由于k是一个table,再次打印
		print(k1,v1)
	end
end

       The output is this:

       As you can see, even if our table key1 is empty, the table t can still be printed (is the table t still contains a reference to the table key1). Below we set the weak reference of the key , the code is as follows:

local t = {}
local key1 = { name  = 'key1' }
t[key1] = 1
key1 = nil
setmetatable(t, {__mode = "k"})   --设置 table 中 key 的弱引用
collectgarbage()                  --手动触发垃圾回收  

for k,v in pairs(t) do
	print(k,v)
	for k1,v1 in pairs(k) do
		print(k1,v1)
	end
end

        The output is this:

        As you can see, when garbage collection is triggered, table t has been emptied. This is because of the weak reference to the table key. When the key value is not referenced by other values, garbage collection will directly reclaim the memory of the table containing the specified key.

        Weak references can also set the value of the bit table .

        Before setting a weak reference to value:

local t = {}
local key2 = { name = 'key2'}
table.insert(t, key2)
key2 = nil

for k,v in pairs(t) do
	print(k,v)
	for k1,v1 in pairs(v) do      --由于 v 是一个table 再次打印
		print(k1,v1)
	end
end

        The results are as follows:

        Set a weak reference to value:

local t = {}
local key2 = { name = 'key2'}
table.insert(t, key2)
key2 = nil

setmetatable(t, {__mode = "v"})   --设置 table value的弱引用
collectgarbage()                  --手动触发垃圾回收

for k,v in pairs(t) do
	print(k,v)
	for k1,v1 in pairs(v) do
		print(k1,v1)
	end
end

         The results are as follows:

        It is also possible to set weak references to key and value at the same time. In this way, once one is set to nil, the garbage collection will directly trigger the collection.

local t = {}
local key2 = { name = 'key2'}
table.insert(t, key2)
key2 = nil

setmetatable(t, {__mode = "v"})   --设置 table,key 和 value 的弱引用
collectgarbage()                  --手动触发垃圾回收

for k,v in pairs(t) do
	print(k,v)
	for k1,v1 in pairs(v) do
		print(k1,v1)
	end
end

         The results are as follows:

 

         Finally, reasonable use of weak references can enhance garbage collection. But if you are not familiar with it, it may also cause unexpected consequences.

Guess you like

Origin blog.csdn.net/banfushen007/article/details/108608072