time.NewTimer vs time.Tick

huangapple go评论79阅读模式
英文:

time.NewTimer vs time.Tick

问题

文档指出time.Tick会泄漏(“请注意,如果没有关闭的方法,底层的 Ticker 将无法被垃圾回收器回收,它会“泄漏”)。我猜这是指通道。然而,似乎没有办法清理Timer:你可以确保它不触发,但你无法关闭通道(只能接收)。

代码中没有close调用。runtime.deltimerStop的底层机制)也不会触及通道。

无论如何,Timer是否总是会泄漏?

英文:

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?

答案1

得分: 1

唯一重要的清理是通过调用Stop方法来完成的(正如@JimB所提到的)。

英文:

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

huangapple
  • 本文由 发表于 2017年2月24日 00:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/42420666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定