Go memory consumption management

前端 未结 2 1511
陌清茗
陌清茗 2021-02-01 17:41

I am new to Go and trying to figure out how it manages memory consumption.

I have trouble with memory in one of my test projects. I don\'t understand why Go uses more an

相关标签:
2条回答
  • 2021-02-01 18:02

    To eventually (force) collect unused memory you must call runtime.GC().

    variable = nil may make things unreachable and thus eligible for collection, but it per se doesn't free anything.

    0 讨论(0)
  • 2021-02-01 18:23

    Currently, go uses a mark-and-sweep garbage collector, which in general does not define when the object is thrown away.

    However, if you look closely, there is a go routine called sysmon which essentially runs as long as your program does and calls the GC periodically:

    // forcegcperiod is the maximum time in nanoseconds between garbage
    // collections. If we go this long without a garbage collection, one
    // is forced to run.
    //
    // This is a variable for testing purposes. It normally doesn't change.
    var forcegcperiod int64 = 2 * 60 * 1e9
    
    (...)
    
    // If a heap span goes unused for 5 minutes after a garbage collection,
    // we hand it back to the operating system.
    scavengelimit := int64(5 * 60 * 1e9)
    

    forcegcperiod determines the period after which the GC is called by force. scavengelimit determines when spans are returned to the operating system. Spans are a number of memory pages which can hold several objects. They're kept for scavengelimit time and are freed if no object is on them and scavengelimit is exceeded.

    Further down in the code you can see that there is a trace option. You can use this to see, whenever the scavenger thinks he needs to clean up:

    $ GOGCTRACE=1 go run gc.go
    gc1(1): 0+0+0 ms 0 -> 0 MB 423 -> 350 (424-74) objects 0 handoff
    gc2(1): 0+0+0 ms 1 -> 0 MB 2664 -> 1437 (2880-1443) objects 0 handoff
    gc3(1): 0+0+0 ms 1 -> 0 MB 4117 -> 2213 (5712-3499) objects 0 handoff
    gc4(1): 0+0+0 ms 2 -> 1 MB 3128 -> 2257 (6761-4504) objects 0 handoff
    gc5(1): 0+0+0 ms 2 -> 0 MB 8892 -> 2531 (13734-11203) objects 0 handoff
    gc6(1): 0+0+0 ms 1 -> 1 MB 8715 -> 2689 (20173-17484) objects 0 handoff
    gc7(1): 0+0+0 ms 2 -> 1 MB 5231 -> 2406 (22878-20472) objects 0 handoff
    gc1(1): 0+0+0 ms 0 -> 0 MB 172 -> 137 (173-36) objects 0 handoff
    getting memory
    gc2(1): 0+0+0 ms 381 -> 381 MB 203 -> 202 (248-46) objects 0 handoff
    returning memory
    getting memory
    returning memory
    

    As you can see, no gc invoke is done between getting and returning. However, if you change the delay from 5 seconds to 3 minutes (more than the 2 minutes from forcegcperiod), the objects are removed by the gc:

    returning memory
    scvg0: inuse: 1, idle: 1, sys: 3, released: 0, consumed: 3 (MB)
    scvg0: inuse: 381, idle: 0, sys: 382, released: 0, consumed: 382 (MB)
    scvg1: inuse: 1, idle: 1, sys: 3, released: 0, consumed: 3 (MB)
    scvg1: inuse: 381, idle: 0, sys: 382, released: 0, consumed: 382 (MB)
    gc9(1): 1+0+0 ms 1 -> 1 MB 4485 -> 2562 (26531-23969) objects 0 handoff
    gc10(1): 1+0+0 ms 1 -> 1 MB 2563 -> 2561 (26532-23971) objects 0 handoff
    scvg2: GC forced // forcegc (2 minutes) exceeded
    scvg2: inuse: 1, idle: 1, sys: 3, released: 0, consumed: 3 (MB)
    gc3(1): 0+0+0 ms 381 -> 381 MB 206 -> 206 (252-46) objects 0 handoff
    scvg2: GC forced
    scvg2: inuse: 381, idle: 0, sys: 382, released: 0, consumed: 382 (MB)
    getting memory
    

    The memory is still not freed, but the GC marked the memory region as unused. Freeing will begin when the used span is unused and older than limit. From scavenger code:

    if(s->unusedsince != 0 && (now - s->unusedsince) > limit) {
        // ...
        runtime·SysUnused((void*)(s->start << PageShift), s->npages << PageShift);
    }
    

    This behavior may of course change over time, but I hope you now get a bit of a feel when objects are thrown away by force and when not.

    As pointed out by zupa, releasing objects may not return the memory to the operating system, so on certain systems you may not see a change in memory usage. This seems to be the case for Plan 9 and Windows according to this thread on golang-nuts.

    0 讨论(0)
提交回复
热议问题