Is memory garbage collected from deleted scripts?

I’m currently experiencing memory leaks in my game and trying to narrow down where I can find them. Can memory leaks still happen from scripts that get destroyed, or does that memory get garbage collected?

1 Like

Scripts don’t stop running when they’re Destroyed or Disabled, and so any objects they’re keeping alive won’t be cleaned up just from doing that. For example:

script.Disabled = true
print("Ayyy")
local a = Instance.new("Part", game.Workspace)
a.AncestryChanged:Connect(function()
	print("Died")
end)
while wait(1) do
	print("ping")
	wait(1)
	print("pong")
end

If you do this in a place with no base plate, the newly created part falls to it’s death and the script prints “Died”. From this you can see that the connection object sticks around after the script is Disabled, and also that the script keeps running in general.

In general, I would refrain from using destroying or disabling scripts to control logic.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.