Auto clear table

If you add parts to the table, and then remove these parts from the workspace, will the table be cleaned over time? Or does it need to be cleaned manually?

1 Like

You would still have to remove the parts manually from the table.


Lua automatically trims nils at the end of the table, However a Destroyed Instance does not become nil, only it’s parent does, you need to remove it from the table manually by (for example) table.remove


Suppose there are parts in the model and I will create a table and then delete these parts from the workspace, what will happen to this variable, won’t it reset to zero over time and will always take up space?

local tab = model:GetChildren()`

1 Like

It will still take up space. As the Instances that i assume you “deleted” using Destroy (please use specific terminology) still exist in the table, they are just parented to nil.
Read documentation for Instance:Destroy .

1 Like

local npcHRP = npc:WaitForChild(“HumanoidRootPart”)

That is, after the death of the NPC, the npcHRP variable will occupy memory? What to do with this variable? Manually do all variables nil ?

1 Like
local t = {}
table.Part ="Part")
print(table.Part.Name) -- 'Part'
print(table.Part.Name) -- 'Part'
table.Part = nil
print(table.Part.Name) -- error, attempt to index nil with 'Name'.

Table entries need to be manually cleared.

By the way, As far as i know, the error part might be incorrect. BasePart:Destroy() sets it’s Parent to nil and locks the Parent attribute, among other things. It does not entirely disappear into nil-ness in this case.

By the way, As far as i know, the error part might be incorrect.


Okay then, I now stand corrected.

By the way, As far as i know, the error part might be incorrect.

among other things.

deep breath

A small note on Garbage Collection

Garbage Collection is the process by which LuaU (and most other languages) clean up memory. Whenever you create a variable it takes up space in your computer’s memory, and when more of these variables get created, they clog up the memory resulting in slow performance so that’s why garbage collectors were implemented; they automatically clean up a variable, when it is not in use.

Without them memory management is solely in the hands of the developer, and screwing up is a huge possibility. (that’s why languages like C++ are considered dangerous because they issue the responsibility of memory management on the programmer and if not used correctly, can cause memory leaks.)

Note:- Memory leaks refer to memory that get never get cleaned up.

Types Of Reference

So in LuaU, there are 2 types of reference

  • Weak Reference
  • Strong Reference

Weak References are prone to garbage collection, you might have heard the term weak tables which can be done using the metamethod __mode, and might have been baffled by it, but in reality it just means that the keys/values of the table, depending on how you set it, will eventually get garbage collected.

Strong References are the exact opposite of weak references, they cant be garbage collected. In LuaU, variables, functions, tables (not weak, normal ones) all acquire strong references.

So with that in mind, let’s get back to your question.

local npcHRP = npc:WaitForChild(“HumanoidRootPart”)

Will the above variable get gced when the NPC dies? The answer is yes. Because when the NPC dies the HumanoidRootPart gets set to nil and it gets garbage collected.