gpt4 book ai didi

许多 goroutine 的内存消耗

转载 作者:IT王子 更新时间:2023-10-29 01:48:09 26 4
gpt4 key购买 nike

我已经建立了一个用 Go 编写的 http 服务器,它每天有超过一千名访问者。我有一个累积的 Goroutine 问题(从未释放)。在一天的时间里,我似乎从 http 服务器获得了超过一千个新的 Goroutines。我使用 pprof 检查问题的来源,我得到了:

Link: memory consumption: SVG pprof

enter image description here

堆:

enter image description here

下面是我的 goroutines

500 @ 0x410255 0x5a9255 0x5a9e25 0x5aa615 0x5990cf 0x5ada95 0x59d23f 0x4367b1
# 0x5a9255 net._C2func_getaddrinfo+0x55 /usr/local/go/src/net/:26
# 0x5a9e25 net.cgoLookupIPCNAME+0x1c5 /usr/local/go/src/net/cgo_unix.go:96
# 0x5aa615 net.cgoLookupIP+0x65 /usr/local/go/src/net/cgo_unix.go:148
# 0x5990cf net.lookupIP+0x5f /usr/local/go/src/net/lookup_unix.go:64
# 0x5ada95 net.func·026+0x55 /usr/local/go/src/net/lookup.go:79
# 0x59d23f net.(*singleflight).doCall+0x2f /usr/local/go/src/net/singleflight.go:91

157871 @ 0x423985 0x4239f8 0x411464 0x410c93 0x5a9d68 0x5aa615 0x5990cf 0x5ada95 0x59d23f 0x4367b1
# 0x5a9d68 net.cgoLookupIPCNAME+0x108 /usr/local/go/src/net/cgo_unix.go:85
# 0x5aa615 net.cgoLookupIP+0x65 /usr/local/go/src/net/cgo_unix.go:148
# 0x5990cf net.lookupIP+0x5f /usr/local/go/src/net/lookup_unix.go:64
# 0x5ada95 net.func·026+0x55 /usr/local/go/src/net/lookup.go:79
# 0x59d23f net.(*singleflight).doCall+0x2f /usr/local/go/src/net/singleflight.go:91

在这里我们可以看到 singleflight.go 使用了大部分 goroutints,它是 Go 的原生库。

我的代码在这个函数中被阻塞了

func getXmlVast(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", errors.New("request error A(" + err.Error() + ")")
}
defer resp.Body.Close()
// read xml http response
xmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.New("request error B(" + err.Error() + ")")
}
return string(xmlData), nil
}

为什么 Go 从未释放 goroutines 以及 singleflight.go 做了什么。

最佳答案

我将我的 Go 版本从 1.4 更新到 1.5,它解决了这个问题。

我之前做了一些研究以找出问题出在哪里我注意到很多人都有同样的问题但没人知道为什么。我认为问题出在 http/net库,因为正如我在问题中所说,使用 goroutints 最多的函数是 singleflight 并且此函数由 http.Get(url)

调用

关于许多 goroutine 的内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31788888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com