[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: question: lua deterministic gc
- From: Gé Weijers <ge@...>
- Date: Tue, 29 Dec 2020 02:04:42 -0800
On Mon, Dec 28, 2020 at 8:24 AM Thijs Schreijer <thijs@thijsschreijer.nl> wrote:
> First one finalises (calling GC metamethods), 2nd call actually cleans them out
That's assuming you don't write a __gc metamethod that does this:
metatable = {
__gc = function(object)
someGlobal = object
end
}
In this case the in object will not be collected until something
modifies "someGlobal" or the program terminates.
The finalizer will only run once for each object, unless you use
"setmetatable" again, which creates an object that is unreachable but
can never be collected, it's a zombie object of sorts:
local mt; mt = {
__gc = function(ob)
print("collecting")
setmetatable(ob, mt)
end,
}
local object = setmetatable({}, mt)
object = nil
while true do
-- generate lots of trash to keep the garbage collector busy
local trash = ("xxxxx"):rep(1000000)
end
There are plenty of ways to shoot yourself in the foot with __gc.
--
Gé