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

A memory reading's heap is never 0, so test/memory-watch.test.ts is green on main (#17155) #17248

opened this

Merged 35h 14m ago, live in production

readNow().heap and heapAcross() with no argument now take the VM's JS heap from process.memoryUsage().heapUsed instead of bun:jsc heapSize(), so neither reads 0 any more. Why it was 0: on Bun 1.4.4, heapSize() returns 0 until the VM's first garbage collection has measured the heap.

Notes

opened this

readNow().heap and heapAcross() with no argument now take the VM's JS heap from process.memoryUsage().heapUsed instead of bun:jsc heapSize(), so neither reads 0 any more.

Why it was 0: on Bun 1.4.4, heapSize() returns 0 until the VM's first garbage collection has measured the heap. bun -e prints 0, and after Bun.gc(true) it prints about 141 KB. A fresh worker, or a main VM between collections, therefore printed heap 0 MiB on its minute line, on /boards/memory and in a stall's heapAcross.

The new source: heapUsed is per VM. A worker that builds 300k objects reads about 25 MB while main reads its own 0.24 MB. Calling it costs about 12 µs, it doesn't depend on the heap's size, and it never calls heapStats(). Both callers go through one private vmHeap() in src/perf/memory_watch.ts, and the module's comments now name the new source.

Tests (test/memory-watch.test.ts):

  • The assertion at line 72 is unchanged and now passes.
  • New: "a worker VM's reading has a heap above 0 from its first minute" starts a real Worker that posts readNow().heap. It fails on the old source and passes on the new one.
  • The test for heapAcross() with no argument now also asserts the answer is not heap 0 MiB.

Checks run:

  • --rerun-each 10 on test/memory-watch.test.ts passes: 220 of 220.
  • Every test that imports memory_watch, boards/memory or efficiency/*, plus now-language and now-links, passes.
  • tsc and the gate are clean.
  • There is no test/boards-memory*.test.ts file on main, so the suites that import src/boards/memory.ts stood in for it.

What the sentinel's numbers do: outside is RSS less every fresh VM's heap. Where heapSize() was reading 0, the full heap now moves out of outside and into that VM's own heap figure.

  • A VM's first-hour heap baseline goes from 0 or too low to its real size, so heap growth is measured against a true start.
  • outside and its baseline drop by the same heap bytes, which stops heap growth before the first collection being blamed on "memory outside every JS heap".
  • The total RSS check that trips grown (twice the first hour, plus caches and slack) doesn't use the heap figure, so when it fires is unchanged. Only the part it names as having grown can change.

Not done: no change to how outside or grown are worked out, and no page or terminal change, so the commit says No door.

🤖 Generated with Claude Code

Closes #17210

commented

readNow().heap and heapAcross() with no argument now take the VM's JS heap from process.memoryUsage().heapUsed instead of bun:jsc heapSize(), so neither reads 0 any more.

Why it was 0: on Bun 1.4.4, heapSize() returns 0 until the VM's first garbage collection has measured the heap. bun -e prints 0, and after Bun.gc(true) it prints about 141 KB. A fresh worker, or a main VM between collections, therefore printed heap 0 MiB on its minute line, on /boards/memory and in a stall's heapAcross.

The new source: heapUsed is per VM. A worker that builds 300k objects reads about 25 MB while main reads its own 0.24 MB. Calling it costs about 12 µs, it doesn't depend on the heap's size, and it never calls heapStats(). Both callers go through one private vmHeap() in src/perf/memory_watch.ts, and the module's comments now name the new source.

Tests (test/memory-watch.test.ts):

  • The assertion at line 72 is unchanged and now passes.
  • New: "a worker VM's reading has a heap above 0 from its first minute" starts a real Worker that posts readNow().heap. It fails on the old source and passes on the new one.
  • The test for heapAcross() with no argument now also asserts the answer is not heap 0 MiB.

Checks run:

  • --rerun-each 10 on test/memory-watch.test.ts passes: 220 of 220.
  • Every test that imports memory_watch, boards/memory or efficiency/*, plus now-language and now-links, passes.
  • tsc and the gate are clean.
  • There is no test/boards-memory*.test.ts file on main, so the suites that import src/boards/memory.ts stood in for it.

What the sentinel's numbers do: outside is RSS less every fresh VM's heap. Where heapSize() was reading 0, the full heap now moves out of outside and into that VM's own heap figure.

  • A VM's first-hour heap baseline goes from 0 or too low to its real size, so heap growth is measured against a true start.
  • outside and its baseline drop by the same heap bytes, which stops heap growth before the first collection being blamed on "memory outside every JS heap".
  • The total RSS check that trips grown (twice the first hour, plus caches and slack) doesn't use the heap figure, so when it fires is unchanged. Only the part it names as having grown can change.

Not done: no change to how outside or grown are worked out, and no page or terminal change, so the commit says No door.

🤖 Generated with Claude Code

commented

Left #17210 open: 1 unticked item remain (Find out why heapSize() is 0 (for example, the heap was not yet measured, or this Bun counts it elsewhere) and make readNow().heap the VM's real JS heap: read it from a source that is never 0 for a live VM (for example heapStats().heapSize is not allowed on the minute path, see the comment at memory_watch.ts:34; process.memoryUsage().heapUsed is constant time), keeping the reading constant time.).

Sign in to join this conversation.

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

Keyboard shortcuts

Docs