When do timer objects get cleaned up?

Quick question - if I create a timer and it can be ‘.rescheduled’, then when does it get cleaned up?

The ‘.isrunning’ method implies the timer object is hanging around after the timer has triggered. So in the code below, is my code consuming memory by creating lots of timer objects that never get cleaned up?

var Timer MyTimerObj = null

rule “Delay Timer”
when
… some trigger …
then
if (MyTimerObj !== null && MyTimerObj.isRunning)
MyTimerObj.cancel()

MyTimerObj = createTimer(now.plusSeconds(delay_period)) [
// do some stuff when the timer goes off
]
end

Each Timer is getting assigned to the MyTimerObj variable, so there is never more than one.

To be fair, the MyTimerObj variable is only a “handle”, a pointer to the timer.

It’s pretty easy to make a demo where creating a second timer assigned to the same handle does not destroy or overwrite the first timer. You just can’t get hold of it anymore, to cancel/reschedule/etc.

I don’t know how the garbage collection works, but can see there will be timers that no longer have external references. i.e. there is neither any actual schedule (it’s been run or cancelled) nor is there a extant handle (it’s been null or overwritten).
If the garbage collector can work that out, it can sweep up that timer code.

Hi Rossko57 - I did there same as you and created a few tests to see what I could do with timer pointers. It looks like they continue to exist for a while at least. You can cancel the timer and then reschedule, but I suspect I’m rescheduling a stale timer object before it’s cleaned up :crazy_face:

I imagine the objects are small, so it would take a long time to chew up memory.

My theory is that if you keep a timer handle it will not (should not!) get garbage collected. If you are rescheduling, you kept a handle.