英文:
time.NewTimer vs time.Tick
问题
文档指出time.Tick
会泄漏(“请注意,如果没有关闭的方法,底层的 Ticker 将无法被垃圾回收器回收,它会“泄漏”)。我猜这是指通道。然而,似乎没有办法清理Timer
:你可以确保它不触发,但你无法关闭通道(只能接收)。
代码中没有close
调用。runtime.deltimer(Stop
的底层机制)也不会触及通道。
无论如何,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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论