Weak table in Lua -- weak table

Source: Internet
Author: User

Weak table is a very interesting thing, such as C ++/Java. A weak table is defined as a weak table is a table whose elements are weak references. A weak referenced table is called a weak table. If there is a weak reference, there will be a strong reference, and if there is a reference, there will be a non-reference. First, we need to define these basic concepts: variables, values, types, and objects.

(1)Variables and values:Lua is a dynamically typed language, that is, in Lua,The variable has no type., It can be anything, andValue TypeSo there is no variable type definition in Lua. In addition, all values in Lua are first-class values ).

(2)Lua has eight basic types:Nil, Boolean, number, String, function, userdata, thread, table. Among them, Nil is the type of Nil variable, and nil is mainly used for a type other than all types, used to distinguish other basic types.

(3)Object objects:Tables, functins, threads, and userdata. For these value types, the variables are all reference types (the variables themselves do not store type data, but point to them ). Value assignment, parameter transfer, and function return all operate on the reference of these values and do not produce any copy behavior.

  Lua's garbage collection mechanism:GC is a common mechanism in many languages. It allows programmers to manage complicated and error-prone memory.

Definition: Lua manages memory automatically by runningGarbage CollectorTo collect allDead objects(That is, objects that are no longer accessible from LUA ).

Three points: (1) Automatic GC running or manual calling; (2) objects with 0 reference count are automatically collected; (3) Dead objects: the object that cannot be accessed does not point to it by reference. Of course, it cannot be accessed, which is equivalent to junk memory.

 

  Weak table definition:

(1) A weak table is a table that has a retriable table, and retriable defines the _ Mode Field;

(2) The reference in the weak table is a weak reference, which does not change the reference count of the object. In other words, if an object only points to a weak reference, GC will automatically recycle the memory of the object.

(3) The _ mode field can have the following three values: K, V, and KV. K indicates table. the key is weak, that is, table keys can be automatically GC; V indicates table. value is weak, that is, table values can be automatically GC; KV is a combination of the two. In any case, as long as one of the key and value is GC, the key-value pair is removed from the table (in any case, if either the key or the value is collected, the whole pair is removed from the table ).

For a normal strongly referenced table, when you put the object into the table, a reference is generated, even if there is no reference to the elements in the table elsewhere, GC will not recycle these objects. There are only two options: manually release table elements or keep them in memory.

strongTable = {}strongTable[1] = function() print("i am the first element") endstrongTable[2] = function() print("i am the second element") endstrongTable[3] = {10, 20, 30}print(table.getn(strongTable))            -- 3collectgarbage()                        print(table.getn(strongTable))            -- 3

However, in a programming environment, sometimes you are not sure to manually assign a key value to nil, but need to release it after all users are used up, yes, you can access this key-value pair. In this case, the weak table will be used. For more information about weak table, see the following small example:

Weaktable = {} weaktable [1] = function () print ("I am the first element") endweaktable [2] = function () print ("I am the second element ") endweaktable [3] = {10, 20, 30} setretriable (weaktable, {__ mode = "v"}) -- set to weak table print (table. getn (weaktable) -- 3ele = weaktable [1] -- Add a reference collectgarbage () print (table. getn (weaktable) -- 1, the first function reference is 1, cannot gcele = Nil -- release reference collectgarbage () print (table. getn (weaktable) -- 0, no other references, All GC

Of course, in the actual code process, we do not need to manually collectgarbage, because this function runs automatically in the background and has its own running cycle and regularity, it is transparent to programmers.

Note: Only objects with display structures are automatically removed from the weak table. Values of Boolean and number are not automatically removed from weak. While the string type is also cleaned by GC, the string type is not displayed in the construction process, so it will not be automatically removed from the weak table, and there is a separate policy for string memory management.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

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.