Lua table garbage collection

Hi!

If I have a lua table inside a lua table, and I remove all references to the parent table, will the parent & inner table be garbage collected? For example (table inside table):

local table = {};
table[1] = {1, 2, 3};
table[2] = ...

I’ve looked at several sources but I’m still not 100% sure about this. In my case, these are ‘OOP’ objects where a ‘part’ object contains a ‘timer’ object. If I delete all references to the part object, will this object be garbage collected, together with its timer? Or do I have to set all part object properties to nil.

Thanks in advance.

So the better way to do this would be with oop, specifically using 1,2,3 as their own properties.

This not really answers the question :stuck_out_tongue: , I am already using the oop lua syntax. I was curious as to what is garbage collected when deleting all references to a table that may have subtables.

if all references to “table” are lost it will be collected, then all your elements will also lose their reference, so later they will also be collected. And so on. You don’t need to set their fields to nil.

3 Likes

Thanks for the confirmation! .