Skip to Content
marmot

Date: 2026-11-18. Status: proposal, no code yet.

Question

The macOS bench run (2026-11-17, main bd3fa968) shows worse ratios to go than Linux for programs that free large memory. wordfreq 1.68 (Linux 1.37), mapint 1.39 (1.09), mapstr 1.31 (1.12). Does the runtime's page release work on macOS, and does the bench see it?

Probe

A C program on macOS touches 200 MiB (region A) and releases A with one method. Then it touches another 200 MiB (region B). It reads resident_size and phys_footprint (task_info TASK_VM_INFO) and ru_maxrss after each step.

MethodAfter release: residentfootprintAfter B: residentfootprintmaxrss
none201 MiB201401401401
MADV_FREE_REUSABLE (the runtime's os_release)2011401201401
MADV_FREE201201401401401
MADV_DONTNEED201201401401401
mmap MAP_FIXED over A11201201201

Findings

  1. MADV_FREE_REUSABLE works for the system: phys_footprint (the "Memory" column of Activity Monitor, and what memory pressure acts on) drops at once.
  2. resident_size and ru_maxrss do not drop. Released pages stay resident until the system takes them. The bench reads ru_maxrss, so on macOS it cannot see any release: 9917e9bc (map tables freed) and every scavenge are invisible in the Mac column. This explains why the map rows are worse on the Mac than on Linux, where MADV_DONTNEED lowers RSS at once.
  3. Go on darwin also releases with MADV_FREE_REUSABLE (sysUnused), so its ru_maxrss has the same blind spot. The macOS ratios still differ from Linux. The two runtimes release at different times and reuse freed memory differently. A runtime that reuses its freed pages before it touches new ones keeps ru_maxrss lower.
  4. Only a remap (mmap MAP_FIXED over the range) lowers resident_size and maxrss. It costs one system call per range and a zero-fill fault per page on reuse, the same as MADV_DONTNEED on Linux.

Proposal, in order

  1. Measurement first (bench/cmd/bench): on darwin, report the lifetime peak of phys_footprint beside ru_maxrss, for go and marmot. The source is proc_pid_rusage, RUSAGE_INFO_V4, ri_lifetime_max_phys_footprint. That is the number a user sees, and it counts releases on both runtimes. Then redo the Mac ratios. I expect the map rows to move toward the Linux ratios, but that needs the measurement.
  2. Runtime, only if the footprint ratios still miss: on darwin, prefer free released spans over fresh arena pages when the heap grows. Reused REUSABLE pages are already counted in resident size, and fresh pages add to it. This touches page_alloc's span choice only. Measure ru_maxrss, footprint and CPU, opt in behind an environment variable, then run perfcmp on macOS.
  3. Not proposed: releasing with mmap MAP_FIXED on darwin. It lowers the reported RSS, but it costs a system call and page faults for memory that phys_footprint already counts as free. It would optimize the metric, not the program.

Time cost when the memory is needed again

  • MADV_FREE_REUSABLE: no fault if the system did not take the page. The runtime never calls MADV_FREE_REUSE before it reuses a released span (os_darwin.c has no such call, go calls it in sysUsed). Without it, darwin may leave reused pages out of phys_footprint, so the footprint measurement of item 1 could read low for marmot. Add the call with item 1, before comparing footprints: one madvise per reused released span.
  • MADV_DONTNEED (Linux): a zero-fill fault per 4 KiB page on reuse, about 0.1 to 0.25 us each. A 128 MiB reuse costs about 32k faults, 3 to 8 ms.
  • The scavenge policy (when and how much) stays as it is: after each collection, idle free memory above the goal (heap_scavenge(next_gc)). Section 15 of rtcore.md measured tighter policies: they cost CPU and did not lower the peak.

On this page

Was this page helpful?

Report an issueOpen issues