为什么golang的记忆力下降? [英] Why does not the memory decrease in golang ?

查看:370
本文介绍了为什么golang的记忆力下降?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当服务器启动时,它所占用的内存大约为83MB,我通过 top 来检查。
当一些连接被接受并做了一些事情时,它所花费的内存大约为500MB。然后,我关闭了所有连接,服务器清除了所有结构,过了一段时间,内存减少了一些,大约30MB。内存在服务器启动时没有返回到级别。



服务器启动时,我查找堆信息

 #runtime .MemStats 
#Alloc = 7251528
#TotalAlloc = 52713992
#Sys = 15010040
#Lookups = 49
#Mallocs = 2072338
#Frees = 2038576
#HeapAlloc = 7251528
#HeapSys = 12025856
#HeapIdle = 2121728
#HeapInuse = 9904128
#HeapReleased = 0
#HeapObjects = 33762
#Stack = 425984/425984
#MSpan = 75504/81920
#MCache = 4800/16384
#BuckHashSys = 1457768
#NextGC = 11496656

当所有连接关闭时,在某段时间和gc之后,我再次抬头看看

 #runtime.MemStats 
#Alloc = 5845912
#TotalAlloc = 13053679584
#Sys = 73128248
#Lookups = 794
#Mallocs = 22728491
#Frees = 22699056
#HeapAlloc = 5845912
#HeapSys = 60112896
#HeapIdle = 52166656
#HeapInuse = 7946240
#HeapReleased = 0
#HeapObjec ts = 29435
#Stack = 3719168/3719168
#MSpan = 88608/180224
#MCache = 4800/16384
#BuckHashSys = 1597264
#NextGC = 9428528

我不知道为什么它减少得这么少。因为我已经清除了保存在服务器中的变量。有人会就我如何找到问题提供一些建议吗?



PS。它不关心goroutine,我检查了它,goroutine的数量没有增加。我已经使用pprof并打开gcdebug。

解决方案

因为它不完全取决于缩小它自己的内存。 Go垃圾收集器偶尔会向操作系统发出请求以释放未使用的内存。操作系统可能会决定不释放内存,因为系统有足够的空间和其他一些原因。



如果您真的担心您的应用程序占用太多内存或泄漏内存,然后注意随时间变化的 HeapAlloc 值。请确保此值保持在您预期的范围内。



另外,不要指望 debug.FreeOSMemory() runtime.GC()来做你的期望。

When the server started , the memory it took was about 83MB, which I checked by top. And When some connections were accepted and did something , the memory it took was about 500MB, Then, I closed all the connections and server cleard all the structs, after sometime, the memory decreased a little , about 30MB.The memory didn't return to the level when the server started.

when server started , I looked up heap info

# runtime.MemStats
# Alloc = 7251528
# TotalAlloc = 52713992
# Sys = 15010040
# Lookups = 49
# Mallocs = 2072338
# Frees = 2038576
# HeapAlloc = 7251528
# HeapSys = 12025856
# HeapIdle = 2121728
# HeapInuse = 9904128
# HeapReleased = 0
# HeapObjects = 33762
# Stack = 425984 / 425984
# MSpan = 75504 / 81920
# MCache = 4800 / 16384
# BuckHashSys = 1457768
# NextGC = 11496656

And when all connections closed, after sometime and gc, I looked up again

# runtime.MemStats
# Alloc = 5845912
# TotalAlloc = 13053679584
# Sys = 73128248
# Lookups = 794
# Mallocs = 22728491
# Frees = 22699056
# HeapAlloc = 5845912
# HeapSys = 60112896
# HeapIdle = 52166656
# HeapInuse = 7946240
# HeapReleased = 0
# HeapObjects = 29435
# Stack = 3719168 / 3719168
# MSpan = 88608 / 180224
# MCache = 4800 / 16384
# BuckHashSys = 1597264
# NextGC = 9428528

And I didn't know why it decrease so little. Because I have already clear the variables saved in server. Would anyone give some advice on how I find the problom?

PS. It is not concerned about goroutine, I checked it, and the numbers of goroutine didn't increase. And I already use pprof and open gcdebug.

解决方案

Because it's not entirely up to Go to shrink it's own memory. The Go garbage collector occasionally makes requests to the OS to release unused memory. The OS may decide to not release the memory because the system has plenty to spare, other some other reason.

If you are really concerned about you app taking too much memory or leaking memory then pay attention to the HeapAlloc value over time. Make sure that this value stays in your expected range.

Also, don't expect debug.FreeOSMemory() or runtime.GC() to do what you expect.

这篇关于为什么golang的记忆力下降?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆