Clarifications/Questions on memory leaks

I have a few questions about what does and does not cause memory leaks.

  1. If you set up a new variable in a function and have it store an object, what happens to that reference when the function is done running?

e.g

function someFunction()
    local object = workspace.Object
end

When that function has finished running, what happens to that reference? Is it stuck in memory, causing a leak? Or does it get cleaned up?

  1. Does overwriting a Connection cause a leak?

For example,

conn = game.Players.PlayerAdded:Connect(function(plr)
    -- do something
end

conn = workspace.Object.Touched:Connect(function(hit)
    -- do something else
end

Is this a leak, or is the first connection cleaned up/overwritten?

  1. If you have a CharacterAdded function connected to a player, does it cause a leak when the player leaves?

The variable is “destroyed” in sense. The object reference should become garbage collected and memory will be “freed”

No.

All connections on an object are destroyed when the object is destroyed. So when a player leaves all of the connections that are connected to them (and are not referencing anything that might keep them alive such as themself) then you can expect there to be no memory leak.

Just be careful to not do anything that can happen here:

Since local variables cease to exist when their scope ends, that reference will go away

no

But the connection is still connected

no