Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

貌似有内存泄漏的问题 #35

Open
biaosheng opened this issue Jun 10, 2023 · 4 comments
Open

貌似有内存泄漏的问题 #35

biaosheng opened this issue Jun 10, 2023 · 4 comments

Comments

@biaosheng
Copy link

测试代码
使用net/http 内存很稳定
使用requests内存一直在增加
func main() {

for i := 0; i < 500; i++ {
	go test() //启动500个协程
}
time.Sleep(time.Hour) //简单写个多协程测试用 防止程序退出

}
func test() {
for {
request() //让request里面的 defer resp.Body.Close()执行
time.Sleep(time.Second * 2) //延迟两秒
}
}

func request() {
session := requests.NewSession()
get, err := session.Get("https://www.baidu.com", nil)
defer get.Body.Close()
if err != nil {
log.Println(err)
} else {
log.Println(get.StatusCode)
}
}

func request2() {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://www.baidu.com/", nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.7")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Cache-Control", "max-age=0")
req.Header.Set("Connection", "keep-alive")
//req.Header.Set("Cookie", newlogin=1; BAIDUID=E28143288BF2CE78F47DEE6D838597AD:FG=1; BIDUPSID=E28143288BF2CE78F47DEE6D838597AD; PSTM=1686290578; BD_UPN=12314753; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; ZFY=vtKQLMb10aj:AelQK2atb6hWafLrIjhTewMncrONOWB0:C; BAIDUID_BFESS=E28143288BF2CE78F47DEE6D838597AD:FG=1; BAIDU_WISE_UID=wapp_1686324034132_312; __bid_n=188a0beb7e9f01bd464207; FPTOKEN=S+748kxNSNES6GscYz26mVNxDWWA1/2I3Z7FA0E0NHuv+jrRn2ENP6+EFP0bjzzJD5FsllQOrq2X6aenUX7e0LQ+mZLbLSjH5hdpt3BnAdBry+KIG0sqKDidlivEJPVqZcvf/gj4HCjf6xwjLyKdEgdGROwDDgpPvHFU1CEVUltYiDsgRPZ7QJHydbPnkQFMp627a8VM+kZOD4/XZRHa3D6Xsu1MMiAsJip2qfU4Xcb/AC9Eyg+AUVjnVzKOJfaYJQXZFLBvbde0X9sYmGzLttNBuGPZNkn9f/XjTqiZwJAQ6ZoyPDUpJZhYlUEI6UmNqYjNO/bKiYgFykO7+buolTN/9h5Kde4ijQrvHbFQim5pVoMZFfPBGPDcUk8DtiOuac+9MEqdnnEimFpoZj+z9w==|g8znJHPGgx8B5pMSiHKdShyqBH6B81O0XbrNOjrIcgE=|10|e6f7011f1825eaffa3e3b4768aa3cd4c; arialoadData=false; RT="z=1&dm=baidu.com&si=4fc19501-3f80-464a-a4cc-5d7877667eb6&ss=liopsf0s&sl=e&tt=6g7&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&nu=2yhyy2du&cl=8tn&ld=31k7&ul=3lv1&hd=3lvz"; BA_HECTOR=80850h2181a08ga18485841j1i87fv51m; baikeVisitId=66992687-24dc-4433-a313-87b7e6b8ec0d; Hm_lvt_aec699bb6442ba076c8981c6dc490771=1686298007,1686356031; BD_HOME=1; H_PS_PSSID=38515_36547_38686_38857_38797_38768_38844_38831_38582_38793_38809_38837_38637_38502_26350)
req.Header.Set("Sec-Fetch-Dest", "document")
req.Header.Set("Sec-Fetch-Mode", "navigate")
req.Header.Set("Sec-Fetch-Site", "none")
req.Header.Set("Sec-Fetch-User", "?1")
req.Header.Set("Upgrade-Insecure-Requests", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
req.Header.Set("sec-ch-ua", "Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114")
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", "Windows")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close() // 新建request函数是为了使这个函数运行结束,能执行resp.Body.Close()
_, err = io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
log.Println(resp.StatusCode)
//fmt.Printf("%s\n", bodyText)
}

@biaosheng
Copy link
Author

是我代码哪里写的不对吗?有什么解决方案吗?

@wangluozhe
Copy link
Owner

好像确实存在内存泄露的问题,我有空得好好排查一下,解决这个问题

@biaosheng
Copy link
Author

好像确实存在内存泄露的问题,我有空得好好排查一下,解决这个问题

有解决方案了吗?好像是utls和fhttp整合的问题 单独测试utls或者测试fhttp 都没什么问题

@aj3423
Copy link

aj3423 commented Sep 14, 2023

@wangluozhe 当http头里用 keep-alive 时候,的确会连接一直保持着: req.Header.Set("Connection", "keep-alive")
这时候看tcp连接数应该一直占着不会释放。怎么样可以主动断开这个keep-alive的连接?因为写Close的话会被服务器检测到。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants