Does this create a memory leak?

Does the reference to the player that joined stay even when that player leaves?

local myDictionary = {}
Players.PlayerAdded:Connect(function(Player)
    local userId = Player.UserId
    myDictionary[userId] = true
end)

Yes, it would stay. However, when the server ends (shuts down), it would be lost.

From my understanding, it wouldn’t contain a reference to the player Instance since you’re storing a number which is their UserId. Regardless, you can remove their UserId from the table upon leaving:

local myDictionary = {}

Players.PlayerAdded:Connect(function(Player)
    local userId = Player.UserId
    myDictionary[userId] = true
end)

Players.PlayerRemoving:Connect(function(Player)
    local userId = Player.UserId
    
    if myDictionary[userId] then
        myDictionary[userId] = nil
    end
end)

More info about memory leaks as well as Weak & Strong References (which pertain closer to the scope of this topic) can be found on the following topic:

1 Like

If I don’t do this would it create a problem if more players join and leave?

layers.PlayerRemoving:Connect(function(Player)
    local userId = Player.UserId
    
    if myDictionary[userId] then
        myDictionary[userId] = nil
    end
end)

Depending on what you use the reference for, maybe. If you use the reference to set datastore, then you’d essentially be wiping new memory if they join another server. It’s just better to set it to nil upon removal of player.

If you don’t remove their UserId from the table, it’ll remain there until the server shuts down.

This means that if Roblox, Builderman, Shedletsky, and myself joined a server, the table would look like this, even if 3/4 of those players have already left:

local myDictionary = {1, 156, 261, 32150440}
(Oh and I just defaulted to using an array here, which makes more sense if you’re not intending on having a value other than “true” being associated with the UserIds)

1 Like

say if roblox leaves

would the userid to roblox be garbage collected?

for example

local myDictionary = {
    [156] = true;
    [261] = true;
    [32150440] = true;


}

@StrongBigeMan9 is exactly right, if you don’t remove the player’s dictionary entry when the player leaves, they will pile up in your dictionary if your server stays online very long. In this case it saves just a number (the key) and a boolean (true), so it won’t eat much memory at all… But your lookups in that table can become very slow because the table is so big.

Note that the UserId is an integer number. The convention is usually not to use non-consecutive integers as table keys… Therefore I always use tostring() on the player’s UserId as well before using it as a dictionary key.

1 Like

From my understanding, it wouldn’t be garbage-collected, however as I don’t have the most adept understanding of garbage collection quite yet, I would advise utilizing the following topic for more reliable information:

1 Like

yes it will create a minor memory leak
but it will be so small that it would take years to become an issue