Why is memory not decreasing in golang?

When the server started, it took about 83MB of memory, which I checked for top

. And when some connections were accepted and done something, it took about 500MB of memory. Then I closed all connections and the server to clear all structures, and once the memory went down a bit, about 30MB. The memory did not return to the level at which the server was started.

when the server started i looked at the heap information

# 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 a while and gc, I looked 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 was so small. Because I already cleared the variables stored on the server. Anyone give some advice on how I find the problem?

PS. He doesn't care about the goroutine, I checked it and the number of goroutines didn't increase. And I am already using pprof and opening gcdebug.

+3


source to share


1 answer


Because it's not entirely up to Go to shrink its own memory. The Go garbage collector sometimes calls the OS to reclaim unused memory. The OS may decide not to release memory because the system has a lot of free space, another for another reason.

If you are really concerned about an application using too much memory or memory leaks, look at the value HeapAlloc

over time. Make sure this value stays within the expected range.



Also, don't expect debug.FreeOSMemory()

or runtime.GC()

will do what you expect.

+4


source







All Articles