The weak reference type of a table is determined by the __mode field in its element table, and the value of the field should be a string, and if the string contains the letter ' K '/' V ' then the value of the table is a weak reference, as the following code reads:
Copy Code code as follows:
Key = {}
A[key] = 1
Key = {}
A[key] = 2
CollectGarbage ()--Force a garbage collection
For K, v. in pairs (a) do
Print (v)
End
Results: 2
The second copy of key = {} overrides the first key. When the collector is running, because there is no other place to reference the first key, the first key is reclaimed, and the corresponding entry in the table is deleted, as for the second key, the variable key still references it, so it is not recycled.
Note that LUA reclaims only the objects in the weak reference table, and values like numbers and bool are not recyclable.