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
- The minute line. Each minute the log carries
memory main: rss …, heap …, process mimalloc committed … (peak …), up … min. Every worker VM writesmemory worker <name>: heap …, process rss …on its own minute (#12925). Only the heap is one VM's own: Bun'smemoryUsage()is the whole process's, and itscurrentis the RSS itself, not malloc (#14264). RSS rising while the main heap stays flat is native memory; a worker line rising is that worker's. Read them in/system's log or the process log. - What is held. Beside the minute line the log carries
memory holds: <name> <count>, …: the forge's long-lived Maps, Sets and lists by name with how many entries each holds now, largest first, twelve at most (src/perf/held.ts). A worker whose thread holds anything writesmemory worker <name> holds: …. A name whose count only rises from line to line is the holder to read. - The stall line. Each loop-stall line ends with
heap X (was Y N s ago; …), the heap now against the last minute's sample.fell: a collection ranmeans the heap shrank by more than a tenth since the sample, so the collector ran inside that stall. Bun 1.4.4 reports no collector time, so this is inferred, not measured.did not fall: no collection seenmeans the stall was not the collector's.
- The need. When RSS is at least twice the mean of the process's first hour (at least ten samples),
/system, the board's forge strip and the terminal's home each list one need: the figure, the first-hour figure, which VM's part grew most and since when (#12951), for example "The forge's own memory is 4200 MiB, over twice its first-hour 2000 MiB; worker pages's JS heap grew most (100 MiB to 2410 MiB) since 2026-11-15T04:00:00.000Z." - What fills by design is not growth (#14343). A database handle may fill its page cache (
FORGE_DB_CACHE_MIB, 1 GiB on the serving handle, 128 MiB on the engine's) and its 512 MiB map as pages are read, each capped by the file's size. Two handles are counted: the serving VM'sapp.db(src/boards/memory.ts) and the engine worker'sapp.db(src/engine/worker.ts). Each reads its ceiling from the handle's own pragmas with its minute reading (ceilingOfinsrc/kernel/db_ceiling.ts:min(cache, file) + min(map, file)); nothing is registered, and a closed handle reads 0. On production's 4.5 GB database the two come to 2176 MiB, most of it filled after the first hour. The line saysdatabase caches up to …; the engine's crosses on the channel asceiling. The need speaks only when RSS is past twice the first hour and past the first hour plus the ceilings of the VMs heard from in the last five minutes; the sentence then names that sum. The read-only handles of thepages,lanes,pipelineandmapworkers are not counted: their caches can only make the need fire sooner, never hide it. Production's evidence on #14343 held 3.0 to 3.3 GiB for twelve hours against a 1420 MiB first hour: a plateau under 1420 + 2176 = 3596 MiB, the shape of a warm cache, where a leak keeps climbing. The ceiling is an upper bound, so a leak smaller than the part of the two caches still cold is not said until it passes it. - The parts. Each owner (the main VM, and each worker by name) keeps its own first-hour mean of its
JS heap(heapSize()); main also keeps the process'smemory outside every JS heap(RSS less main's heap and every reporting worker's, floored at 0: malloc, SQLite's pages, native buffers). That part is the process's, not one VM's, and the sentence says so; mimalloc's commit is printed on the line but is never a part, since it overlaps the heaps. A worker's hour starts at its first reading, so one started on its first ask has its own. The part named is the one that grew the most bytes past its baseline. A worker silent five minutes is not named, and its heap stops counting against the outside. When no part is above its own hour the sentence says so. - The worker names.
engine(src/engine/worker.ts);disk,tree(src/apps);lanes,pages,pipeline(src/flow);scan,du,leak(theanswerAsksworkers of src/kernel/worker_ask.ts);thumb(src/looks);log-writer(src/runs);mirror,vacuum(src/snapshot);code-rows,map,raster(src/web). A one-shot worker (mirror,vacuum) that ends inside its first minute writes no line; the timer is unref'd, so it never keeps a worker alive.
What to do when the need shows
- The need names the VM and the part. To see the curve, read the
memory mainandmemory workerlines 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 thememory holds:lines across the same minutes: a holder that grew with the heap is named there with its count. - Check the stall lines for
fell: a collection ran: stalls that are collections point at the heap. - 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.
- The turn gate kept every request an app's host answered (
src/http/app_turn.ts, in since 0.7.598, one release before Bun 1.4.4). The listener began a turn for every request and only the forge's own routes ended it; a request answered ahead of the routes (an app's host through the relay, a refusal) stayed in the gate's Map, with itsRequest,HeadersandAbortSignal, until an app's own token next waited on a quiet thread, which on production is never. On the copy, with nobody visiting an app, the looks' Chromium alone asked previews for about 450 requests a minute. The listener now leaves the gate in itsfinally, whatever door answered: the gate holds what is in flight. Bun holds nothing here: a servedRequestis collected on 1.3.14 and 1.4.4 alike once nothing of ours keeps it. - The plan and lane caches kept answers nobody could ask for again (
src/web/map_cache.ts,src/view/cache.ts). A plan was keyed by its stamp, which moves with every run that starts or spends, and a lane by its repository's latest event; an entry past its minute was only dropped when the same key was read again or the count (64 plans, 128 lanes) pushed it out. A plan holds its repository's whole graph: the copy held ten graphs of the busiest repository (4,593 issues each) after an hour of page views, and the cache had room for 64. Now there is one plan per repository, spend visibility and day, and one lane per repository, scope and day, each replaced when its stamp moves; and every view cache drops what is past its time on the first write afterSWEEP_MS(30 s). - A hosted forge that slept left its request counter and route timing behind (
src/http/server.ts), one more of each with every wake, each keeping the closed database and a flush that retried against it. They now go when the forge closes, written out first.
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.
- The first census is the baseline. Press again an hour or more later; from the second census on,
/systemlists the twenty object types that grew most from the first to the last, each with its count then and now. A type that only grows from press to press is the retainer to look for. - Each census is a row of
heap_censusin the forge's database ({at, uptimeMin, heapSize, objectCount, byType}): the first and the last twenty are kept, 21 rows at most. - The crew reads the same data as JSON,
GET /api/v1/telemetry/heap-census(the owner, or an agent holdingtelemetry:read):{ censuses: [{at, uptimeMin, heapSize, objectCount}], grew: [{type, then, now}] }. Reading it never takes a census. An agent's token asking to take one is refused403with one sentence. - In the terminal, the memory screen (
src/tui/screens/system.ts,censusLines) shows the same count and comparison, andcthere takes a census (takeCensus): the same gate, the same ten-minute limit and the same sentence on refusal, as a toast, into the same table the page reads.
Built in src/perf/heap_census.ts; tests: test/heap-census.test.ts, test/tui-system.test.ts.