lua垃圾收集的原理 参见: http://lua-users.org/wiki/EmergencyGarbageCollector
Notes about how the Lua garbage collector works
Disclaimer: This is the first time I have worked on a garbage collector so some of this may be incorrect. Corrections/cleanups are welcome. --RobertGabrielJakabosky
"While working on this patch I had to learn how the garbage collector in Lua works. I am writing this to help me later if I need to fix more bugs with the collector and I hope this info can help other people who are interested in how the Lua garbage collector works." --RobertGabrielJakabosky
Simple description
The Lua garbage collector is a mark & sweep collector. The collector has two major phases mark & sweep that it runs each collection cycle. During the mark phases the collector traverse the Lua stack and into tables to mark values it finds as live. Next the sweep phases will walk a list of all collectible values and free all dead values it finds.
Detailed description
All collectible type objects have a 'marked' bit field. The bits are defined as (copied from header "lgc.h"):
* bit 0 - object is white (type 0)
* bit 1 - object is white (type 1)
* bit 2 - object is black
* bit 3 - for userdata: has been finalized
* bit 3 - for tables: has weak keys (note this bit has two different meanings one for userdata and one for tables)
* bit 4 - for tables: has weak values
* bit 5 - object is fixed (should not be collected)
* bit 6 - object is "super" fixed (only the main thread)
The garbage collector keeps track of a current white (type 0 or 1) and objects with the other white are dead objects that can be collected during the sweep states.
An object's color is defined by which of the first 3 bits (0, 1, 2) are set:
* It is white if one of the two white bits (0,1) are set and the black bit is clear. Only one white bit should be used by a white object.
* It is gray if all three color bits (0,1,2) are clear.
* It is black if the black bit is set and the two white bits are clear.
Garbage collector states (each collection cycle passes through these states in this order):
* GCSpause - Start of collection cycle. At this state all objects should be marked with the current white. The main lua_State, globals table, registry, and metatables are marked gray and added to the gray list. The state now changes to GCSpropagate.
* GCSpropagate - Each object in the gray list is removed and marked black, then any white (type 0 or 1) objects it references are marked gray and added to the gray list. Once the gray list is empty the current white is switched to the other white. All objects marked with the old white type are now dead objects. The state now changes to GCSsweepstring.
* GCSsweepstring - The color of each string in the internal strings hashtable is checked. If the color matches the old white type that string is dead and is freed. If the color matches the current white (newly created string) or is gray (some other object references it), then it is alive and it's color is reset to the current white. Once all strings are checked the state is changed to GCSsweep.
* GCSsweep - The color of each objects in the global rootgc list (this list holds all objects except strings) is checked just like the strings during the GCSsweepstring state. Dead objects are freed and removed from the rootgc list. Live objects have their color reset to the current white. Once all objects have been checked the state is changed to GCSfinalize.
* GCSfinalize - This state will finalize all dead userdata objects by running their "__gc" metamethod. Once all dead userdata objects have been finailzed the state is changed to GCSpause and this completes a cycle of the garbage collector.
--RobertGabrielJakabosky
还有一本Garbage Collection垃圾收集的书 国内翻译的也许对你理解Lua的gc运作有帮助。
分享到:
相关推荐
LUA comes with its own library that contains ready-to-use functions but I prefer you to explain the complete version so you can understand better how LUA works. In order to execute LUA scripts you ...
The book is targeted at people with some programming background, but it does not assume any prior knowledge about Lua or other scripting languages. When you buy a copy of this book, you help to ...
所有版本LUA源码 lua-5.3.5 lua-5.3.4 lua-5.3.3 lua-5.3.2 lua-5.3.1 lua-5.3.0 lua-5.2.4 lua-5.2.3 lua-5.2.2 lua-5.2.1 lua-5.2.0 lua-5.1.5 lua-5.1.4 lua-5.1.3 lua-5.1.2 lua-5.1.1 lua-5.1 lua-5.0.3 lua-...
LUAC脚本是一种基于Lua语言的编译格式,它将Lua源代码编译成字节码,以便在 Lua 解释器上高效运行。LUAC(Lua Compiler)是Lua官方提供的编译器,它将源代码转换为这种优化的字节码,以提高执行速度。在游戏开发、...
Lua源代码是文本形式的,易于阅读和编写,但为了保护代码不被轻易篡改或盗用,开发者通常会将Lua代码编译成字节码(.lua.c文件或.luac文件)。"LUAC"就是Lua的官方编译器,它将Lua源代码转换为字节码,以提高执行...
"LUAC解密工具"就是针对Lua编译后的二进制文件(.luac)进行解密的工具,目的是为了让加密过的Lua代码能够恢复成可读的源代码格式(.lua)。 LUAC是Lua的编译器,它将Lua源代码转换为字节码,这个过程通常是为了...
Contents Preface xiii I The Language 1 1 Getting Started 3 1.1 Chunks 4 1.2 Some Lexical Conventions 5 ...1.4 The Stand-Alone Interpreter 7 ...31.2 The Garbage Collector 295 Index 299
ios版本将lua加密成luac
《Lua在STM32F407上的移植与应用》 在嵌入式系统领域,为了提高代码的可读性、可维护性和灵活性,越来越多的开发者选择使用高级脚本语言,如Lua,来替代传统的C/C++编程。本文将详细探讨如何在STM32F407微控制器上...
内含luaDll,luacom以及luaiconv源码。luaDll和luaiconv直接打开sln在Visual studio中即可进行编译。luacom可使用nmake进行编译。也可直接使用已经生成了的dll.
Lua是一种轻量级的脚本语言,常用于游戏开发、嵌入式系统和服务器配置等领域。LuaEditor是一款专为Lua编程设计的中文编辑器,它为程序员提供了方便的开发环境,提高了编写和调试Lua代码的效率。luaEditor-v4.10是该...
使用`lua_pushnumber`、`lua_pushstring`等函数将C++数据推送到Lua栈,使用`lua_tonumber`、`lua_tostring`等函数从Lua栈获取数据。 6. 清理:调用`lua_settop(L, 0)`清空栈,防止内存泄漏。 二、Lua调用C++函数 ...
4. **Lua.dll**:这是Lua51.DLL,是Lua解释器的核心部分,包含了Lua的所有运行时功能。在易语言中,可以通过动态链接这个DLL来调用Lua的功能。 5. **测试.e**:这是一个易语言的源代码文件,很可能包含了如何在...
If you just want to know how many bytes the string occupies, so that you can make space for copying it into a buffer for example, then the existing Lua function string.len will work. You might want ...
本示例中的"lua_test.rar"文件集是一个关于C++与Lua交互的实践案例,主要探讨了如何通过Lua脚本调用C++编写的函数。下面我们将详细探讨这个主题。 首先,Lua是一种轻量级的脚本语言,它简洁、易学,常被用于游戏...
LuaBitOp-1.0.2 是一个针对 Lua 语言的位操作库,它提供了对二进制数据进行位运算的功能。位操作是计算机科学中的基础概念,它们在底层编程、数据处理和优化中有着广泛的应用。LuaBitOp 的源码可以帮助开发者深入...
Lua for Windows is a 'batteries included environment' for the Lua scripting language on Windows. Lua for Windows (LfW) combines Lua binaries, Lua libraries with a Lua-capable editor in a single ...