Go 1.3 Garbage collector not releasing server memory back to system

后端 未结 3 1982
囚心锁ツ
囚心锁ツ 2020-11-30 04:23

We wrote the simplest possible TCP server (with minor logging) to examine the memory footprint (see tcp-server.go below)

The server simply accepts connections and do

3条回答
  •  广开言路
    2020-11-30 04:41

    As Jsor said, you should wait at least 7 minutes to check how much memory is freed. Sometimes it needs two GC passes, so it would be 9 minutes.

    If that is not working, or it is too much time, you can add a periodic call to FreeOSMemory (no need to call runtime.GC() before, it is done by debug.FreeOSMemory() )

    Something like this: http://play.golang.org/p/mP7_sMpX4F

    package main
    
    import (
        "runtime/debug"
        "time"
    )
    
    func main() {
        go periodicFree(1 * time.Minute)
    
        // Your program goes here
    
    }
    
    func periodicFree(d time.Duration) {
        tick := time.Tick(d)
        for _ = range tick {
            debug.FreeOSMemory()
        }
    }
    

    Take into account that every call to FreeOSMemory will take some time (not much) and it can be partly run in parallel if GOMAXPROCS>1 since Go1.3.

提交回复
热议问题