Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gui: the warm-up's whole-history reads hold Go to the memory it held when they began (#3493)

yetone99de8e1ad1d7
diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go
index e9a6863ff..ebaf5203a 100644
--- a/internal/gui/sessions.go
+++ b/internal/gui/sessions.go
@@ -9,6 +9,7 @@ import (
"os"
"runtime"
"runtime/debug"
+ "runtime/metrics"
"slices"
"strconv"
"strings"
@@ -576,15 +577,36 @@ func warmUp(steps ...func()) {
// and Codex's effort, which reads the whole ledger again while the page
// 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.
func underTightGC(fn func()) {
- if was := debug.SetGCPercent(tightGCPercent); was < tightGCPercent {
+ was := debug.SetGCPercent(tightGCPercent)
+ if was < tightGCPercent {
debug.SetGCPercent(was)
} else {
defer debug.SetGCPercent(was)
}
+ if limit := heldMemory(); was >= 0 && limit < debug.SetMemoryLimit(-1) {
+ defer debug.SetMemoryLimit(debug.SetMemoryLimit(limit))
+ }
fn()
}
+// 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 4f6f65592..76e39b5c1 100644
--- a/internal/gui/warm_gc_test.go
+++ b/internal/gui/warm_gc_test.go
@@ -1,6 +1,7 @@
package gui
import (
+ "math"
"runtime/debug"
"testing"
)
@@ -33,6 +34,38 @@ 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))
+ 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)
+ }
+ }
+}
+
// The warm-up's usage read is the one under it, and the warm-up's other
// reads are not.
func TestWarmUpTightensOnlyTheUsageRead(t *testing.T) {