lua weak table 概念解析

lua weak table

经常看到lua表中有 weak table的用法, 例如:

weak_table = setmetatable({}, {__mode="v"})

官网上的解释:

http://www.lua.org/pil/17.html

Weak tables are the mechanism that you use to tell Lua that a reference should not prevent the reclamation of an object. A weak reference is a reference to an object that is not considered by the garbage collector. If all references pointing to an object are weak, the object is collected and somehow these weak references are deleted. Lua implements weak references as weak tables: A weak table is a table where all references are weak. That means that, if an object is only held inside weak tables, Lua will collect the object eventually.

大体意思是, 如果一个对象(table1)被另一个对象(table2)引用(reference), 如果table2的表的引用table1方式为弱引用(weak reference), 则 table2是 weak table, table1在被垃圾收集时候不会考虑table2的引用。

Tables have keys and values and both may contain any kind of object.

表可的 key 可以是一个表, 表的value也可以是一个表。

表中元素的 值为 表不稀奇, 但是key可以为表比较新颖, 如下(这种用法,可以做表的扩展描述, 不用修改原表的基础上)

a = {}

b = {}

a[b] = "this is b"

设置弱表方法:

The weakness of a table is given by the field __mode of its metatable. The value of this field, when present, should be a string: If the string contains the letter `k´ (lower case), the keys in the table are weak; if the string contains the letter `v´ (lower case), the values in the table are weak. The following example, although artificial, illustrates the basic behavior of weak tables:

通过设置元表(metatable)的_mode属性, 属性值为 k - key为弱引用, v - 值为弱引用, kv - key 和 值 都为弱引用。

例如

    a = {}
    b = {}
    setmetatable(a, b)
    b.__mode = "k"         -- now `a' has weak keys

当垃圾回收器,将弱引用的对象回收后, 对被引用表的影响,如下。 (表中此元素将消失)

Irrespective of the table kind, when a key or a value is collected the whole entry disappears from the table.

如何理解弱表的行为?

为什么弱表, 会有这种现象, 当 弱引用对象被回收掉, 主表中弱引用的元素也会消失?

为什么这么定义?

首先, 看看其中有两个概念:

1、 引用(reference), 对对象类型的变量, 其实现上都是引用方式。 基本类型中 number string, 非引用类型。

http://www.cnblogs.com/sifenkesi/p/3850760.html

(2)Lua有8种基本类型:nil、boolean、number、string、function、userdata、thread、table。其中Nil就是nil变量的类型,nil的主要用途就是一个所有类型之外的类型,用于区别其他7中基本类型。

  (3)对象objects:Tables、functins、threads、userdata。对于这几种值类型,其变量皆为引用类型(变量本身不存储类型数据,而是指向它们)。赋值、参数传递、函数返回等都操作的是这些值的引用,并不产生任何copy行为。

引用代码示例:

a = {1}

b = a -- a 和 b 都是引用变量, 引用对象 {1}, 可以理解为 c中的指针概念。

非引用代码示例:

a = 1

b = a --- b 和 a 是两份不同的内存空间, 本别存储值 1

2、 弱引用(weak reference)

https://en.wikipedia.org/wiki/Weak_reference#Lua

In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector, unlike a strong reference.

wiki上解释与lua中弱表引用概念一样, 如果是弱引用, 则此引用关系, 不能阻止 对象对 垃圾回收。

一旦对象 被回收后, 此弱引用, 对应的表中的元素, 也将随之消失。

Linux 软硬连接 类比

Lua strong 和 weak 引用 与 linux 软连接概念类似,

硬连接可以增加文件的引用数目, 并且共享同一个inode,

但是软连接, 新建一个不同的inode, 仅仅是链接到目标文件的inode。

如果一个文件有两个硬链接, 无论删除任何其中一个连接, 则文件不会被删除, 如果最后一个硬链接也被删除, 则文件会被删除。

如果一个文件有一个硬链接 和 一个软链接, 删除硬链接, 则文件 会被删除, 同时软链接同步被删除。

关于软硬连接概念和实例, 请参考:

http://www.cnblogs.com/itech/archive/2009/04/10/1433052.html

参考代码:

[oracle@Linux]$ echo "I am f1 file" >>f1
[oracle@Linux]$ cat f1
I am f1 file
[oracle@Linux]$ cat f2
I am f1 file
[oracle@Linux]$ cat f3
I am f1 file
[oracle@Linux]$ rm -f f1
[oracle@Linux]$ cat f2
I am f1 file
[oracle@Linux]$ cat f3
cat: f3: No such file or directory

下面类比关系

Strong reference                       hard link
Weak reference                       Soft link

LUA EXAMPLE CODE

VALUE作为 weak 引用

--- weak table
a = {}
b = {}
setmetatable(a, b)
b.__mode = "v" -- now `a' has weak value
key = {} -- creates second key
a["aa"] = key
key = nil
collectgarbage() -- forces a garbage collection cycle
for k, v in pairs(a) do print(v) end
  --> 0 entry

key作为 weak 引用

a = {}
b = {}
setmetatable(a, b)
b.__mode = "k" -- now `a' has weak value
key = {} -- creates second key
a[key] = 2
key = nil
collectgarbage() -- forces a garbage collection cycle
for k, v in pairs(a) do print(v) end  
-----> 0 entry
原文地址:https://www.cnblogs.com/lightsong/p/4899439.html