Lua Tutorial (13): Weak reference Table_lua

Source: Internet
Author: User
Tags garbage collection lua

LUA uses a garbage collection based memory management mechanism, so for programmers, memory problems will not bother them any more. However, any garbage collector is not omnipotent, in some special cases, the garbage collector is not accurate to determine whether the current object should be cleaned up. This is most likely to cause many garbage objects to be freed. To solve this problem, there is a need for LUA developers to be somewhat coordinated. For example, when a table object is stored in a container, and the outside of the container no longer has any variables referencing that object, the Lua garbage collector is not cleaned up because the container object still references him. If the application for the container is limited to lookup, not traversal, then the object will never be used. In fact, for such objects we want the LUA garbage collector to clean it out. See the following code:

Copy Code code as follows:

A = {}
Key = {}
A[key] = 1
Key = {}
A[key] = 2
CollectGarbage ()
For k,v in pairs (a) do
Print (v)
End
--Output 1 and 2

After garbage collection, two keys in table A cannot be cleaned, but for key value equal to 1, if the subsequent logic does not traverse table A, then we can assume that the object memory is leaking. A mechanism called a weak reference table is provided in Lua to prompt the garbage collector that if an object, such as the first table key in the code above, is simply referenced by a weak reference table, it can be cleaned up when garbage collection is performed.

The weak reference table in Lua provides a weak reference pattern in 3, where key is weak, value is weak, and key and value are weak references. Regardless of the type of weak reference table, as long as a key or value is reclaimed, the entire entry is removed from the table.

The weak reference type of a table is determined by the __mode field of its meta table. If the value contains the character "K", then the table is the key weak reference and, if it contains "V", is the value if it is referenced, if two characters exist, it is the key/value weak reference. See the following code:

Copy Code code as follows:

A = {}
b = {__mode = "k"}
Setmetatable (A,B)
Key = {}
A[key] = 1
Key = {}
A[key] = 2
CollectGarbage ()
For k,v in pairs (a) do
Print (v)
End
--Output only 2

In the preceding code example, the first key is overwritten by the definition of the second key after it is stored to table A, so its only reference is from the key weak reference table. In fact, this mechanism also exists in Java, and Java in the 1.5 version also provides a set of weak reference containers whose semantics are similar to LUA's weak reference table.

Finally, the weak reference table in Lua is just a variable that acts on a table type, and weak reference tables have no effect on other types of variables, such as values and strings.

1. Memo (memoize) function:

"Space Change Time" is a general program efficiency optimization means, for example: For a normal server, the request it received contains the LUA code, every time it receives a request will call the LUA LoadString function to dynamically resolve the request in the LUA code, If this operation is too frequent, it can result in inefficient server execution. To solve this problem, we can cache the results of each resolution into a table, and the next time, if you receive the same LUA code, you do not need to invoke loadstirng to dynamically parse, but directly from the table to get the parsed function directly executed. This can greatly improve the efficiency of server execution in the case of a large number of repetitive LUA code. Conversely, if a significant portion of the LUA code occurs only once, then using this mechanism will result in a large amount of memory resources being consumed without effective release. In this case, if the use of weak reference table, not only to some extent to improve the operation of the program efficiency, memory resources will be effectively released. See the following code:

Copy Code code as follows:

Local results = {}
Setmetatable (Results,{__mode = "V"}) the key in the--results table is the LUA code in string form
function mem_loadstring (s)
Local res = Results[s]
if res = = Nil Then
res = assert (LoadString (s))
Results[s] = Res
End
return res
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.