Memory by owner, the heap across a stall, memory growth (#12901)

Since Bun 1.4.4 the serving process has held several GiB after hours. This change makes the growth readable; it does not stop it (see "Not done").

Where it shows

What to do when the need shows

  1. The need names the VM and the part. To see the curve, read the memory main and memory worker lines around the time named: heap up means JS objects, heap flat with RSS up means native memory, a worker line up means that worker. Then read the memory holds: lines across the same minutes: a holder that grew with the heap is named there with its count.
  2. Check the stall lines for fell: a collection ran: stalls that are collections point at the heap.
  3. Restart the forge to release it, and report the lines with the issue.

The need clears by itself once RSS falls under twice the baseline. A process that slept through its first hour has no baseline and raises none.

How it is built

src/perf/memory_watch.ts owns the readings (heapSize() and memoryUsage(), constant time; never heapStats(), which held the loop ~280 ms a minute, #11118). vitals() takes the watch as watch, as it takes disk. Each worker entry calls workerMemory(<name>) once at start; FORGE_WORKER_MEMORY_MS shortens the minute for tests. The same minute timer that writes a worker's line posts its reading ({ name, heap, ceiling? }) on the forge-memory BroadcastChannel (tellReading); the serving VM's memory sampler listens (hearWorkers), validates each message (workerReadingOf: a lower-case name, not main, finite byte counts) and notes it. Asks carry nothing extra, and at most 64 workers are kept. Tests: test/memory-watch.test.ts, test/db-ceiling.test.ts, test/worker-memory-lines.test.ts, test/vitals.test.ts, test/tui-memory-growth.test.ts.

What grew, and what holds it now

Found on a copy of production's data with no network, production's own work on (apps, crew, checks, the picture keeper) and a replay of pages, API and MCP calls, event streams and pushes; object counts by type from bun:jsc heapStats() after a full collection, and heap snapshots, at 5, 20 and 60 minutes.

A new long-lived collection names itself in heldOf(app) (a forge's) or threadHeld (the thread's), with a bound and a test of its eviction. Named today: turn, requests in flight, event streams, request counters, route timings, map plans, map lanes, map layouts, finishing said (the newest 512 pulls, src/crew/said_once.ts), and three the crew keeps per pull or per check (autonomy conflicted pulls, checks retried, app links said), each now bounded to its newest 1024 entries (src/perf/bounded.ts, #15210).

Reading the retainers (#15286)

heapStats() and a snapshot walk the whole heap, so neither runs on the serving process by default. On a copy of production's data (never production), set FORGE_HEAP_CAPTURE_DIR=<dir under FORGE_APP_DATA or $TMPDIR> (and FORGE_HEAP_CAPTURE_MIN, default 20): the process writes heap-<minutes up>.json (object counts by type after a full collection) and heap-<minutes up>.snapshot.json at boot and every interval (src/perf/heap_capture.ts; unset, nothing runs). Then bun scripts/heap-growth.ts heap-0000.json heap-0060.json prints the types that grew, bytes by type, and for the three that grew most the holders as <holder type>.<edge name> with count and bytes (src/perf/heap_diff.ts). The holder type and edge name point to the module: grep the property name.

Not yet measured. The hour on production's copy has not been taken: the run that built this tool could not read production's data. Until it is, the retainers of the main heap's 251 to 1057 MiB (#15210) are not named, and the three fixes above are the ones the earlier hour found. A short boot-plus-1,500-requests run on an empty forge works end to end and held nothing growing beyond collectable strings. Whoever takes the hour should paste the heap-growth output on #15286, name the largest holder and its module here, and bound it with a test of its eviction; if the growth is outside the heap (extraMemorySize or RSS rising while heapSize is flat), open a follow-up naming the native owner.

Tests: test/heap-diff.test.ts, test/app-turn.test.ts, test/cache-holds.test.ts, test/forge-sleep.test.ts, test/held.test.ts.

A heap census on the running forge (#15309)

To find what grows on production itself, with no copy of its data: on /system, under Forge memory, a forge admin who is a person presses Take a heap census. One heapStats() (bun:jsc) of the main VM counts every object on the heap by type. It holds the forge for a fraction of a second on a large heap (~280 ms on 2M objects), so it runs only on that press, never on a timer, and at most once every ten minutes; a second press sooner is answered 429 with Retry-After and the sentence why.

Built in src/perf/heap_census.ts; tests: test/heap-census.test.ts, test/tui-system.test.ts.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs