首页  >  文章  >  后端开发  >  为什么 http post 请求在 go 中给我带来很高的内存使用率?

为什么 http post 请求在 go 中给我带来很高的内存使用率?

PHPz
PHPz转载
2024-02-14 15:24:08890浏览

为什么 http post 请求在 go 中给我带来很高的内存使用率?

php小编新一在使用Go语言进行http post请求时,发现高内存使用率的问题。这个问题引发了他的思考,为什么会出现这样的情况?经过调研和分析,他找到了一些可能的原因,并提出了一些解决方案。在本文中,我们将深入探讨这个问题并给出解答。

问题内容

我有一个 go 应用程序在 k8s 容器内运行。它作为一个 rest api 工作,接收请求并将它们的请求写入 elasticsearch。

我的代码是:

var r = gin.default()
r.post("/logs", func(c *gin.context) {
        fmt.println("receive log event")
        printmemusage()
        jsondata, err := ioutil.readall(c.request.body)
        d := strings.newreader(jsondata)
        http.post(fmt.sprintf("%s/_bulk", geteshost()), "application/json", d)
        ...
    })
}


在上面的代码中,它监听路径 /logs 并调用 http 将数据保存到 elasticsearch 中。当我使用下面的函数打印内存使用情况时,我可以看到 alloc 不断增加,直到内存耗尽。如果我删除 http.post 调用,内存使用量始终为 1 到 3mb。内存使用量不断增加的原因可能是什么?

func bToMb(b uint64) uint64 {
    return b / 1024 / 1024
}
func PrintMemUsage() {
    var m runtime.MemStats
    runtime.ReadMemStats(&m)
    // For info on each, see: https://golang.org/pkg/runtime/#MemStats
    fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
    fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
    fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
    fmt.Printf("\tNumGC = %v\n", m.NumGC)
}

解决方法

http 文档多次提到:

客户端完成后必须关闭响应正文:

这是文档中的示例:

resp, err := http.Get("http://example.com/")
if err != nil {
    // handle error
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
// ...

如果你不这样做,就会发生泄漏,因为主体将永远保留在内存中。

以上是为什么 http post 请求在 go 中给我带来很高的内存使用率?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除