docs(plans): why host-service uses so much memory on a cloud box

Merged
SA
7d ago
+205-0

What changed

Report only, no product code: plans/20261117-host-service-memory.md.

Finding

On Linux, defaultWatchBackend() picks chokidar, not @parcel/watcher. chokidar opens one fs.watch per file and per directory, which costs about 6.5 KB of RSS per watched entry. host-service watches with useDefaultIgnores: false, and it prunes only the gitignored dirs that exist when the watch attaches. So a node_modules / .next / dist created afterwards (a bun install on a fresh clone, next dev) is watched file by file until the workspace is unwatched. This repo's node_modules is 248k entries, about 1.6 GB, which matches the 1.4 GB seen on a box.

Measured on a second instance of the released bundle (own port, state, HOME, API URL pointed at a closed port):

  • Idle: 163 MB RSS, 36.5 MB heap.
  • git:watch on /workspace (23,227 watches, 16,517 of them files; node_modules correctly pruned): 252 MB steady, 424 MB peak during the crawl. The cost is per workspace, not per client.
  • 60k-file node_modules created after attach: +438 MB, 66,065 watches. The same tree present before attach: 4 watches, no growth. 132k entries after attach: +834 MB. Deleting the tree gave the memory back except about 88 MB of glibc fragmentation.
  • Terminals are bounded: about 4.1 MB per session in host-service (2 MiB catch-up ring + 1,000-line headless xterm) and 3.8 MB in pty-daemon.
  • Chat adds about 1 MB to host-service; the adapter and claude are separate processes (about 370 MB per session).
  • The live host was inspected read-only (/proc, its own vitals log): flat at 257 to 272 MB over 20 minutes with no watch client.

Expected saving of the top-ranked follow-up (prune gitignored dirs that appear after attach): up to about 1.5 GB on a box that installs or builds after the desktop attaches. The report ranks the other follow-ups.

Checks

  • bun run lint:fix: ran, exit 0, no fixes.
  • bun run typecheck: ran, 42/42 tasks passed.
  • bun test: not run, because no package code changed.
  • Image tests and Docker: not run. Not relevant to a doc-only change, and Docker was not started.
  • A real bun install after attach was not run, so as not to write GBs to this workspace's root disk. That case is scaled linearly from the synthetic 60k and 132k-entry trees.
  • The 8 GB / 4 vCPU box shape was not reproduced. This box has 16 GB.

Summary by cubic

Adds a report measuring host-service memory on a cloud box and pinning the 1.4 GB growth to the workspace file watcher.

On Linux the watcher uses chokidar, which opens one fs.watch per file and per directory (~6.5 KB RSS per entry). Gitignored dirs are pruned only when they exist at attach time, so a node_modules or .next created afterwards (a bun install, next dev) is watched file by file. That's about 1.6 GB for this repo's node_modules, matching the report.

The report also breaks down fixed cost, terminal, and chat memory, and ranks follow-up fixes. Top-ranked is pruning gitignored dirs that appear after attach, saving up to ~1.5 GB on boxes that install or build after the desktop attaches.

Summary by CodeRabbit

  • Documentation
    • Added a report measuring host-service memory usage across a cloud workspace and a test instance.
    • The report compares memory costs across workspace watching, terminals, chat processes, and runtime overhead, and outlines measurement limitations.
    • Ranked potential memory-saving changes with estimated impact and risks.