diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index 5b974d466b81f..730b64cd198da 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -765,6 +765,14 @@ func pollFractionalWorkerExit() bool { // // mheap_.lock must be held or the world must be stopped. func gcSetTriggerRatio(triggerRatio float64) { + // Compute the next GC goal, which is when the allocated heap + // has grown by GOGC/100 over the heap marked by the last + // cycle. + goal := ^uint64(0) + if gcpercent >= 0 { + goal = memstats.heap_marked + memstats.heap_marked*uint64(gcpercent)/100 + } + // Set the trigger ratio, capped to reasonable bounds. if triggerRatio < 0 { // This can happen if the mutator is allocating very @@ -807,22 +815,16 @@ func gcSetTriggerRatio(triggerRatio float64) { print("runtime: next_gc=", memstats.next_gc, " heap_marked=", memstats.heap_marked, " heap_live=", memstats.heap_live, " initialHeapLive=", work.initialHeapLive, "triggerRatio=", triggerRatio, " minTrigger=", minTrigger, "\n") throw("gc_trigger underflow") } - } - memstats.gc_trigger = trigger - - // Compute the next GC goal, which is when the allocated heap - // has grown by GOGC/100 over the heap marked by the last - // cycle. - goal := ^uint64(0) - if gcpercent >= 0 { - goal = memstats.heap_marked + memstats.heap_marked*uint64(gcpercent)/100 - if goal < trigger { + if trigger > goal { // The trigger ratio is always less than GOGC/100, but // other bounds on the trigger may have raised it. // Push up the goal, too. goal = trigger } } + + // Commit to the trigger and goal. + memstats.gc_trigger = trigger memstats.next_gc = goal if trace.enabled { traceNextGC()