Introduction to weak references in Lua _lua

Source: Internet
Author: User

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.

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.