Golang协程泄漏定位和排查

1 场景:无缓冲channel写阻塞

package main

import (
	"log"
	"net/http"
	_ "net/http/pprof"
	"time"

	"github.com/gin-gonic/gin"
)

func main() {

	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()

	// 你的程序逻辑...

	g := gin.Default()
	g.GET("/hello", func(ctx *gin.Context) {
		ch := make(chan bool)
		go func() {
			time.Sleep(2 * time.Second)
			ch <- true
		}()
		select {
		case <-ch:
			break
		case <-time.After(time.Second):
			break
		}
		ctx.JSON(200, map[string]interface{}{
			"success": true,
		})
	})
	g.Run("127.0.0.1:8080")

}

2 排查和定位思路

2.1 Golang pprof

在这里插入图片描述

在这里插入图片描述
stack和数量check匹配
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/3942d93e1efa4827a5a1794d0a5a1923.png

2.2 协程数监控

在Golang中,可以使用runtime包的函数来监控和获取Goroutines的相关信息,如:

runtime.NumGoroutine():获取当前系统中的Goroutines数量
runtime.Gosched():主动让出CPU,让其他Goroutine运行
runtime.GOMAXPROCS():设置同时运行的最大CPU数量

2.3 操作系统内存泄漏

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
ePBF


# -a 表示显示每个内存分配请求的大小以及地址
# -p 指定案例应用的PID号
$ docker cp app:/app /app
$ /usr/share/bcc/tools/memleak -p $(pidof app) -a
Attaching to pid 12512, Ctrl+C to quit.
[03:00:41] Top 10 stacks with outstanding allocations:
    addr = 7f8f70863220 size = 8192
    addr = 7f8f70861210 size = 8192
    addr = 7f8f7085b1e0 size = 8192
    addr = 7f8f7085f200 size = 8192
    addr = 7f8f7085d1f0 size = 8192
    40960 bytes in 5 allocations from stack
        fibonacci+0x1f [app]
        child+0x4f [app]
        start_thread+0xdb [libpthread-2.27.so] 

参考

1 Go 项目中的 Goroutine 泄露及其防范措施
2 案例篇:内存泄漏了,我该如何定位和处理?
3 golang并发编程实践之goroutines的监控与调试技巧
4 go pprof 性能分析
5 Golang 内存泄漏详解:原因、检测与修复

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值