Need clarification in regards to garbage collecting references

Hello everyone,

I need help.

For the past few months I have been experiencing memory leaks in my games which I suspect is from the CharacterAppearanceLoaded events that I have implemented within my games which are responsible for giving recently loaded characters outfits and tools.

I have been reading @Hexcede’s great guide on garbage collection but I personally cannot quite grasp the concept of it properly, or rather gain the confidence to say I truly understand it at this time due to my inexperience. The article can be accessed here: Garbage Collection and Memory Leaks in Roblox - What you should know

I just want someone to help clarify my understanding in regards to memory management, memory leaks, and strong references on Roblox.

Here is the scenario: 10000 players join your game over a period of 6 hours

Do any of these examples of code leak memory via non garbage collected references?

EXAMPLE 1

local theValue = {"a", "b", "c"}
game.Players.PlayerAdded:Connect(function()
   print(theValue)
end)

EXAMPLE 2

game.Players.PlayerAdded:Connect(function()
   local theValue = {"a", "b", "c"}
   print(theValue)
end)

@Hexcede mentions at line 3 that we reference a variable but it will never be collected even if the script stops running. Do they mean that the reference at that line inside of the anonymous function is never cleaned up as long as theValue is never set to nil?

If PlayerAdded ran 10000 times does this mean that I will have 10000 non garbage collected references to the theValue table?

Please help clarify my understanding regarding this topic.

I appreciate all of your help in advance to those who have taken the time to read this :handshake:

“Player.CharacterAppearanceLoaded”, when the player instance is removed (PlayerRemoving) all of its event connections are destroyed too.

The connection is only made once per player instance, providing you’re connecting that event correctly.

The first example would leak theValue, as it is used as a upvalue by the function connected to the event which never is never collected as it is connected to an event which in your example, is never disconnected.

@Hexcede mentions at line 3 that we reference a variable but it will never be collected even if the script stops running. Do they mean that the reference at that line inside of the anonymous function is never cleaned up as long as theValue is never set to nil?

If you set theValue to nil, the connected function will no longer hold a reference to the table {"a", "b", "c"}, so yes the table will be collected in the next gc cycle assuming no further references to it are kept:

If PlayerAdded ran 10000 times does this mean that I will have 10000 non garbage collected references to the theValue table?

In the first example, the function connected keeps just 1 internal reference to theValue and will keep additional references whenever it runs. However, In your second example, the function doesn’t keep an internal reference to theValue since it isn’t used as an upvalue but a temporary reference is kept everytime the function runs and cleared out whenever it ends.

1 Like

In the first example, the function connected keeps just 1 internal reference to theValue and will keep additional references whenever it runs.

I understand. These additional references would cause a large leak if the event handler is triggered on a recurring basis in-game right?

No, there’s only 1 reference kept overtime and so no memory leak would build up.

I see. Thank you for the help.