Go goroutine 泄漏
Go 服务内存和 goroutine 数持续增长,最终 OOM 或调度变慢;/debug/pprof/goroutine 显示大量相同调用栈的 goroutine 阻塞在 channel、锁或网络读写。
curl -s http://localhost:6060/debug/pprof/goroutine?debug=1 | head -50
curl -s http://localhost:6060/debug/pprof/goroutine?debug=2 > goroutines.txt
go tool pprof http://localhost:6060/debug/pprof/goroutine
查看 goroutine 数和内存:
curl -s http://localhost:6060/debug/pprof/ | grep goroutine
go tool pprof -top http://localhost:6060/debug/pprof/heap
errgroup 或 WaitGroup 管理生命周期。Timeout 和 Transport 超时。go tool pprof — goroutine/heap/CPU 分析go-torch / pprof — 火焰图合上页面,先回答,再展开对照。
/debug/pprof/goroutine?debug=2 导出调用栈,用 go tool pprof 分析;若大量 goroutine 阻塞在相同调用栈的 channel、锁或网络读写上,且数量持续增长,就是泄漏特征。errgroup/WaitGroup 管理生命周期并限制并发;修复后对比 goroutine 曲线,确认不再增长。
小程序版
微信扫码,随时查命令