local table = {}
table[1] = someInstance
table = nil
Will table
and the someInstance
reference get garbage collected ? In theory it should since table
is pointing to nil
and so is someInstance
.
Thanks
local table = {}
table[1] = someInstance
table = nil
Will table
and the someInstance
reference get garbage collected ? In theory it should since table
is pointing to nil
and so is someInstance
.
Thanks
this table will be garbage collected after you do
But I donât think
will be garbage collected because the variable isnât being set to nil. Also of course the instance the reference points to will only be garbage collected if itâs parented to nil.
But if the someInstance
variable is created inside of a function then it will be garbage collected when the function ends.
So the reference someInstance
will still be present ? Doesnât that mean table
will also not be garbage collected since thereâs a strong reference inside that table ?
Iâm not sure about strong and weak references, but I think since nothing is referencing the table it will be garbage collected. References donât work both ways, youâd need another reference.
local a = {
b = {
1
}
}
a = nil
It would be weird if the table wasnât garbage collected, just because a
holds a strong reference to b
that isnât being set to nil.
In general it will be garbage collected if there is no symbol (variable name) that can reach it. table[1]'s contents will be garbage collected unless there is another reference to it. An instance being in the workspace counts as a reference, since it has a Parent and that Parent must be able to access its children. Also keep in mind that local variables, unless they are tables, are not strictly garbage collected when they go out of scope; instead they âfall offâ the stack, but the effect to you is the same, you can no longer access it.
So this means that the table wont be garbage if it has a reference somewhere pointing to it ? I assume this is the same case in OOP Objects, in which the table (Or the Object) is never garbage collected if other scripts require and instantiate it ?
Yes, objects (userdata) and tables are treated the same for garbage collecting.
(Kind of unrelated to this topic) But, how would I remove those references to OOP objects exactly ? Considering that I require them in a âglobalâ context (Which is just the outside scope of a script, in which inner scopes such as connections utilize them).
I tried searching some topics but its either doesnât make any sense or is not feasible
You remove refernces by making them (or letting them become) inaccessible.
u = {}
function a()
local b = {}
if florp then
local t ={}
end
--Table t can be garbage collected here, since it fell out of scope and is not accessible
end
a()
--Table b collected after a() returns as theres no way to access it
--talbe u will be garbage collected when the script stops, since its global
I know this is a completely different language but Rust does a better job visually conveying how this works by explicitly listing lifetime annotations: Validating References with Lifetimes - The Rust Programming Language
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.