Do variables in module scripts get garbage collected if they’re no longer referenced?
2 Likes
Generally the rules would be the same as normal.
-- some ModuleScript
-- Never garbage collected (but only because this is returned from the module),
-- because every call to `require` after the first has to return the same table
local a = {}
-- Garbage collected after the module script runs the first time
local b = {}
do
-- Garbage collected after it goes out of scope of the `do ... end` block
local c = {}
end
-- Never garbage collected, because its referenced by `a` and `a` is not
local d = {}
a.d = d
-- Garbage collected only if that `Part` is destroyed/also collected.
local conn = workspace.Part.Touched:Connect(function() print("touch") end)
return a
3 Likes
So these variables would get gc’d if I had my module like this?
local b = {} do
local Variable = 1
local Variable = 2
end
return b
1 Like
They wouldn’t need to be inside that do ... end
block at all. See b
in my original reply. It will be GC’d just fine.
1 Like