How do I let garbage collection eat self (metatables)

Been using metatables for a while but I cant quite tell if I’m removing self correctly

How would I get garbagecollection to completely remove this object? do I delete any instances related towards to it? What requirements need to be in order to remove this object from memory?

It just looks like you have to delete

(you may just have to set those properties that are referencing instances to nil but I haven’t done this in a while, all I remember is that instances are always strong references)

any referenced instances (like you said) and then set the __mode metamethod

Loop through self and check the type of data (typeof(v) == "Instance") and if it’s an instance, then destroy it first and assign it as nil; if not, then simply set it to nil.

After all that, you can set self and all other variables referencing self as nil.

The gc automatically collects any variable that is nowhere referenced, including tables since they are references

local t = {} --// assuming you have bunch of keys and instances

for key, value in next, t do
   t[key] = nil
end

t = nil --// boom table no longer lives in memory

--// gc automatically collects for loop key, value pairs too after 
2 Likes