Best/most performant way to remove an object in memory after X seconds?

Hey everyone, sorry if this questions seems a bit “dumb”, but I honestly have no clue what its answer is.

So, in one of my server modules, I create an object rather frequently. (Not a physical object, rather a “class” /oop object in memory)

Once this object is created though, I want to dispose it after x seconds(in my case x is set to 120 seconds or 2 minutes)

However, I’m not sure if it’s better to tie this module to RunService.Stepped and dispose of the object after time elapsed since it was created: i.e

-- On stepped

for i,v in pairs(objects) do
   if tick()-v.TimeCreated >= TIME_TO_REMOVE then


Using delay or a coroutine to accomplish this

-- After object is created in
delay(TIME_TO_REMOVE,function() self:Dispose(); end)

I just worry about creating so many threads so often, I’m not sure if it has any big performance impact and I assume that’s what delay(fn) is doing but I don’t know the internals :man_shrugging:

I’d really appreciate it if someone could let me know if there’s any real difference, and what is best to use for my case. Thanks!

Option #2 would definitely be more performance friendly.

1: check every frame if time has been up
2: wait until time has been up & destroy.

option #1 is redundant because TIME_TO_REMOVE is static.


Debris:AddItem is for physical objects/instances, like BaseParts, etc.

Unless you mean creating a temporary physical instance, add it to Debris, and dispose my object when
instance:GetPropertyChangedSignal(“Parent”) says it’s been deleted

But that just seems silly

1 Like

delay does not necessarily resume at the time you expect (similar to spawn, wait, or when using coroutines). If there is a lot of other thread resume work going on, the scheduler might decide to postpone it to a later frame. In that sense, there is definitely a difference between 1 and 2, namely that 1 will dispose as soon as possible, and 2 will be slightly more performant but will not always dispose at the soonest possible moment.


A possible further optimization would be to use an object pool, thus saving you time spent destroying and creating the objects at the expense of memory.