diff --git a/internal/gui/api.go b/internal/gui/api.go index 339435c36..dddf14e2e 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -2571,10 +2571,10 @@ func held(h http.Handler) http.Handler { // and collected under GOGC 25 while it runs: a poll's reads // allocated ~330 MB to keep ~105 MB, and at the default 100 the // heap grew to twice what it kept before a collection, ~320 MB - // in Activity Monitor during a poll. The memory limit the - // warm-up's read takes too isn't taken here: held as close as - // heldMemory, every poll collected over and over, 5x the CPU. - defer tightenGC(false)() + // in Activity Monitor during a poll. No memory limit: held as + // close as what Go held, every poll collected over and over, + // 5x the CPU. + defer tightenGC()() } if !(api && r.Method == http.MethodGet) { // anything that may have written drops what was held, once it diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 10a3d619f..bdce714c6 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -9,7 +9,6 @@ import ( "os" "runtime" "runtime/debug" - "runtime/metrics" "slices" "strconv" "strings" @@ -640,16 +639,14 @@ func warmUp(steps ...func()) { // still holds its rows, run under it: the warm-up as a whole did too for // ~11 s more CPU. // -// It also holds the memory Go asks of the system to what it held when fn -// began, as a soft limit (a lower one the user set is kept, and GOGC off -// has none). A build of the request index after an update reads every -// session again and allocates ~1.2 GB to keep ~40 MB more, and the heap -// overshot its goal into pages it then kept: 308–323 MB in Activity -// Monitor for 15.7k sessions, where a start that reads the index peaked at -// 247–255 MB. Under the limit, 241–251 MB and 211–228 MB, for 1–7 s more -// CPU in that build. +// It sets no memory limit. It held the memory Go asked of the system to +// what it held when fn began, and a cold start then reached the limit +// with the heap all live: from ~11 s to ~24 s it collected every ~160 ms, +// a core's worth, 3:39 of CPU in its first 60 s for 390k calls and 1166 +// collections. Without it, 2:03–2:06 and 618, and the footprint's peak +// the same, 835–863 MB against 855 (#2278). func underTightGC(fn func()) { - defer tightenGC(true)() + defer tightenGC()() fn() } @@ -659,15 +656,12 @@ func underTightGC(fn func()) { // when the first to begin ended before the second. var tightGC struct { sync.Mutex - n, limits int // reads under it, and those of them limited - was int // GOGC before the first - limit int64 // the memory limit before the first limited one - limited bool // whether that one set one + n int // reads under it + was int // GOGC before the first } -// tightenGC begins a read under a tight GC, with the memory limit too when -// limit is set; undo ends it. -func tightenGC(limit bool) (undo func()) { +// tightenGC begins a read under a tight GC; undo ends it. +func tightenGC() (undo func()) { tightGC.Lock() defer tightGC.Unlock() if tightGC.n == 0 { @@ -678,37 +672,15 @@ func tightenGC(limit bool) (undo func()) { tightGC.was = was } tightGC.n++ - if limit { - if tightGC.limits == 0 { - tightGC.limited = false - if held := heldMemory(); tightGC.was >= 0 && held < debug.SetMemoryLimit(-1) { - tightGC.limit, tightGC.limited = debug.SetMemoryLimit(held), true - } - } - tightGC.limits++ - } return sync.OnceFunc(func() { tightGC.Lock() defer tightGC.Unlock() - if limit { - if tightGC.limits--; tightGC.limits == 0 && tightGC.limited { - debug.SetMemoryLimit(tightGC.limit) - } - } if tightGC.n--; tightGC.n == 0 { debug.SetGCPercent(tightGC.was) } }) } -// heldMemory is what Go holds of the system's memory: all it mapped, less -// the pages it gave back. -func heldMemory() int64 { - s := []metrics.Sample{{Name: "/memory/classes/total:bytes"}, {Name: "/memory/classes/heap/released:bytes"}} - metrics.Read(s) - return int64(s[0].Value.Uint64() - s[1].Value.Uint64()) -} - const tightGCPercent = 25 // openTerminal runs a command in the chosen Mac terminal through a .command diff --git a/internal/gui/warm_gc_test.go b/internal/gui/warm_gc_test.go index f9b866e39..1de0d842d 100644 --- a/internal/gui/warm_gc_test.go +++ b/internal/gui/warm_gc_test.go @@ -34,35 +34,14 @@ func TestUnderTightGC(t *testing.T) { } } -// Under it the memory Go holds is limited to what it held when the read -// began, and the limit set before is put back; a lower limit the user set -// is kept, and GOGC off gets none. -func TestUnderTightGCLimitsMemory(t *testing.T) { - defer debug.SetGCPercent(debug.SetGCPercent(100)) +// Under it no memory limit is set: the warm-up's read held at what Go +// held when it began collected every ~160 ms for 13 s of a cold start. +func TestUnderTightGCSetsNoMemoryLimit(t *testing.T) { defer debug.SetMemoryLimit(debug.SetMemoryLimit(math.MaxInt64)) - for _, c := range []struct { - name string - gogc int - before int64 - low bool - }{ - {"default", 100, math.MaxInt64, true}, - {"user limit lower", 100, 1 << 20, false}, - {"GOGC off", -1, math.MaxInt64, false}, - } { - debug.SetGCPercent(c.gogc) - debug.SetMemoryLimit(c.before) - var during int64 - underTightGC(func() { during = debug.SetMemoryLimit(-1) }) - if c.low && during >= c.before { - t.Errorf("%s: read under limit %d, want what Go held", c.name, during) - } - if !c.low && during != c.before { - t.Errorf("%s: read under limit %d, want %d kept", c.name, during, c.before) - } - if after := debug.SetMemoryLimit(-1); after != c.before { - t.Errorf("%s: after the read the limit is %d, want %d", c.name, after, c.before) - } + var during int64 + underTightGC(func() { during = debug.SetMemoryLimit(-1) }) + if during != math.MaxInt64 { + t.Errorf("read under limit %d, want none", during) } } @@ -73,8 +52,8 @@ func TestTightGCOverlappingReads(t *testing.T) { defer debug.SetGCPercent(debug.SetGCPercent(100)) defer debug.SetMemoryLimit(debug.SetMemoryLimit(math.MaxInt64)) for _, firstEndsFirst := range []bool{true, false} { - first := tightenGC(true) - second := tightenGC(false) + first := tightenGC() + second := tightenGC() if p := gcPercent(); p != tightGCPercent { t.Fatalf("two reads under GOGC=%d, want %d", p, tightGCPercent) }