When you have a table and you want to garbage collect it (free up space, etc.) - you simply NIL it correct?

Hello everyone, if I have a table that I am not using anymore and I want to garbage collect it, I simply NIL the table and Lua will take care of it?

Suppose this table:

local testTable = {
Name = “Super Big Brick”,
BigBrick = Instance.new(“Part”)
}

And then I do:
testTable.BigBrick:Destroy()
testTable = nil

It will garbage collect and free up space, right? Am I understanding this correctly?

2 Likes

Basically, yes. It’ll just let the garbage collector clean it up, like you have said.

2 Likes

Technically yes this is a valid way to do this if you want to get rid of the table entirely,

but if you want to keep the table itself and just clear the contents of it (example: playing players and the round ends), you can use the helpful method: table.clear(table).

This method only requires one parameter and its just the table! This is very useful in the case of storing stuff for a limited time and you need to re-use it so you don’t have to “restructure” the table by adding the braces back!

You can view the official documentation here!

Happy Scripting!

2 Likes

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