H5W3
当前位置:H5W3 > go > 正文

【go】golang time.NewTicker 内存泄漏?

  1. 程序运行内存不停增加 , 使用 pprof 查看,怀疑是time.NewTicker问题

部分代码

func (spider *Spider) downloaderTotalPlatform()  {
rconn := redis.GetConn()
defer rconn.Close()
queue := db.Queue{}
for {
v, err := rconn.Do("LPOP", db.RedisListList)
if err != nil {
log.Println(err.Error())
continue
}
if v == nil {
//暂停 5 秒
<-time.Tick(time.Second * 5)
continue
}
body, err := downloaders(v, &queue)
if err != nil {
log.Println(err.Error())
continue
}
if body == nil {
continue
}
spider.ChanParsers <- &Parser{
Body:body,
Queue:queue,
}
}
}

pprof图:

【go】golang  time.NewTicker 内存泄漏?

【go】golang  time.NewTicker 内存泄漏?

看看Tick的注释就知道了:

// Tick is a convenience wrapper for NewTicker providing access to the ticking
// channel only. While Tick is useful for clients that have no need to shut down
// the Ticker, be aware that without a way to shut it down the underlying
// Ticker cannot be recovered by the garbage collector; it "leaks".

回答

本文地址:H5W3 » 【go】golang time.NewTicker 内存泄漏?

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址