# Research: memory (/docs/experiments/runtime/research)

Date: 2026-11-17. Scope: the runtime (scheduler, allocator, collector, page release). Measurements: release builds of the bench programs on the linux/amd64 benchmark machine, best of 3 runs. Peak RSS comes from wait4 (ru_maxrss), and CPU from perf stat task-clock. The machine was loaded (load 3 to 27), so CPU numbers are indicative. RSS is not sensitive to load.

## 1. The gaps [#1-the-gaps]

The goal for memory is at most 0.7x of go. Run time and CPU must be at least 1.2x faster.

| Program | CPU go/marmot | Peak RSS marmot/go | Bound by |
| --- | --- | --- | --- |
| jsondec | 1.07x | 2.16x (68 against 31 MB) | live heap retention (section 3) |
| mapint | 1.20x | 1.48x (144 against 95 MB) | map growth (section 4) |
| mapstr | 1.00x | 1.39x (138 against 98 MB) | map growth, probe cache misses |
| stringsearch | 3.0x | 1.41x (35 against 24 MB) | |
| wordfreq | 1.12x | 1.36x (550 against 401 MB) | buffer growth before a collection (section 4) |
| fmtout | 0.81x | 1.14x | fmt code (not runtime) |
| strconcat | 1.00x | 0.99x | a byte loop vectorized 2 bytes per iteration (backend) |
| pingpong, selectpipe | 2.0x, 2.2x | 1.5x to 1.7x (small programs) | dynamic libc pages (section 5) |
| workerpool | 0.88x | 1.5x | the work loop (compute) |
| bintrees | 1.54x | 0.94x | |

The scheduler CPU gap of the last scoreboard is gone: pingpong and selectpipe now use about half of go's CPU. Memory is the largest gap in the runtime's area: no program except bintrees meets 0.7x.

The three gaps picked: live heap retention by the conservative scan (jsondec) and peaks of growing data structures (mapint, mapstr, wordfreq). The third is the base RSS of small programs.

## 2. What other systems do (evidence) [#2-what-other-systems-do-evidence]

- Go's scavenger keeps the backed heap near the heap goal plus 10% (retainExtraPercent = 10). It releases on heap growth, before new pages are mapped. Source: runtime/mgcscavenge.go (https://go.dev/src/runtime/mgcscavenge.go). Design 59960, a more hugepage-aware Go heap (https://go.googlesource.com/proposal/+/HEAD/design/59960-heap-hugepage-util.md).
- Go 1.24 maps are swiss tables split by extendible hashing: a directory of tables of at most 1024 slots, each grown on its own. So a growing map never holds two copies of the whole table. Source: internal/runtime/maps (https://go.dev/src/internal/runtime/maps/map.go). The Go blog, Faster Go maps with Swiss Tables (2025).
- TCMalloc's hugepage-aware allocator (Hunter et al., OSDI 2021): 26% less memory lost to fragmentation across the fleet. One service had 6.5% lower peak memory, with more requests per second. Https://www.usenix.org/conference/osdi21/presentation/hunter
- Mesh (Powers, Tench, Berger, McGregor, PLDI 2019): compaction without moving objects by remapping pages. Firefox memory -16%, Redis -39%. Https://arxiv.org/abs/1902.04738
- Conservative collection space bounds (Boehm, POPL 2002): one false reference can retain an unbounded amount. The bound depends on the data structures. Https://hboehm.info/gc/bounds.html
- Conservative Immix (Shahriyar, Blackburn, McKinley, OOPSLA 2014) has a precise heap and conservative stacks. Excess retention in Java benchmarks averaged about 0.02% of the heap, at most 6.1% (secondary source, check the paper). The small numbers come from short frames and little stale stack memory.
- Liveness for root sets: Agesen, Detlefs, Moss (PLDI 1998) found intraprocedural liveness of local variables mostly prevents bad surprises. Hirzel, Diwan, Henkel report up to 62% smaller reachable heaps with interprocedural liveness that covers records and arrays.

## 3. Gap: live heap retention by the conservative stack scan (jsondec) [#3-gap-live-heap-retention-by-the-conservative-stack-scan-jsondec]

Measurement: after runtime.GC() inside the decode loop, go keeps 713 objects (2.7 MB), marmot 272,851 (28.6 MB).

- main's frame keeps the dead local items (used only before the loop) and the previous round's result. Moving the setup into a function that returns: 148,849 objects.
- The remaining objects came from the runtime itself. The register buffer that the collector hands to rt_spill_regs had 24 words. rt_spill_regs fills 6 of them on x86-64 (20 on arm64), but the scan covered all 24. The other words were stale stack memory from the decoder's deeper frames. Zeroing the buffer: 719 objects, as go.

Candidates:

1. Zero the register buffer (done: 4f483e6d). Cost: 24 stores per collection. No risk: it removes false roots only.
2. Precise liveness for pointer locals (stack maps, MARMOT_GCPTR): would remove the dead items reference. Expected jsondec peak RSS 68 to about 36 MB (measured with the setup moved out: 42 MB, go 31 to 36 MB). MARMOT_GCPTR=1 fails to build -release today and does not change the result at -O 2.
3. Cheaper than full maps: clear dead pointer spill slots before allocation slow paths and calls that can collect (as park clear does at park calls). Backend's idea. Its gain is the part of 2 that comes from spill slots.

Pick: 1 (done), then 2 or 3 in the backend.

## 4. Gap: peaks of growing data structures (mapint, mapstr, wordfreq) [#4-gap-peaks-of-growing-data-structures-mapint-mapstr-wordfreq]

Measurement: a marmot map is one table that doubles. Every rebuild left the earlier tables resident until the program ended. The next table is twice as large and cannot reuse them, and no collection ran in between to release them. A bytes.Buffer that doubles does the same, and wordfreq's heap reaches 659 MB before its next collection.

Candidates:

1. Free the old table at a rebuild when the map was never iterated. Give large freed objects back to the OS at once (done: 993e8863). Mapint 144 to 107 MB, mapstr 138 to 111 MB, CPU unchanged. Reviewed for soundness (no free during a concurrent mark, no stop between the test and the free).
2. A directory of bounded tables (Go 1.24's design): a rebuild copies at most 1024 slots, so the transient copy is small. Expected: mapint toward go's 95 MB and less time in large copies. Cost: the table layer of map.go (about 500 lines). Risk: medium (iteration rules across table splits).
3. Release on heap growth, as go's scavenger does (target: goal plus 10%). Measured tonight in two forms, both worse. A tighter retain target after each sweep: wordfreq 547 to 678 MB, parse CPU +50%. Release before arena growth: wordfreq 549 to 677 MB. Releasing pages should not raise the peak, so the free span merging or the choice of spans after a release does something unexpected. Next step: trace spans and released bytes in wordfreq before enabling either.

4. Collect before a large allocation that crosses the trigger, not after (done: cf887266). Found while investigating 3: the release policies were not the cause of wordfreq's peak. The collection timing was. Wordfreq 550 to 409 MB (go 401 MB), stringsearch 27.7 to 17.3 MB (go 22.9 MB), jsondec 68.5 to 63.5 MB, CPU unchanged.

Item 3 explained: released spans were marked as needing zeroing, although MADV_DONTNEED already zeroes them on Linux. So a large allocation from them ran a memset over all its pages. Marked clean, the release policies no longer raise the peak, but they do not lower it either and cost CPU on parse. Dropped.

Pick: 1 and 4 (done), then 2 (bounded tables) if the map rows still miss the goal after the gates.

## 5. Gap: base RSS of small programs [#5-gap-base-rss-of-small-programs]

Measurement: a program that sleeps: marmot 4.4 MB VmRSS, go 4.9 to 5.5 MB. Pingpong maxrss: marmot about 2.1 to 4.3 MB, go 1.3 to 2.8 MB, with large run to run variation. Of marmot's resident pages, about 1.5 MB are libc and the dynamic loader (file pages, shared with other processes). go's static binary has none. One 2 MB anonymous region is resident at once, which fits a transparent huge page (the machine runs THP "always").

Candidates:

1. Static linking (musl) for small programs, or by default: removes the libc pages.
2. MADV_NOHUGEPAGE for the sparse runtime tables (page map, heap bitmap, card table): tried, no measurable change in the small programs. The variation between runs is larger than the effect.

Pick: none in the runtime tonight. The number is within noise of go except for the libc pages.

## 6. Dropped tonight [#6-dropped-tonight]

- Scheduler spinning: not a gap any more (section 1).
- A tighter scavenge target after each sweep and release before heap growth: both raised wordfreq's peak (section 4, item 3).
- MADV_NOHUGEPAGE for the runtime tables: no measurable change (section 5).