Will this cause a memory leak?

I made my own Debris system since Roblox has a limit on how many items can be added, and one of the functions I added was “:AddMultiple()”, it requires a table argument (for the instances to be added) and its lifetime. The function will just call “:Add():” on each item that was listed.

However, when I use LuaHeap the instances that are being added to the Debris are not being recycled properly and I was wondering if it that’s because I am using tables as an argument.

This is the code:

velocity - BodyVelocity
rotation - BodyAngularVelocity
VELOCITY_DEBRIS_TIME - 0.4

	Debris:AddMultiple({velocity, rotation}, VELOCITY_DEBRIS_TIME)

And the “:AddMultiple()” function

function debris:AddMultiple(instances: {Instance}, lifetime: number)
	lifetime = tonumber(lifetime)

	if not instances or not lifetime then
		return
	end
		
	for k,v in pairs(instances) do
		task.spawn(debris.Add, debris, v, lifetime)
	end
	
	task.delay(lifetime, function()
		table.clear(instances)
	end)
end
1 Like

It’s a bit hard to debug when I don’t know what debris.Add looks like. So my answer is operating under the assumption that it just adds all items in instances to some kind of table and nothing else. If you can share debris.Add, I’ll be able to provide a more informed solution.

In the meantime, I’d check to see if you’re actually destroying velocity and rotation. Keep in mind that setting instances’s items to nil is not the same as deleting, because the only thing you’ve done is remove their reference. If you have some background in C++, you can think of it as changing a value by setting the passed variable rather than using a pointer. Try adding a destroy method wherever is relevant.

1 Like

I highly doubt your :AddMultiple() function is at all the cause of the memory leak, you don’t even have to clear the table after using it, the garbage collector will do it for you. So if there is actually a memory leak with your function, it would have to be the Debris:AddItem() function, which has been heavily battle-tested and should not be leaking any memory at all.

1 Like

Just do this..

for _, part in ipairs(parts) do
    task.delay(3, part.Destroy, part)
end

This approach allows unlimited scheduling without interference from Debris’s 1,000‑item queue.

1 Like

can you share the debris.Add function?

only thing i would change is instead of creating a new task for each item, put the for loop inside of the task.spawn / task.delay function. you also dont have to clear the table, luaus garbage collector will do that for you.

1 Like

thanks for the feedback everyone - here’s the debris:add function

function debris:Add(instance: Instance, lifetime: number)
	lifetime = tonumber(lifetime)

	if not instance or not lifetime then
		return
	end
		
	if (lifetime ~= lifetime) or (lifetime >= math.huge) then
		return
	end
	
	task.delay(lifetime, function()
		if not table.find(debris._items, instance) then
			return
		end
		
		instance:Destroy()
	end)
	
	table.insert(debris._items, instance)
end

i see, but i forgot to add that i want to have a “removeitem” function so that way if i dont want something to be deleted i can just call that to remove it from being deleted

It’s just a way to slow it down to manageable if you’re using it a lot…

I’d recommend you use Collector for this type of thing

1 Like

you don’t need to put a table as an argument for the add multiple function. you can make it a variadic function

1 Like

found out the problem

in my Debris:Add() function after destroying the instance, it isn’t removed from debris._items and which is why i would still see stuff unrecycled on my luaheap snapshots

ty everyone for ur input. i’ll try to make it the Debris:AddMultiple() variadic function instead

1 Like

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