gpt4 book ai didi

lua - 为什么我们需要调用Lua的collectgarbage()两次?

转载 作者:行者123 更新时间:2023-12-03 00:37:01 32 4
gpt4 key购买 nike

我遇到过several places人们打电话的地方 collectgarbage() 两次完成所有未使用的对象。

这是为什么呢?为什么一次调用还不够?为什么不打三个电话?

当我尝试以下代码(在 Lua 5.2 上)时,只需调用一次 collectgarbage 即可最终确定该对象(这意味着:它的 __gc 被调用):

do
local x = setmetatable({},{
__gc = function() print("works") end
})
end
collectgarbage()
os.exit()

这是否意味着一次调用就足够了?

最佳答案

Programming in Lua 第 3 版 §17.6 Finalizers 对此进行了解释。简而言之,就是因为复活。

终结器是与对象关联的函数,当该对象即将被收集时调用该函数。 Lua 使用 __gc 元方法实现终结器。

问题是,当调用终结器时,在某些情况下对象必须处于事件状态。 PiL 用这个例子解释了这一点:

A = {x = "this is A"}
B = {f = A}
setmetatable(B, {__gc = function (o) print(o.f.x) end})
A, B = nil
collectgarbage() --> this is A

The finalizer for B accesses A, so A cannot be collected before the finalization of B. Lua must resurrect both B and A before running that finalizer.

复活是调用collectgarbage两次的原因:

Because of resurrection, objects with finalizers are collected in two phases. The first time the collector detects that an object with a finalizer is not reachable, the collector resurrects the object and queues it to be finalized. Once its finalizer runs, Lua marks the object as finalized. The next time the collector detects that the object is not reachable, it deletes the object. If you want to ensure that all garbage in your program has been actually released, you must call collectgarbage twice; the second call will delete the objects that were finalized during the first call.

关于lua - 为什么我们需要调用Lua的collectgarbage()两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28320213/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com