Garbage collection and functions #1820
-
I was recently doing some testing with what would/wouldn't cause an object to be collected, and I noticed that no-longer-accessable functions aren't being cleared on garbage collection. Is this an intentional difference to regular lua, and if so, why? I used the below code for testing: local t = setmetatable({
"hi", -- Shouldn't collect
function() end, -- Should collect
{}, -- Should collect
{function() end}, -- Should collect
}, {__mode="v"})
print(t[1], t[2], t[3], t[4])
collectgarbage()
print(t[1], t[2], t[3], t[4]) -- t[2] won't collect in luau? In regular lua 5.1, everything except |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This function is still reachable through the table literal stored in the constant table of the top-level function prototype. |
Beta Was this translation helpful? Give feedback.
-
Ah, I forgot that functions become constants due to optimisations (and also running it with |
Beta Was this translation helpful? Give feedback.
This function is still reachable through the table literal stored in the constant table of the top-level function prototype.
Luau can store more types of literals in the constant table than Lua.