2

I am trying to implement a lua runtime and while figuring out the design of garbage collection, i saw that Lua actually specifies a defined order in which destructors of UserData or Table should run.

In Lua 5.4 Reference Manual: 2.5.3 – Garbage-Collection Metamethod

For an object (table or userdata) to be finalized when collected, you must mark it for finalization. You mark an object for finalization when you set its metatable and the metatable has a __gc metamethod. Note that if you set a metatable without a __gc field and later create that field in the metatable, the object will not be marked for finalization.

In the simple case of setting metatables for UserData A first and then B second, i understand that i should call B.__gc first and then A.__gc.

But, if i have two objects A and B, and i set a metatable (with __gc) for A, then B and then A again, what will be the order of finalizers being run?

will __gc method be called for A first or B first?

I can't rely on the order used by lua5.4 because it is the reference (and only) implementation.

3
  • you can log in finalizer to discover =) Dec 28, 2022 at 4:28
  • @DenisDosSantosSilva I can't rely on the order used by lua5.4 because it could be wrong :)
    – red artist
    Dec 28, 2022 at 5:26
  • The features of the reference implementation are frequently becoming the part of the language. You can rely on the official implementation of Lua.
    – ESkri
    Dec 28, 2022 at 9:33

1 Answer 1

0

I got the answer from Lua mailing list. http://lua-users.org/lists/lua-l/2022-12/msg00123.html

First, objects without any references can be considered "dead". but only after their finalizers are called, they can be truly garbage which needs to be cleaned up.

Second, setting a metatable (with a __gc method) for a table/userdata makes it finalize-able. after that we can change the __gc method in the metatable or even remove that method, and it will all be valid. because the only thing that matters is that when we set a metatable with __gc method, we make it a finalize-able object. when we finally find the object dead, we will check the metatable and only run the __gc method if it exists. it doesn't matter if it is the same __gc method which existed when the object was marked for finalization.

with that, we have our answer. finalizers of younger objects MUST be called before older objects. so, if we set the metatable for A, B and then A again, we will just call the finalizer for B first and then A as that is the order in which they were marked for finalization.

Finally, the important thing to remember is that, the order only exists for dead objects. It means we have to make sure that if we are running the finalizer of a dead object, then all younger finalize-able objects MUST be alive, otherwise we are breaking the assumption of the manual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.