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 超时操作,肯定执行不到了