Would this cause a memory leak?

Memory leak example:

do 
    local p = Instance.new('Part')
    p.Touched:connect(function() 
       print(p) 
    end)
end

I saw in an example explaining that p is never set to nil within the function so it never gets GCed

Would the example below also be a memory leak?

do 
    local p = Instance.new('Part')
    p.Touched:connect(function() 
       if p.Name == "Part" then
          print("hello")
       end
    end)
end

Assuming that the part gets destroyed at some point, then no it is not a memory leak because the do scope will delete the variable, if it was not in a scope then it would be a memory leak and you would need to set it to nil

p = nil

as for the event connection, when a part is destroyed the connections are automatically cleared

Something to also keep in mind is that if your game suffers from lag over time, and you thinkit may be be caused by a memory leak, the memory leak isn’t always your fault; roblox has historically had memory leaks in there own functions… Still though is good to check for memory leaks in your own code

Referring to this post.

do 
	local baseplate = game.Workspace:WaitForChild("Baseplate")
	baseplate.Touched:Connect(function(hit)
		print(baseplate)
	end)
end

I’ve named the script “MEMORY TEST” and here are the results


The data never goes up after reaching .002

There doesn’t seem to be a leak with this example.

In the example below, the memory also sits at .002. If you can explain the difference of the two I’d appreciate it! I’ve been looking for the memory leak within my game with no luck.

do 
	local baseplate = game.Workspace:WaitForChild("Baseplate")
	baseplate.Touched:Connect(function(hit)
		print("touched")
	end)
end

The guy who made that post is incorrect or at the very least explained it without enough information, the way he explained his post requires a lot of assumpations to be made by the reader

The way he explained memory leaks is very strange to me so I could just be reading it wrong