Would this be a cause for memory leaks?

I was wondering if a connection to a part would stay in memory and not get automatically disconnected regardless if the part destroyed if another part instance is created inside of the connection.

This was brought to my mind when I read this post: Why is my game lagging? - #9 by cjjdawg

4 Likes

Instances when destroyed get their connections automatically disconnected, however those created in the connection won’t have their connections disconnected until they too are destroyed or disconnected manually.

3 Likes

Memory Leaks happens with some reasons such as:
1- Many connections that arent being disconnected.
2- So many scripts and parts.
3- Global functions, variables.
4- Many threads being created (Coroutines, spawn, delay)

But yes the connections are disconnected once a part is destroyed.

(Other way of disconnecting is making a variable have a nil value and then setting it to the connection, later on doing variable:Disconnect())

4 Likes

Thanks, I wrote a little connection and was wondering if you can confirm there is no memory leak taking place.

local PartA = Instance.new("Part", workspace)
workspace.PartA.Touched:Connect(function()
	local PartB = Instance.new("Part", workspace)
	PartA:Destroy()
end)
1 Like

Yes there is no memory leak in this one since the connection is only used once and then the part is destroyed.

(Logically, memory leaks by using connections is doing a connection that you arent gonna need anymore but still not getting rid of it.)

2 Likes

This is not correct, only the first point in the post is correct, I recommend you read the following articles:

3 Likes

I didnt see where I was wrong at but thank you!, These posts helped me to understand more things I didnt know.

So many scripts and parts - this statement is very vague and having a lot of parts and scripts itself will not cause a memory leak.

Global functions and variables - have nothing to do with memory leaks

Many threads being created - have nothing to do with memory leaks.

Memory leaks are only caused by not destroying parts created in scripts when no longer needed, never disconnecting events on instances not destroyed or not removing references to instances in the workspace contained in a table.

1 Like

Well the things I’ve said were based on a memory leak topic ive seen (I forgot where it is located at) it wasnt in this forum but thanks I guess.

1 Like

Sorry for bumping, but I would just like to confirm one thing then: @AstralBlu_e’s solution to my code snippet is still correct, right?

Yeah destroying the part destroys any connections, but ensure you remove all references to the part.

2 Likes

if something falls out of the world into the void, is it disconnected. (like the 10 scripts inside a model)