gpt4 book ai didi

debugging - go http 服务器和 fasthttp 中的内存泄漏

转载 作者:数据小太阳 更新时间:2023-10-29 03:41:58 40 4
gpt4 key购买 nike

我的代码是一个简单的 fasthttp 服务器,就像它的 github 示例一样但那有一个未知的内存泄漏。然后我试图找到它并清除我的代码,但它又出现了这个问题。

然后我只运行了官方示例,甚至出现了内存泄漏(这意味着我观察了 Windows 进程管理器上的内存使用情况,它使用的内存在负载中增长,并且即使在我的 Windows 崩溃之前一段时间后也不会释放) .

然后我通过一个非常简单的 hello world 服务器使用了 std net/http,我又遇到了那个问题。我的内存使用量随着每个请求而增长,而 Go 不会释放它。

我的版本是go 1.11.2 windows/amd64

这是我的代码有这个问题:

package main

import (
"net/http"
"strings"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
message := r.URL.Path
message = strings.TrimPrefix(message, "/")
message = "Hello " + message
w.Write([]byte(message))
r.Body.Close()
}
func main() {
http.HandleFunc("/", sayHello)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}

最佳答案

根据 Go http.Request documentation

// The Server will close the request body. The ServeHTTP
// Handler does not need to.

因此您应该删除 r.Body.Close() 调用,因为它不需要。

关于debugging - go http 服务器和 fasthttp 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53473638/

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