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] =
key = nil
collectgarbage() -- forces a garbage collection cycle
for k, v in pairs(a) do print(v) end
-----> 0 entry

lua weak table 概念解析的更多相关文章

  1. Lua中的weak表——weak table

    弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...

  2. Lua中的weak表——weak table(转)

    弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...

  3. Lua 基础之Weak Table(5)

    Lua垃圾收集策略 Lua自动进行内存的管理.程序只能创建对象,而没有执行删除对象的函数.通过使用垃圾收集技术,Lua会自动删除那些失效的对象,也就是引用为0 的对象.但是呢?有些对象,引用没有指向它 ...

  4. Lua弱表Weak table

    定义:弱表的使用就是使用弱引用,很多程度上是对内存的控制. 1.weak表示一个表,它拥有metatable,并且metatable定义了__mode字段. 2.弱引用不会导致对象的引用计数变化.换言 ...

  5. 手游开发之lua的table 元表的运用

    元表在项目中的运用,其中就包括元方法这点.元方法是指__index和__newIndex,下面我总结下,更详细的例子讲解可以参考<lua程序设计 第2版>的第13章内容.长h短说,简言之有 ...

  6. mongodb基本概念解析

    MongoDB 概念解析 不管我们学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是文档.集合.数据库,下面我们挨个介绍. 下表将帮助您更容易理解Mongo中的一些概念: SQL术 ...

  7. MongoDB学习笔记—概念解析

    Mongo基本概念 下表将帮助您更容易理解Mongo中的一些概念: SQL术语/概念 MongoDB术语/概念 解释/说明 database database 数据库 table collection ...

  8. 打印Lua的Table对象

    小伙伴们再也不用为打印lua的Table对象而苦恼了, 本人曾也苦恼过,哈哈 不过今天刚完成了这个东西, 以前在网上搜过打印table的脚本,但是都感觉很不理想,于是,自己造轮子了~ 打印的效果,自己 ...

  9. lua的table表处理 及注意事项

    lua,一款很轻量级很nice很强大的脚本语言,做为lua中使用最为频繁的table表,在使用之时还是有颇多的好处与坑的: 下面是大牛 云风的一片关于lua table的blog,可使得对lua ta ...

随机推荐

  1. Leetcode Integer to Roman

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  2. Codeforces Round #248 (Div. 2) A. Kitahara Haruki's Gift

    解决思路是统计100的个数为cnt1,200的个数为cnt2 则 cnt1    cnt2 奇数      奇数 奇数      偶数 偶数      奇数 偶数     偶数 当cnt1为奇数时一定 ...

  3. 【BZOJ】1006: [HNOI2008]神奇的国度

    http://www.lydsy.com/JudgeOnline/problem.php?id=1006 题意:在一个弦图中找最少染色数.(n<=10000, m<=1000000) #i ...

  4. nodejs高大上的部署方式-PM2

    1.最常用的属nohup了,其实就是在后台执行进程,末尾加个&   [zhoujie@ops-dev ~]$ nohup node /home/zhoujie/ops/app.js & ...

  5. Hashtable在ViewState中无法增加值

    在我调试程序的时候,我发现WebForm 2.0和MVC3解析ViewState的方式不同,同样的代码,在Weorm中管用,在MVC中不起作用. private Hashtable ht { get ...

  6. 李洪强iOS经典面试题131

    培训机构量产iOS程序员,导致了现在iOS就业的浮躁和困难.但是技术好的人仍然不愁工作,而一些想进入行业捞一笔就走的人,势必在今年这种艰难就业形式下,被迫淘汰,转行. look.jpg 这张图是git ...

  7. 【android design】android常用设计资源

    一.概述 大部分程序员擅长开发,但是对于设计却知之甚少.这直接导致,程序员在初期开发出来的应用(大多为兴趣或实用导向)中看不中用.因此,有必要搜集整合一些设计资源,这样既能减轻程序员在设计上所耗费的时 ...

  8. mysql双主复制总结

    双主复制: 1).在两台服务器上各自建立一个具有复制权限的用户: 2).修改配置文件: # 主服务器A上 [mysqld] server-id = 10 log-bin = mysql-bin rel ...

  9. java中用过滤器解决字符编码问题

    java的web程序经常出现中文乱码的问题,用一个实现了Filter接口的过滤器类可以较好地解决这个问题. 方式一 EncodingFilter import java.io.IOException; ...

  10. html图片和文字的细节

    ul中的每一个li如果里面添加“一个图,一行字”, 这样图片会紧贴在左侧,而文字会居中,这两个元素不会紧贴着. 产生这种问题的原因我推测是:我图片设置了左浮动,但文字没有设置浮动,而一旦将文字设置为浮 ...