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图:

clipboard.png

clipboard.png

阅读 6.3k
1 个回答

看看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".
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题