Will this be garbage collected?

Hello. Lets say I create some kind of class:

local classObject = classModule.new(Put_Some_Args_In_Here)

Now lets say that at some point I now no longer need this class, and need to destroy it. Can I just set the classObject variable equal to nil? Like this?

local classObject = classModule.new(Put_Some_Args_In_Here)
-- Running a whole bunch of code
-- ...
-- Done running this code, and I no longer need this class
classObject = nil

This is setting the classObject variable to nil. But, is it’s previous value still stored in memory somewhere? Or is it’s previous value also destroyed now?

I’m pretty confident that any data with no references will be garbage collected. This is what I use in my scripts with no issues.

3 Likes

When the variable goes out of scope or when it’s reference is set to nil, the garbage collector should collect it unless something else is still holding a reference to it. You should check your classModule to make sure it doesn’t put the table inside of some other table, if it does do that you’ll have to add a :Destroy() function to it to get rid of those references.

2 Likes

If there is no reference to it, it should be garbage collected. Make sure that any instances or connections associated with your object have been properly taken care of.

1 Like