From: Austin Clements Date: Sun, 17 May 2015 01:14:37 +0000 (-0400) Subject: runtime: minor clean up to heapminimum X-Git-Tag: go1.5beta1~530 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f4d51eb2f567d12c8b908f9fe3f9650e8a175eb7;p=gostls13.git runtime: minor clean up to heapminimum Currently setGCPercent sets heapminimum to heapminimum*GOGC/100. The real intent is to set heapminimum to a scaled multiple of a fixed default heap minimum, not to scale heapminimum based on its current value. This turns out to be okay because setGCPercent is only called once and heapminimum is initially set to this default heap minimum. However, the code as written is confusing, especially since setGCPercent is otherwise written so it could be called again to change GOGC. Fix this by introducing a defaultHeapMinimum constant and using this instead of the current value of heapminimum to compute the scaled heap minimum. As part of this, this commit improves the documentation on heapminimum. Change-Id: I4eb82c73dc2eb44a6e5a17c780a747a2e73d7493 Reviewed-on: https://go-review.googlesource.com/10181 Reviewed-by: Russ Cox --- diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index a16d7603a6..fb2b210020 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -127,13 +127,22 @@ const ( _RootCount = 5 ) -// heapminimum is the minimum number of bytes in the heap. -// This cleans up the corner case of where we have a very small live set but a lot -// of allocations and collecting every GOGC * live set is expensive. -// heapminimum is adjust by multiplying it by GOGC/100. In -// the special case of GOGC==0 this will set heapminimum to 0 resulting -// collecting at every allocation even when the heap size is small. -var heapminimum = uint64(4 << 20) +// heapminimum is the minimum heap size at which to trigger GC. +// For small heaps, this overrides the usual GOGC*live set rule. +// +// When there is a very small live set but a lot of allocation, simply +// collecting when the heap reaches GOGC*live results in many GC +// cycles and high total per-GC overhead. This minimum amortizes this +// per-GC overhead while keeping the heap reasonably small. +// +// During initialization this is set to 4MB*GOGC/100. In the case of +// GOGC==0, this will set heapminimum to 0, resulting in constant +// collection even when the heap size is small, which is useful for +// debugging. +var heapminimum uint64 = defaultHeapMinimum + +// defaultHeapMinimum is the value of heapminimum for GOGC==100. +const defaultHeapMinimum = 4 << 20 // Initialized from $GOGC. GOGC=off means no GC. var gcpercent int32 @@ -180,7 +189,7 @@ func setGCPercent(in int32) (out int32) { in = -1 } gcpercent = in - heapminimum = heapminimum * uint64(gcpercent) / 100 + heapminimum = defaultHeapMinimum * uint64(gcpercent) / 100 unlock(&mheap_.lock) return out }