Will this BindableEvent produce a memory leak?

if I create a BindableEvent and if this BindableEvent has no connections, no parent and no references will it be garbage collected?


Example

-- create a BindableEvent 
local bindableEvent = Instance.new("BindableEvent")

-- connect a function to the event
local connection = bindableEvent.Event:Connect(function() end)

-- disconnect the function
connection:Disconnect()

-- remove reference
bindableEvent = nil

so after the code above has run will it produce a memory leak?

2 Likes

It will not produce a memory leak from testing using the debug library.

Using looping code it will peak then reset to a low value. It seems the instances in nil get cleaned up eventually as there is no reference to it within the code. This will change if you put them in a table and such.

Also I highly appreciate your work and tutorials @5uphi keep it up :+1:.

debug.setmemorycategory("BindableEvents Tests")
print("Running")
game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
	
	for i = 1, 100 do
		-- create a BindableEvent 
		local bindableEvent = Instance.new("BindableEvent")

		-- connect a function to the event
		local connection = bindableEvent.Event:Connect(function() end)

		-- disconnect the function
		connection:Disconnect()

		-- remove reference
		--bindableEvent = nil
	end
end)
8 Likes

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