Public: anyone can read this; the crew can change it.

The memory line says what is held: RSS and its anonymous part, and the allocator's churn as a rate, never "committed 126 GiB" #17667

opened this

Merged 20h 10m ago, live in production

memory-line-true → main 0 commits 2 threads · 2 unresolved

  • Merged. memory-line-true was merged into main as 59691dc.

Review

Sign in to review this change.
7 files changed +128 −49
docs/features/memory-growth.md +2 −2
@@ -8,14 +8,14 @@ Since Bun 1.4.4 the serving process has held several GiB after hours. This chang
88
99 ## Where it shows
1010
11 - **The minute line.** Each minute the log carries `memory main: rss …, heap …, process mimalloc committed … (peak …), up … min`. Every worker VM writes `memory worker <name>: heap …, process rss …` on its own minute (#12925). Only the heap is one VM's own: Bun's `memoryUsage()` is the whole process's, and its `current` is 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.
11 - **The minute line.** Each minute the log carries `memory main: rss … (anonymous …), heap …, allocator churn … a minute, database caches up to …, up … min`. Bun's mimalloc total (`currentCommit`) only grows on Linux (126,739 MiB 22 minutes after a boot, beside a VmSize of 22 GiB), so it is said only as churn, a rate; what is held is RSS and its anonymous part. Every worker VM writes `memory worker <name>: heap …, process rss …` on its own minute (#12925). Only the heap is one VM's own: Bun's `memoryUsage()` is the whole process's, and its `current` is 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.
1212 - **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 writes `memory worker <name> holds: …`. A name whose count only rises from line to line is the holder to read.
1313 - **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.
1414 - `fell: a collection ran` means 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.
1515 - `did not fall: no collection seen` means the stall was not the collector's.
1616 - **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."
1717 - **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's `app.db` (`src/boards/memory.ts`) and the engine worker's `app.db` (`src/engine/worker.ts`). Each reads its ceiling from the handle's own pragmas with its minute reading (`ceilingOf` in `src/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 says `database caches up to …`; the engine's crosses on the channel as `ceiling`. 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 the `pages`, `lanes`, `pipeline` and `map` workers 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.
18 - **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's `memory 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.
18 - **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's `memory 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; the allocator's churn is a rate on the line and never a part. 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.
1919 - **The worker names.** `engine` (src/engine/worker.ts); `disk`, `tree` (src/apps); `lanes`, `pages`, `pipeline` (src/flow); `scan`, `du`, `leak` (the `answerAsks` workers 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.
2020
2121 ## What to do when the need shows
src/boards/memory.ts +17 −6
@@ -21,7 +21,7 @@ import { heldLine, heldNow } from "../perf/held.ts";
2121 import { allocationLine } from "../perf/job_alloc.ts";
2222 import { ceilingOf } from "../kernel/db_ceiling.ts";
2323 import { keepIfGrown } from "../perf/memory_kept.ts";
24 import { hearWorkers, memoryLine, memoryWatch, readNow, type MemoryWatch } from "../perf/memory_watch.ts";
24 import { churnPerMinute, hearWorkers, memoryLine, memoryWatch, readNow, type MemoryWatch, type Reading } from "../perf/memory_watch.ts";
2525
2626 export const MEMORY_EVERY_MS = 60_000;
2727 export const HOUR_MS = 3_600_000;
@@ -74,12 +74,23 @@ export type MemorySampler = { tick(now?: number): void; stop(): void };
7474
7575 /**
7676 * Sample now and every minute until stopped. A failed tick is logged and the next one tries again.
77 * `watch` is the process's own; a test hands it one already grown (#16872).
77 * `watch` is the process's own; a test hands it one already grown (#16872), and `read` its own
78 * readings, so the churn between two ticks is known.
7879 */
79 export function startMemorySampler(app: App, everyMs = MEMORY_EVERY_MS, watch: MemoryWatch = memoryWatch): MemorySampler {
80 export function startMemorySampler(
81 app: App,
82 everyMs = MEMORY_EVERY_MS,
outdated

The before state is the new behaviour and nothing tests it. Tick the sampler twice with a fake now and assert that the second line has allocator churn and the first does not.

83 watch: MemoryWatch = memoryWatch,
84 read: (ceiling: number) => Reading = (ceiling) => readNow(undefined, ceiling),
85 ): MemorySampler {
86 // The last reading's allocator total, for its rate (`churnPerMinute`): the total itself is no
87 // size.
88 let before: { committedSoFar: number; at: number } | undefined;
8089 const tick = (now = Date.now()) => {
8190 try {
82 const reading = readNow(undefined, ceilingOf(app.db));
91 const reading = read(ceilingOf(app.db));
92 const churn = churnPerMinute(before, { committedSoFar: reading.committedSoFar, at: now });
93 before = { committedSoFar: reading.committedSoFar, at: now };
8394 recordMemory(app.db, reading.rss, reading.heap, now);
8495 // The held counts are read once: the line writes them, the watch keeps them for a finding
8596 // (#14362).
@@ -90,12 +101,12 @@ export function startMemorySampler(app: App, everyMs = MEMORY_EVERY_MS, watch: M
90101 // The jobs open now: a trip keeps them, so native memory held by work that ended names it
91102 // (#17083).
92103 watch.note(reading.rss, reading.heap, now, {
93 commit: reading.commit, held, ceiling: reading.dbCeiling, allocated: jobs.jobs, skipped: jobs.skipped, running: runningOf(app),
104 ...(reading.anon === undefined ? {} : { anon: reading.anon }), held, ceiling: reading.dbCeiling, allocated: jobs.jobs, skipped: jobs.skipped, running: runningOf(app),
94105 });
95106 // A grown minute's evidence outlives this process (#16872): a restart would leave the
96107 // sentinel quoting a fresh watch that never tripped.
97108 keepIfGrown(app.config.dataDir, watch, now);
98 app.log.info(memoryLine(reading, process.uptime() * 1000));
109 app.log.info(memoryLine(reading, process.uptime() * 1000, churn));
99110 app.log.info(heldLine(held));
100111 const allocated = allocationLine(jobs);
101112 if (allocated !== undefined) app.log.info(allocated);
src/perf/memory_watch.ts +51 −27
@@ -12,9 +12,11 @@
1212 * What Bun can tell apart (#14264): `heapUsed` is one VM's own; `jsc.memoryUsage()` is the
1313 * whole process's, and its `current` is the RSS itself (not malloc: reading `current - heap`
1414 * as a VM's malloc blamed "the main VM's malloc" for every worker's heap and every native
15 * byte). Its `currentCommit` is mimalloc's commit for every thread at once, and overlaps the
16 * heaps (the heap counts the buffers its objects own), so it is printed on the line and
17 * is never a part. What is outside every heap belongs to the process, not to one VM. A worker's
15 * byte). Its `currentCommit` is not memory held: on Linux it only grows (126,739 MiB 22 minutes
16 * after a boot on 2026-12-02, beside a VmSize of 22 GiB and an RSS of 1.8 GiB), so the line says
17 * it only as the allocator's churn, its rise a minute, never as a size; what is held is RSS and
18 * its anonymous part. Churn overlaps the heaps (the heap counts the buffers its objects own), so it
outdated

This paragraph is stale. The commit is no longer "printed on the line"; only its rate is. The sentence is also broken mid-line (It overlaps the / heaps). Reflow it and say what is true now.

19 * is never a part either. What is outside every heap belongs to the process, not to one VM. A worker's
1820 * reading reaches the serving VM on a `BroadcastChannel` the worker posts to from the same minute
1921 * timer that writes its line (`tellReading`, `hearWorkers`), so no ask and no hot-path message
2022 * carries it; a worker silent for five minutes is not blamed.
@@ -45,6 +47,7 @@
4547 * need; a warm cache at its ceiling, which levels off where a leak keeps climbing, does not.
4648 */
4749 import { memoryUsage as jscMemory } from "bun:jsc";
50 import { readFileSync } from "node:fs";
4851 import { createLog } from "../kernel/log.ts";
4952 import { heldLine, threadHeld, type HeldCount } from "./held.ts";
5053 import type { Allocation } from "./job_alloc.ts";
@@ -71,9 +74,13 @@ export interface Reading {
7174 readonly rss: number;
7275 /** This VM's own JS heap (`memoryUsage().heapUsed`). */
7376 readonly heap: number;
74 /** `jsc.memoryUsage().currentCommit` and its peak: mimalloc's, for the whole process. */
75 readonly commit: number;
76 readonly peakCommit: number;
77 /** The anonymous part of RSS (`RssAnon` in /proc/self/status): undefined where there is none. */
78 readonly anon: number | undefined;
79 /**
80 * `jsc.memoryUsage().currentCommit`: what mimalloc has committed since the process began, as
81 * Bun reports it. It only grows, so only its rate is said (`churnPerMinute`), never its size.
82 */
83 readonly committedSoFar: number;
7784 /** What this VM's open databases may fill by design (`kernel/db_ceiling.ts`). */
7885 readonly dbCeiling: number;
7986 }
@@ -92,13 +99,30 @@ function vmHeap(): number {
9299 * database handle may fill (`ceilingOf` in `kernel/db_ceiling.ts`), 0 for a VM with none.
93100 */
94101 export function readNow(rss: number = process.memoryUsage().rss, dbCeiling = 0): Reading {
95 const m = jscMemory();
96 return { rss, heap: vmHeap(), commit: m.currentCommit, peakCommit: m.peakCommit, dbCeiling };
102 return { rss, anon: rssAnon(), heap: vmHeap(), committedSoFar: jscMemory().currentCommit, dbCeiling };
97103 }
98104
99 /** The serving VM's minute line: `memory main: rss 5.1 GiB, heap …`. */
100 export function memoryLine(r: Reading, uptimeMs: number): string {
101 return `memory main: rss ${mib(r.rss)}, heap ${mib(r.heap)}, process mimalloc committed ${mib(r.commit)} (peak ${mib(r.peakCommit)}), database caches up to ${mib(r.dbCeiling)}, up ${Math.round(uptimeMs / 60000)} min`;
105 /** The process's anonymous RSS from /proc/self/status, in bytes; undefined off Linux. */
106 export function rssAnon(status: () => string = () => readFileSync("/proc/self/status", "utf8")): number | undefined {
107 try {
108 const kb = /^RssAnon:\s+(\d+) kB$/m.exec(status())?.[1];
109 return kb === undefined ? undefined : Number(kb) * 1024;
110 } catch {
111 return undefined;
112 }
113 }
114
115 /** The allocator's churn between two readings, bytes a minute; undefined for a first or a reset. */
116 export function churnPerMinute(before: { committedSoFar: number; at: number } | undefined, now: { committedSoFar: number; at: number }): number | undefined {
117 if (before === undefined || now.at <= before.at || now.committedSoFar < before.committedSoFar) return undefined;
118 return ((now.committedSoFar - before.committedSoFar) * 60_000) / (now.at - before.at);
119 }
120
121 /** The serving VM's minute line: `memory main: rss 1844 MiB (anonymous 1662 MiB), heap …`. */
122 export function memoryLine(r: Reading, uptimeMs: number, churn?: number): string {
123 const anon = r.anon === undefined ? "" : ` (anonymous ${mib(r.anon)})`;
124 const turned = churn === undefined ? "" : `, allocator churn ${mib(churn)} a minute`;
125 return `memory main: rss ${mib(r.rss)}${anon}, heap ${mib(r.heap)}${turned}, database caches up to ${mib(r.dbCeiling)}, up ${Math.round(uptimeMs / 60000)} min`;
102126 }
103127
104128 /** A worker VM's line: its heap is its own; RSS is the process's. */
@@ -220,12 +244,12 @@ function mostGrown(owners: Iterable<readonly [string, Owner]>, now: number): Gro
220244 }
221245
222246 /**
223 * What the serving VM's minute tick also hands the watch (#14362): mimalloc's commit for the
224 * process and the held-by-name counts it already read for its line. Both are kept as the
225 * latest and, at the trip, as they stood then; nothing here is read by the watch itself.
247 * What the serving VM's minute tick also hands the watch (#14362): the process's anonymous RSS
248 * and the held-by-name counts it already read for its line. Both are kept as the latest and, at
249 * the trip, as they stood then; nothing here is read by the watch itself.
226250 */
227251 export interface NoteExtra {
228 readonly commit?: number;
252 readonly anon?: number;
229253 readonly held?: readonly HeldCount[];
230254 /** What this VM's database handle may fill by design (`kernel/db_ceiling.ts`, #14343). */
231255 readonly ceiling?: number;
@@ -244,7 +268,7 @@ export interface NoteExtra {
244268 interface Trip {
245269 readonly at: number;
246270 readonly rss: number;
247 readonly commit: number | undefined;
271 readonly anon: number | undefined;
248272 readonly outside: number | undefined;
249273 /** Each fresh owner's JS heap then, by name. */
250274 readonly heaps: ReadonlyMap<string, number>;
@@ -292,8 +316,8 @@ export interface MemoryWatch {
292316 grown(): Grown | undefined;
293317 /**
294318 * The lines a memory-growth finding quotes (#14362): each reporting VM's JS heap and the
295 * process's RSS outside every heap at the first-hour mean, at the trip and now; mimalloc's
296 * commit; the largest held-by-name counts at the trip and now. Read from what the minute
319 * process's RSS outside every heap at the first-hour mean, at the trip and now; its anonymous
320 * RSS; the largest held-by-name counts at the trip and now. Read from what the minute
297321 * tick kept: nothing is measured here. Empty before any reading.
298322 */
299323 evidence(): string[];
@@ -313,9 +337,9 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch {
313337 /** The last minute RSS was not above 1.25x its first hour (every first-hour minute is one). */
314338 let below: number | undefined;
315339 let latest: number | undefined;
316 let commitSum = 0;
317 let commitCount = 0;
318 let commitNow: number | undefined;
340 let anonSum = 0;
341 let anonCount = 0;
342 let anonNow: number | undefined;
319343 let heldNow: readonly HeldCount[] = [];
320344 let runningNow: readonly Running[] | undefined;
321345 let trip: Trip | undefined;
@@ -346,10 +370,10 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch {
346370 main.ceiling = bytes(extra.ceiling) ? extra.ceiling : 0;
347371 last = { heap, rss, at: now };
348372 latest = rss;
349 commitNow = bytes(extra.commit) ? extra.commit : undefined;
350 if (commitNow !== undefined && now - startedAt < FIRST_HOUR_MS) {
351 commitSum += commitNow;
352 commitCount++;
373 anonNow = bytes(extra.anon) ? extra.anon : undefined;
374 if (anonNow !== undefined && now - startedAt < FIRST_HOUR_MS) {
375 anonSum += anonNow;
376 anonCount++;
353377 }
354378 if (extra.held !== undefined) heldNow = extra.held.slice();
355379 runningNow = extra.running === undefined ? undefined : extra.running.filter(isRunning);
@@ -390,7 +414,7 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch {
390414 for (const [name, w] of workers) if (now - w.at <= WORKER_STALE_MS) heaps.set(name, w.heap);
391415 const jobs = [...runJobs.values()].sort((a, b) => b.bytes - a.bytes).slice(0, EVIDENCE_JOBS);
392416 trip = {
393 at: now, rss, commit: commitNow, outside: main.latest.outside, heaps, held: heldNow, jobs, skipped: runSkipped,
417 at: now, rss, anon: anonNow, outside: main.latest.outside, heaps, held: heldNow, jobs, skipped: runSkipped,
394418 runStart: below ?? above, running: runningNow ?? [],
395419 };
396420 }
@@ -430,7 +454,7 @@ export function createMemoryWatch(startedAt: number = Date.now()): MemoryWatch {
430454 if (trip !== undefined) out.push(`memory allocated by jobs from the run's start to the trip: ${jobsWords(trip.jobs, trip.skipped)}`);
431455 if (trip !== undefined) out.push(`memory jobs open at the trip: ${openWords(trip.running, trip.runStart)}`);
432456 if (runningNow !== undefined) out.push(`memory jobs open now: ${openWords(runningNow)}`);
433 out.push(`memory mimalloc committed (process): ${three(commitCount > 0 ? commitSum / commitCount : undefined, trip?.commit, commitNow)}`);
457 out.push(`memory anonymous rss (process): ${three(anonCount > 0 ? anonSum / anonCount : undefined, trip?.anon, anonNow)}`);
434458 const then = new Map((trip?.held ?? []).map((c) => [c.name, c.size]));
435459 const named = heldNow.filter((c) => c.size !== 0).slice(0, EVIDENCE_HELD);
436460 if (named.length === 0) out.push("memory held: nothing held by name now");
test/boot-idle-memory.test.ts +2 −2
@@ -1,7 +1,7 @@
11 /**
22 * A fresh forge on empty data, booted as production boots it, settles under `IDLE_RSS_BUDGET`
3 * (#16851). When it does not, the failure prints the forge's own memory line (JS heap, mimalloc
4 * committed, RSS) so the cause is named, not only the total.
3 * (#16851). When it does not, the failure prints the forge's own memory line (JS heap, anonymous
4 * RSS, RSS) so the cause is named, not only the total.
55 *
66 * Linux only (`IDLE_SKIP_REASON`).
77 *
test/held.test.ts +31
@@ -106,4 +106,35 @@ describe("the memory sampler", () => {
106106 await rm(dir, { recursive: true, force: true });
107107 }
108108 });
109
110 test("the allocator's churn is on the line from the second tick, as the rise since the first (#17667)", async () => {
111 const dir = await mkdtemp(join(tmpdir(), "forge-churn-"));
112 const app = createApp({ dataDir: dir, port: 0 });
113 const info = spyOn(app.log, "info").mockImplementation(() => {});
114 const MIB = 1024 * 1024;
115 const totals = [100 * MIB, 400 * MIB, 400 * MIB];
116 const read = (dbCeiling: number) => ({ rss: 900 * MIB, anon: 700 * MIB, heap: 50 * MIB, committedSoFar: totals.shift()!, dbCeiling });
117 const t0 = Date.now();
118 // The first tick is the sampler's own, as it starts; an hour apart, so no timer tick lands between.
119 const sampler = startMemorySampler(app, 3_600_000, undefined, read);
120 try {
121 sampler.tick(t0 + 30_000);
122 sampler.tick(t0 + 90_000);
123 const mains = info.mock.calls.map((call) => String(call[0])).filter((l) => l.startsWith("memory main:"));
124 expect(mains).toHaveLength(3);
125 expect(mains[0]).toContain("rss 900 MiB (anonymous 700 MiB)");
126 expect(mains[0]).not.toContain("churn");
127 // 300 MiB risen in the 30 s or so since the first tick: some 600 MiB a minute.
128 expect(mains[1]).toMatch(/allocator churn \d+ MiB a minute/);
129 expect(Number(/allocator churn (\d+) MiB/.exec(mains[1]!)![1])).toBeGreaterThan(300);
130 // Nothing risen in the next minute: churn 0, said as a rate, never the total.
131 expect(mains[2]).toContain("allocator churn 0.0 MiB a minute");
132 for (const l of mains) expect(l).not.toContain("400 MiB");
133 } finally {
134 sampler.stop();
135 info.mockRestore();
136 app.close();
137 await rm(dir, { recursive: true, force: true });
138 }
139 });
109140 });
test/memory-watch.test.ts +18 −5
@@ -1,6 +1,6 @@
11 import { expect, test } from "bun:test";
22 import {
3 createMemoryWatch, grownSentence, hearWorkers, memoryLine, tellReading, workerMemoryLine, workerReadingOf,
3 churnPerMinute, createMemoryWatch, grownSentence, hearWorkers, memoryLine, rssAnon, tellReading, workerMemoryLine, workerReadingOf,
44 BURST_SLACK, FIRST_HOUR_MS, MAX_OWNERS, MEMORY_CHANNEL, WORKER_STALE_MS, type WorkerReading,
55 } from "../src/perf/memory_watch.ts";
66 import { readNow } from "../src/perf/memory_watch.ts";
@@ -91,8 +91,11 @@ postMessage(readNow().heap);`);
9191 });
9292
9393 test("the minute lines name the owner and the figures", () => {
94 const r = { rss: 5 * 1024 * MIB, heap: 1024 * MIB, commit: 512 * MIB, peakCommit: 700 * MIB, dbCeiling: 1536 * MIB };
95 expect(memoryLine(r, 3_600_000)).toBe("memory main: rss 5120 MiB, heap 1024 MiB, process mimalloc committed 512 MiB (peak 700 MiB), database caches up to 1536 MiB, up 60 min");
94 const r = { rss: 5 * 1024 * MIB, anon: 4 * 1024 * MIB, heap: 1024 * MIB, committedSoFar: 126 * 1024 * MIB, dbCeiling: 1536 * MIB };
95 expect(memoryLine(r, 3_600_000, 5 * 1024 * MIB)).toBe("memory main: rss 5120 MiB (anonymous 4096 MiB), heap 1024 MiB, allocator churn 5120 MiB a minute, database caches up to 1536 MiB, up 60 min");
96 // The allocator's total is never said as a size: on Linux it only grows (#17629).
97 expect(memoryLine(r, 3_600_000)).not.toContain("126");
98 expect(memoryLine({ ...r, anon: undefined }, 60_000)).toBe("memory main: rss 5120 MiB, heap 1024 MiB, database caches up to 1536 MiB, up 1 min");
9699 expect(workerMemoryLine("engine", r)).toBe("memory worker engine: heap 1024 MiB, process rss 5120 MiB");
97100 expect(readNow().heap).toBeGreaterThan(0);
98101 });
@@ -100,7 +103,17 @@ test("the minute lines name the owner and the figures", () => {
100103 // #14264: `jsc.memoryUsage().current` is the RSS, not malloc. Read as "malloc", RSS less
101104 // main's heap blamed the main VM for every worker's heap and every native byte.
102105 test("no part is read from Bun's memoryUsage().current", () => {
103 expect(Object.keys(readNow()).sort()).toEqual(["commit", "dbCeiling", "heap", "peakCommit", "rss"]);
106 expect(Object.keys(readNow()).sort()).toEqual(["anon", "committedSoFar", "dbCeiling", "heap", "rss"]);
107 });
108
109 test("the allocator's total is said as a rate a minute, and anonymous RSS from the kernel's own line", () => {
110 expect(churnPerMinute(undefined, { committedSoFar: 9, at: 1 })).toBeUndefined();
111 expect(churnPerMinute({ committedSoFar: 100 * MIB, at: 0 }, { committedSoFar: 400 * MIB, at: 30_000 })).toBe(600 * MIB);
112 // A total that went down is a new process's, not a negative churn.
113 expect(churnPerMinute({ committedSoFar: 400 * MIB, at: 0 }, { committedSoFar: 100 * MIB, at: 60_000 })).toBeUndefined();
114 expect(rssAnon(() => "Name:\tbun\nVmRSS:\t 1849696 kB\nRssAnon:\t 1701708 kB\nRssFile:\t 147988 kB\n")).toBe(1701708 * 1024);
115 expect(rssAnon(() => "Name:\tbun\n")).toBeUndefined();
116 expect(rssAnon(() => { throw new Error("no /proc here"); })).toBeUndefined();
104117 });
105118
106119 // #12951: which VM grew, and which part. First hour: rss 2000, main heap 500, worker pages
@@ -227,7 +240,7 @@ test("a worker's reading crosses on the memory channel to the watch that hears i
227240 const { promise: two, resolve } = Promise.withResolvers<void>();
228241 const stop = hearWorkers({ noteWorker: (r) => { got.push(r); if (got.length === 2) resolve(); } });
229242 try {
230 const r = { rss: 9, heap: 3 * MIB, commit: 7 * MIB, peakCommit: 9, dbCeiling: 640 * MIB };
243 const r = { rss: 9, anon: undefined, heap: 3 * MIB, committedSoFar: 7 * MIB, dbCeiling: 640 * MIB };
231244 expect(tellReading("pages", r)).toBe(r);
232245 const other = new BroadcastChannel(MEMORY_CHANNEL);
233246 other.postMessage({ name: "../etc", heap: 1 }); // malformed: dropped
test/sentinel-memory-evidence.test.ts +7 −7
@@ -38,16 +38,16 @@ function grownWatch(start: number) {
3838 ];
3939 for (let i = 0; i < 12; i++) {
4040 w.noteWorker({ name: "pages", heap: 100 * MIB }, start + i * 5 * MIN);
41 w.note(2000 * MIB, 500 * MIB, start + i * 5 * MIN, { commit: 1200 * MIB, held: held(10) });
41 w.note(2000 * MIB, 500 * MIB, start + i * 5 * MIN, { anon: 1200 * MIB, held: held(10) });
4242 }
4343 // The hour is over: the baseline is struck, then the trip (twice the first hour), then now.
4444 const trip = start + FIRST_HOUR_MS + MIN;
4545 w.noteWorker({ name: "pages", heap: 100 * MIB }, start + FIRST_HOUR_MS);
46 w.note(2000 * MIB, 500 * MIB, start + FIRST_HOUR_MS, { commit: 1200 * MIB, held: held(10) });
46 w.note(2000 * MIB, 500 * MIB, start + FIRST_HOUR_MS, { anon: 1200 * MIB, held: held(10) });
4747 w.noteWorker({ name: "pages", heap: 2000 * MIB }, trip);
48 w.note(4100 * MIB, 500 * MIB, trip, { commit: 3000 * MIB, held: held(400) });
48 w.note(4100 * MIB, 500 * MIB, trip, { anon: 3000 * MIB, held: held(400) });
4949 w.noteWorker({ name: "pages", heap: 2410 * MIB }, trip + MIN);
50 w.note(4200 * MIB, 500 * MIB, trip + MIN, { commit: 3100 * MIB, held: held(900) });
50 w.note(4200 * MIB, 500 * MIB, trip + MIN, { anon: 3100 * MIB, held: held(900) });
5151 return { w, trip };
5252 }
5353
@@ -69,7 +69,7 @@ test("a memory-growth finding names the part that grew, each VM's heap and the h
6969 expect(text).toContain("memory heap main: first hour 500 MiB, trip 500 MiB, now 500 MiB");
7070 // Outside every heap: RSS less main's and the fresh worker's heap.
7171 expect(text).toContain("memory outside every heap (process): first hour 1400 MiB, trip 1600 MiB, now 1290 MiB");
72 expect(text).toContain("memory mimalloc committed (process): first hour 1200 MiB, trip 3000 MiB, now 3100 MiB");
72 expect(text).toContain("memory anonymous rss (process): first hour 1200 MiB, trip 3000 MiB, now 3100 MiB");
7373 expect(text).toContain("memory held sse: trip 400, now 900 (+500)");
7474 expect(lines.filter((l) => l.startsWith("memory held ")).length).toBe(EVIDENCE_HELD);
7575 expect(lines.length).toBeLessThan(40);
@@ -90,7 +90,7 @@ test("another cause's finding carries no memory lines", () => {
9090 test("a watch that never tripped says so, and lists no trip figures", () => {
9191 const start = Date.now() - 2 * FIRST_HOUR_MS;
9292 const w = createMemoryWatch(start);
93 for (let i = 0; i < 12; i++) w.note(1000 * MIB, 200 * MIB, start + i * 5 * MIN, { commit: 300 * MIB, held: [{ name: "sse", size: 4 }] });
93 for (let i = 0; i < 12; i++) w.note(1000 * MIB, 200 * MIB, start + i * 5 * MIN, { anon: 300 * MIB, held: [{ name: "sse", size: 4 }] });
9494 const lines = w.evidence();
9595 expect(lines[0]).toBe("memory rss: first hour 1000 MiB, trip none, now 1000 MiB");
9696 expect(lines).toContain("memory held sse: trip none, now 4");
@@ -103,7 +103,7 @@ test("a watch that never tripped says so, and lists no trip figures", () => {
103103 */
104104 function settledWatch(start: number) {
105105 const w = createMemoryWatch(start);
106 for (let i = 0; i <= 12; i++) w.note(1000 * MIB, 200 * MIB, start + i * 5 * MIN, { commit: 300 * MIB });
106 for (let i = 0; i <= 12; i++) w.note(1000 * MIB, 200 * MIB, start + i * 5 * MIN, { anon: 300 * MIB });
107107 return w;
108108 }
109109

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs