golang time.After 内存泄漏

2022/4/30 7:13:05

本文主要是介绍golang time.After 内存泄漏,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

官方一段话 time.After 内存gc 不会回收
  • 其实不是 源码
/ After waits for the duration to elapse and then sends the current time
// on the returned channel.
// It is equivalent to NewTimer(d).C.
// The underlying Timer is not recovered by the garbage collector
// until the timer fires. If efficiency is a concern, use NewTimer
// instead and call Timer.Stop if the timer is no longer needed.
func After(d Duration) <-chan Time {
 return NewTimer(d).C
}
结论
  • until the timer fires,说明fire后是会被回收的,fire就是到时间了
一个问题就在 死循环或者 select 使用 time.After
 case <- time.After(time.Second)  :

// 只是子本次只有在本次select 操作中会有效, 再次select 又会重新开始计时(从当前时间1秒后),
 但是有default ,那case 超时操作,肯定执行不到了


这篇关于golang time.After 内存泄漏的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程