dqajyxqem115006813 2016-01-26 03:22
浏览 20
已采纳

有什么工具可以检测到何时忘记关闭goroutine?

When I do this

done := make(chan bool)
for i := 0; i < 10; i++ {
    go func() {
        done <- true
    }()
}
<-done

instead of this

done := make(chan bool)
for i := 0; i < 10; i++ {
    go func() {
        done <- true
    }()
}
for i := 0; i < 10; i++ {
    <-done
}

Am I leaking goroutines if I do not close them and Is there a tool to detect when I forget to close goroutines?

  • 写回答

1条回答 默认 最新

  • douyin7829 2016-01-26 03:45
    关注

    Yes, you are leaking 9 goroutines in you first example.

    I don't believe there's any tool to tell you this.

    would be an interesting thing to make, if there's a way to query for all existing non-system (ie: gc) goroutines.

    Probably can do something with: runtime.Stack, but it would be super-specific to a given codebase as you likely have some "good" goroutines and some "rogue" ones.

    Update: Feb 4, 2016

    I got curious on this, so I made a really simple (and terribly named) library to do a diff of goroutines over time. A simplistic leak detector. https://github.com/dbudworth/greak

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制