#578

Severe memory leak in --watch mode

OpenRyanGarberopened 16d ago

Summary

On macOS, electrobun dev --watch (the hutch-engine process) leaks memory. It grows by roughly 50–100 MB for each file-change event and never releases it. After about an hour of normal editing it reached ~12.7 GB physical footprint. The app itself (the bun main process and its WebKit view) stays at about 1 GB. A built app isn't affected.

Environment

  • Electrobun 2.0.2 (~/.hutch/npm/electrobun/2.0.2/macos-arm64/bin/hutch-engine)
  • macOS 27.2 (26B5091g), arm64
  • Node 24.14.0, pnpm 12.11.2 (workspace monorepo), Bun 1.4.2
  • build.mainProcess: "bun", bundleCEF: false (system WebKit)
  • Run as electrobun dev --watch, with build.watchIgnore: ["dist/**", "native/**", "src/client-runtime.ts"]

Observations

From vmmap --summary <hutch-engine pid> after ~66 min of uptime:

Physical footprint:         11.7G
Physical footprint (peak):  11.7G
REGION TYPE   VIRTUAL  RESIDENT  DIRTY  SWAPPED ...
Untagged        11.7G     16.2M  16.2M    11.7G ...
  • Nearly all of the footprint is in the Untagged region (anonymous mmap, not malloc). It is compressed or swapped out, so ps RSS shows only ~70 MB, while Activity Monitor shows the full amount. Activity Monitor counts it under whatever terminal launched it (Cursor in my case).

  • Only ~11 file descriptors are open, so it isn't leaking file handles or watchers.

  • Each file change grows it, whether or not the app relaunches. I tracked the Untagged swapped size while touching files:

    ActionUntagged
    baseline12.2G
    touch packages/gui/src/main.tsx (app not relaunched)12.3G
    touch packages/core/src/.../env.ts (app not relaunched)12.4G
    touch README.md (repo root, outside the app dir)12.5G
    touch packages/server/.../ApiRouter.ts12.5G
  • With no edits it still creeps up about 0.1 GB/min (12.6G → 12.7G over 90 s). This is probably from background writes elsewhere in the repo (editor, tsc, Vite caches), which also suggests the watcher covers more than the app directory.

Repro

  1. Run electrobun dev --watch on macOS in a project inside a larger workspace.
  2. Get the hutch-engine PID and run vmmap --summary <pid> | grep -E "Physical footprint|^Untagged".
  3. touch a few files in the project, and some outside the app directory, a few times.
  4. Run vmmap again: the footprint and Untagged swapped size go up with each change and never come back down.

Expected

The watcher's memory should stay roughly constant across rebuilds and change events.

Notes

  • I haven't tested the 2.0.3-beta.* releases.
  • Possibly related: the watcher seems to react to changes outside the app directory, such as the repo-root README.md. If that's intended, it would help to document the default watch roots alongside build.watch / build.watchIgnore.

Activity

0

No activity yet.