为什么在golang中内存不会减少?

huangapple go评论84阅读模式
英文:

Why does not the memory decrease in golang ?

问题

当服务器启动时,我通过top命令检查,它所占用的内存约为83MB。当有一些连接被接受并执行了一些操作后,它所占用的内存约为500MB。然后,我关闭了所有的连接,服务器清除了所有的结构体,过了一段时间后,内存稍微减少了一点,约为30MB。但内存并没有恢复到服务器启动时的水平。

当服务器启动时,我查看了堆信息:

  • 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

当所有连接关闭后,经过一段时间和垃圾回收,我再次查看了堆信息:

  • 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

我不知道为什么内存减少得如此之少。因为我已经清除了服务器中保存的变量。是否有人能给出一些建议,告诉我如何找到问题所在?

PS. 这与goroutine无关,我已经检查过了,goroutine的数量没有增加。我已经使用了pprof并打开了gcdebug。

英文:

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.

答案1

得分: 4

因为Go语言无法完全控制自身的内存收缩,所以它不完全依赖于Go语言来收缩内存。Go语言的垃圾回收器偶尔会向操作系统发出请求,释放未使用的内存。操作系统可能决定不释放内存,因为系统有足够的内存可用,或者出于其他原因。

如果你真的担心你的应用程序占用太多内存或者存在内存泄漏问题,那么请关注HeapAlloc值随时间的变化。确保这个值保持在你期望的范围内。

此外,不要期望debug.FreeOSMemory()或者runtime.GC()能够按照你的期望工作。

英文:

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.

huangapple
  • 本文由 发表于 2014年12月16日 12:26:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/27497512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定