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.
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
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.
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.
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.
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
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