Parenting to nil and dataleak

If you keep parenting objects to nil is it possible to create a data leak?

How do you prevent this?

Lua’s garbage collector will reclaim memory that you can no longer reference (ie. you lose track of every variable that refers to an instance, and the instance is no longer parented to anything). You can create a memory leak if you keep references after you no longer need them. Parenting instances to nil will not create a memory leak unless you keep a reference to the instance in a variable somewhere. Once instances are no longer referenced in code or their variables go out of scope (ie. when an if block containing local variables ends, those variables will go out of scope), and they have no parent, they will be destroyed and their memory will be reclaimed.

A common case where a memory leak can happen is keeping a Player or information about a player as a table’s key or value, and not removing it when they leave the server.

You can also find a lot of discussion about memory leaks already by using the search bar.

10 Likes