3

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.

frank.lin
  • 1,614
  • 17
  • 26
  • 1
    Possible duplicate of [Go 1.3 Garbage collector not releasing server memory back to system](http://stackoverflow.com/q/24376817/978917)? – ruakh Dec 16 '14 at 06:16
  • something like that, but I wait more than ten hours, the memory remains more than 400MB, I'm confused and want to know what occupied so much memory. – frank.lin Dec 16 '14 at 06:57
  • @buzz: what is confusing about that answer? Stack memory currently can't be freed. Are you using go1.4? – JimB Dec 16 '14 at 15:02
  • yes, go1.4. But I call `debug.FreeOSMemory()` and it doesn't decrease after long time – frank.lin Dec 17 '14 at 04:25
  • 1
    @frank.lin do you find any clue at last about this problem? – xjdrew Aug 12 '15 at 11:26
  • @xjdrew Not solve the problem completely. It seemed that the program itself didn't use that much memory and not give them to os either. So that‘s the problem of the strategy of allocing memory of go and in fact go's program would use more memory , according to other gophers. And what I do is reducing the use of memory as possible as I can.That's it. – frank.lin Oct 26 '15 at 01:59

1 Answers1

4

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.

tidwall
  • 6,881
  • 2
  • 36
  • 47