Will this get garbage collected?

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.

2 Likes

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.

2 Likes

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.

1 Like

(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

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.