Lua uses the garbage collection-based memory management mechanism. Therefore, for Program , in many cases, memory problems will no longer affect them. However, no garbage collector is omnipotent. In some special cases, the garbage collector cannot accurately determine whether to clear the current object. In this way, many junk objects may not be released. To solve this problem, Lua developers must cooperate with each other to a certain extent. For example, when a table object is stored in a container and no variable is referenced outside the container, the garbage collector of Lua will not clean up such objects, because the container object still references it. If the application targeting the container is limited to search, rather than traversing, the object will never be used. In fact, we want Lua's garbage collector to clear such objects. See the following Code :
1 A ={}< br> 2 key ={}< br> 3 A [Key] = 1
4 key ={}< br> 5 A [Key] = 2
6 collectgarbage ()
7 for K, v in pairs (a) DO
8 Print (v)
9 end
10 -- output 1 and 2
After garbage collection, neither of the two keys in Table A can be cleared. However, for keys whose values are equal to 1, if the subsequent logic does not traverse Table, then we can think that the object memory is leaked. Lua provides a mechanism called weak reference table, which can prompt the Garbage Collector. If an object, such as the first table key in the code above, is referenced by a weak reference table, you can clear the garbage collection.
The weak reference table in Lua provides the weak reference mode in 3, that is, the key is a weak reference, the value is a weak reference, and the key and value are both weak references. No matter which type of weak referenced table, as long as one key or value is recycled, the entire entry where they are located will be deleted from the table.
The weak reference type of a table is based on its metadata_ ModeField. If the value is a contain character"K"The table is a weak key reference."V"If both characters exist, it means weak reference of key/value. See the following code:
1 A = {}
2 B = {__ mode = " K " }
3 Setretriable (A, B)
4 Key = {}
5 A [Key] = 1
6 Key = {}
7 A [Key] = 2
8 Collectgarbage ()
9 For K, V In Pairs () Do
10 Print (V)
11 End
12 -- Only output 2
In the preceding code example, the first key is overwritten by the definition of the second key after it is stored in Table A. Therefore, its unique reference comes from the weak reference table of the key. In fact, this mechanism also exists in Java. Java also provides a group of weak reference containers in Versions later than 1.5. Its semantics is similar to Lua's weak reference table.
It should be noted that the weak reference table in Lua only acts on the table type variables and does not play any role in other types of variables, such as values and strings.
1. Memorandum (memoize) function:
Using "space for time" is a common method for optimizing program running efficiency. For example, for a common server, the request it receives contains the Lua code, every time a request is received, it will call the loadstring function of Lua to dynamically parse the Lua code in the request. If this operation is too frequent, the execution efficiency of the server will decrease. To solve this problem, we can cache the results of each parsing to a table. If we receive the same Lua code next time, we do not need to call loadstirng for dynamic parsing, instead, retrieve the parsed function from the table and execute it directly. In this way, the execution efficiency of the server can be greatly improved when a large number of Lua code is repeated. If a considerable amount of Lua Code only appears once, then using this mechanism will cause a large amount of memory resources to be occupied and cannot be effectively released. In this case, the use of weak reference tables not only improves the program running efficiency to a certain extent, but also effectively releases memory resources. See the following code:
1 Local Results = {}
2 Setretriable (Results, {__ mode = " V " })-- The key in the results table is a string-type Lua code.
3 Function Mem_loadstring (s)
4 Local Res = Results [s]
5 If Res = Nil Then
6 Res = Assert ( Loadstring (S ))
7 Results [s] = res
8 End
9 Return Res
10 End