A weak reference to the Lua tutorial Table_lua

Source: Internet
Author: User

This time I want to introduce a little bit, just one--weak reference table

1. Intelligence beyond human wisdom-defects in automatic memory management

We all know that Lua is equipped with automatic memory management, well, maybe some friends don't know.

We just create objects without deleting objects (of course, you need to set nil values for unwanted objects), and Lua automatically deletes objects that are considered junk.

The problem is now, what objects are garbage objects, and sometimes we know that some object is rubbish, but Lua can't find it.

such as an example:

Copy Code code as follows:

t = {};

--use a table as the key value for T
Key1 = {name = "Key1"};
T[key1] = 1;
Key1 = nil;

--and use a table as the key value for T
Key2 = {name = "Key2"};
T[key2] = 1;
Key2 = nil;

--Force a garbage collection
CollectGarbage ();

For key, value in pairs (t) do
Print (Key.name. ":" .. Value);
End

This code is a bit complicated, and the IQ of less than 250 may not be understood.

First with a table, called T.

Then create a new table--key1, this key1 as the key value for T, add a field to T, assign a value of 1.

Similarly, Key2 is also used as a key value for T.

Next, we call the CollectGarbage function, and whatever it is, we just know that it lets Lua do a garbage collection.

The last output is all the fields of T, and the results are as follows:

Copy Code code as follows:

[Lua-print] Key1:1
[Lua-print] Key2:1

It's very common sense, and in our estimates, even though we're assigning T, Key1 and Key2 are assigned to nil.

However, key values that have been added to the table will not be treated as garbage.

In other words, the key1 itself is already a nil value, but the content it once pointed to is still stored in T. The same is true of Key2.

So we can finally output the Key1 and Key2 name fields.

2. Subversion of your perceptions-weak reference table

Just the example is normal, then, if we take a table as the key value of another table, we hope that when the table is set to the nil value, the other table that field is also deleted.

How should it be implemented?

At this point, the weak reference table is used, and the implementation of the weak reference table also takes advantage of the meta table.

Let's take a look at the following code, almost as before, just add a code:

Copy Code code as follows:

t = {};

--Set a meta table for T, add a __mode method, and assign a value of "K"
Setmetatable (t, {__mode = "k"});

--use a table as the key value for T
Key1 = {name = "Key1"};
T[key1] = 1;
Key1 = nil;

--and use a table as the key value for T
Key2 = {name = "Key2"};
T[key2] = 1;
Key2 = nil;

--Force a garbage collection
CollectGarbage ();

For key, value in pairs (t) do
Print (Key.name. ":" .. Value);
End

Note that when T is created, it is immediately set to the meta table, which has a __mode field and is assigned a "K" string.

If you run the code at this point, you will find that nothing is output, because none of the fields of T exist.

This is one of the weakly referenced table, which adds the __mode element method to the table, and if the value of the Meta method contains the string "K", it means that the key of the table is weakly referenced.

Once the reference to the key value has been canceled elsewhere (set to nil), the field in this table will also be deleted.

In layman's parlance, because the key of T is set to a weak reference, the field does exist in T after executing t[key1] = 1.

Subsequently, the Key1 = nil is executed, and there is no place for Key1 to be referenced except T itself, so the Key1 field of T is also deleted.

3. Three types of weak references

For a weak reference table, there are actually three different forms:

1 key value Weak reference, that is, just said the case, as long as other places do not have a reference to the key value, then the table itself this field will also be deleted. Set method: Setmetatable (t, {__mode = "k"});
2 The value of the weak reference, the case is similar, as long as other places do not reference value value, then the table of the value of the field will also be deleted. Set method: Setmetatable (t, {__mode = "V"});
3 Key and value weak references, same rules, but key and value both take effect, and any one that works will cause the table's fields to be deleted. Set method: Setmetatable (t, {__mode = "kv"});

Of course, what is said here is deleted, which means that when the LUA performs garbage collection, it does not necessarily take effect immediately.
We've just been forced to do garbage collection just for testing purposes.

4. End

Well, this time the content is relatively few, in fact, there are quite a few examples of weak references in the book.

The most basic part of LUA is over here.

The following is an introduction to some libraries and a bit more in-depth (c and Lua calls, custom types, threads, memory management).

Next May slow down the article update speed, because many library's introduction, does not know has the necessity to use the article to record down.

Maybe just.

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.