Dustin Oprea
Dustin Oprea

Reputation: 10276

time.NewTimer vs time.Tick

The documentation indicates the time.Tick leaks ('be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks"'). I'm assuming this refers to the channel. However, there appears to be no way to cleanup a Timer, either: You can make sure it doesn't fire but you can't close the channel (receive only).

The code for it doesn't contain a close call. runtime.deltimer (which is the underlying mechanism for Stop) doesn't touch the channel either.

No matter what, does Timer always leak?

Upvotes: 2

Views: 652

Answers (1)

Dustin Oprea
Dustin Oprea

Reputation: 10276

The only cleanup that matters is performed by calling Stop (as mentioned by @JimB).

Upvotes: 1

Related Questions