Skip to content

Windows: memory growth is JS-heap-side — heapUsed peaked 5729MB vs RSS 3001MB, so the RSS-only watchdog never warns #509

Closed

Description

@DiNaSoR

Follow-up to #314, which was closed as "tracked upstream — Bun runtime, not an opencodex leak." Per the closing comment's request for "a process.memoryUsage() sample (rss vs heapUsed) at high memory", here it is — and it points the other way.

The Windows Memory Growth doc states the attribution rule:

a rising responseState.totalBytes under a rising heap points at conversation retention (long store:false chains re-expanding each turn), whereas a flat responseState under a rising RSS points back at the native runtime

By that rule this looks JS-heap-side, which the same page says is "an opencodex bug you should report."

Environment

opencodex 2.7.39, reproduced then upgraded to 2.7.40
Bun (bundled) 1.3.14 (0d9b296a) — unchanged between 2.7.39 and 2.7.40
OS Windows 11 Pro 26200
streamMode auto; eagerRelay: {useEagerRelay: false, reason: "auto-known-bad"}
Client Codex CLI 0.144.5 through the proxy

heapUsed runs consistently above RSS

From GET /api/system/memory on 2.7.39 after 22.9 h uptime, using the watchdog's own 60-sample ring (~1 h at 60 s):

peak notes
heapUsed 5,729 MB JSC heapSize; objectCount ~948k
rss 3,001 MB min in the same window: 10 MB
heapTotal 57 MB stays tiny throughout

Point-in-time sample from that run: rss=929MB heapUsed=2398MB heapTotal=57MB.

heapUsed exceeding rss is the unusual part, and it's persistent, not a blip. During an idle stretch earlier that day:

17:35:34  rss=21    heapUsed=1572   heapTotal=52
17:40:34  rss=21    heapUsed=1572   heapTotal=52
17:45:34  rss=21    heapUsed=1572   heapTotal=52
17:50:34  rss=21    heapUsed=1572   heapTotal=52
17:55:34  rss=22    heapUsed=1572   heapTotal=52
18:00:34  rss=19    heapUsed=1572   heapTotal=52
18:05:34  rss=240   heapUsed=1789   heapTotal=52
18:20:34  rss=267   heapUsed=2036   heapTotal=52
18:25:34  rss=1702  heapUsed=3862   heapTotal=61

1.5 GB of JSC heap held flat for 25 minutes while the working set read 21 MB. Since heapUsed here is bun:jsc heapSize, some of that is GC-registered external memory rather than plain JS objects — you'll read that better than I can — but it is GC-visible retention, which is not the signature of an invisible native leak.

For scale, that day served 2,497 requests and the process reached ~16 GB RSS before I began restarting it.

The watchdog can't see this

src/server/memory-watchdog.ts compares RSS only:

if (s.rss >= warnThresholdBytes && (lastWarnAt === null || now() - lastWarnAt >= WARN_INTERVAL_MS)) {

With the 4 GiB default, lastWarnAt was still null after 22.9 h — RSS peaked at 3,001 MB and never crossed it, while heapUsed went to 5,729 MB. So on Windows the built-in warning is silent in exactly the case it exists for. Anything keyed to RSS inherits this (see the workaround below — I made the same mistake).

responseState isn't in 2.7.40

The docs tell users to read responseState from /api/system/memory to attribute growth, but it isn't in the latest stable release:

$ grep -rn "responseState" --include=*.ts src/       # 2.7.40
(no matches)

The endpoint returns pid, bunVersion, bunRevision, platform, uptimeSeconds, rss, heapUsed, heapTotal, jscHeap, streamMode, eagerRelay, watchdog — no responseState. So I can't run your documented triage step. Is it preview-only (2.7.41-preview.20260726)?

Indirect signal instead: ~/.opencodex/responses-state.json swung from 25 B → 19,970,862 B → 25 B within minutes while the heap climbed. Three orphaned temp files were also left behind from the high-memory window, 20.4 MB / 23.1 MB / 24.3 MB (responses-state.json.ocx.<pid>.<n>.tmp) — they survived proxy restarts and aren't cleaned up.

Abort-teardown counts (correlation only)

~/.opencodex/crash.log, summing the xN multipliers:

Date benign-abort-teardown requests
2026-07-20 149 338
2026-07-21 560 1,240
2026-07-22 475 1,186
2026-07-23 446 1,279
2026-07-24 472 1,177
2026-07-25 999 2,497

These come from cancelResponseBodyBestEffort(res) (src/lib/upstream-retry.ts:102) on the transient-5xx retry path — res.body?.cancel(). I initially thought aborts drove RSS independently of traffic, but the data doesn't support that: 07-25 was both the highest-abort and highest-traffic day, so the two are confounded here. Including the counts only as context in case the abort path is relevant to heap retention.

Workaround, for other Windows users

I understand from the docs that threshold auto-restart is deliberately not shipped; this is a user-side stopgap, not a request to change that.

A Task Scheduler job every 5 minutes polls /api/system/memory and runs ocx restart when heapUsed ≥ 5000 MB, or ≥ 2500 MB when idle (idleness = usage.jsonl mtime; ESTABLISHED connections on the proxy port are useless as a busy signal, since Codex holds keep-alive sockets open while idle).

The part worth passing on: my first version triggered on the Windows WorkingSetSize and was near-useless — it logged ok rss=21MB while the heap held 1,572 MB. Switching the trigger to heapUsed fixed it. Same reason the built-in watchdog stays quiet.

Happy to help

The proxy is now on 2.7.40 with a fresh process, so I can gather a clean long-uptime sample. I can also run streamMode: "eager-relay", a bun:jsc heap snapshot, or a preview build with responseState if that's more useful — just say which.


Unrelated packaging note: npm install -g @bitkyc08/[email protected] under npm's default allow-scripts policy silently blocks bun's postinstall, leaving node_modules/bun/bin/bun.exe as a 450-byte stub that errors with "Bun's postinstall script was not run". Fix is cd node_modules/bun && node install.js. May be worth a docs line, since it breaks the proxy on upgrade.

Activity

  1. added
    bug
    needs-info
    and removed
    bug
    on Jul 26, 2026
  2. Repository owner deleted a comment from github-actions on Jul 26, 2026
  3. Wibias commented on Jul 26, 2026

    @Wibias
    Contributor

    Thanks for the detailed samples — this is useful, but the conclusion that this is an opencodex JS-heap leak does not hold up against how these counters work on Bun/Windows.

    Verdict

    The Windows memory pain is real (same class as #314). The reading "heapUsed ≫ rss ⇒ JS-side opencodex bug" is not substantiated.

    Why the attribution rule does not apply that way

    1. Doctor / docs JS-vs-native split requires high RSS first. ocx doctor only treats growth as "likely an opencodex bug" when RSS is already above the watch threshold and the JS share of RSS is large. Under the threshold it reports usage as normal. Your RSS stayed under the 4 GiB warn line while a broken-looking heapUsed climbed — that is not the documented "JS leak" shape.

    2. heapUsed ≫ heapTotal (~5729 MB vs ~57 MB) is a known Bun accounting bug, not evidence of multi-GB live JS objects. Bun previously sourced heapUsed and heapTotal from mismatched JSC heaps, so heapUsed can exceed heapTotal by tens of times (oven-sh/bun#33368). Treat process.memoryUsage().heapUsed on the bundled 1.3.14 runtime as unreliable for leak triage.

    3. Idle rss≈21 MB with heapUsed≈1572 MB for ~25 minutes is almost certainly working-set / counter semantics, not 1.5 GB of resident retained objects. Real retention of that size would show up in the working set.

    4. API heapUsed is not bun:jsc heapSize. /api/system/memory exposes both: heapUsed from process.memoryUsage(), and jscHeap.heapSize separately. Equating them overstates "GC-visible retention."

    5. responses-state.json swinging 25 B → ~20 MB → 25 B while the "heap" kept climbing argues against conversation retention as the multi-GB driver. The docs' responseState rule needs a rising store under a rising heap.

    6. The ~16 GB RSS day still matches the 램 누수가 있어요 #314 / Bun Windows native pattern the troubleshooting page already describes.

    What is real in this report

    Claim Take
    Windows memory pain / huge RSS days Real — still #314 / upstream Bun class
    "opencodex JS leak" from heapUsed ≫ rss Not substantiated
    RSS-only watchdog stays quiet Real observability gap on Windows
    No responseState on 2.7.40 Real docs/release lag — field landed after 2.7.40, in 2.7.41
    Orphaned responses-state.json.ocx.*.tmp Plausible separate bug; worth filing/following separately
    npm/bun postinstall stub under default allow-scripts Real packaging footgun; unrelated to the leak claim

    Useful next samples (if you can)

    On 2.7.41+ (has responseState), a long-uptime /api/system/memory sample with:

    • rss, heapUsed, heapTotal
    • jscHeap (heapSize, heapCapacity, objectCount)
    • responseState (entries, totalBytes, largestBytes, age)
    • whether traffic was active or idle

    Until the bundled Bun carries fixed memoryUsage accounting, please do not treat heapUsed alone as the leak discriminator on Windows. RSS (and, when present, a flat vs rising responseState) remain the primary signals.

  4. github-actions commented on Jul 26, 2026

    @github-actions
    Contributor
  5. DiNaSoR commented on Jul 27, 2026

    @DiNaSoR
    Author

    Thanks — you're right about the core inference, and I can now show why with a controlled test rather than more field samples. But two of the specific rebuttals don't hold, and the recommended fallback to RSS is the one signal that fails hardest on Windows.

    Everything below is on the bundled Bun 1.3.14 (0d9b296a), allocating a known quantity of provably-reachable memory and comparing every available counter against it.

    Conceded: heapUsed is not a usable discriminator

    heapUsed is Heap::sizeAfterLastEdenCollection(), and on this runtime it simply stops tracking. 1,800 MB retained in a rooted array, with a forced full GC in the middle:

    after alloc 1200MB         heapUsed=   946 external=   946 rss=    74
    after Bun.gc(true)         heapUsed=   946 external=  1200 rss=    74
    after +600MB (no gc)       heapUsed=   946 external=  1200 rss=    77
    after +600MB then gc       heapUsed=   946 external=  1800 rss=    77
    retained: 1800 = 1800MB live
    

    heapUsed is pinned at 946 MB while live retention grows to 1,800 MB. So my "JS-heap-side leak" conclusion doesn't stand — I withdraw it.

    It also explains the idle plateau I reported better than my reading did: heapUsed sat at exactly 1572 MB for 25 minutes because an idle proxy allocates nothing, so no eden collection ran and the counter never refreshed. That's an artifact, as you suspected. (It's stale, not fabricated — 5729 MB means the heap did measure that after some collection — but as a live gauge it's unusable, which is the part that matters.)

    Your points 1 and 5 were fair too: the responses-state.json swing argues against retention rather than for it, and I shouldn't have leaned on it.

    Not correct: heapUsed vs jscHeap.heapSize

    /api/system/memory exposes both: heapUsed from process.memoryUsage(), and jscHeap.heapSize separately. Equating them overstates "GC-visible retention."

    Separately sourced in system-routes.ts, yes — but they return the same value, byte for byte, on every sample:

    heapUsed = 95129641   jscHeap.heapSize = 95129641   identical=True
    heapUsed = 96205533   jscHeap.heapSize = 96205533   identical=True
    

    Bun implements memoryUsage().heapUsed as the JSC heap size, so the two fields carry identical information on this build. It doesn't rescue my conclusion — both inherit the same eden-snapshot staleness — but the distinction can't be used to discount a reading.

    Not correct: the heapUsed ≫ heapTotal ratio as a phantom-number test

    oven-sh/bun#33368 is still open (not merged), and it fixes heapTotal's sourcing — its own description keeps heapUsed as sizeAfterLastEdenCollection(). So the ratio is meaningless, but that indicts heapTotal, not the heap figure.

    More directly: the same ratio appears with unambiguously live data. 1,500 MB of retained strings gives heapUsed=976 / heapTotal=1 — a 976× ratio, larger than the ~55× in the PR, on memory that is definitely reachable. A large ratio therefore can't distinguish real retention from a bad counter.

    The RSS fallback is the weakest of the three

    This is the part I'd push back on hardest. 1,500 MB of retained Uint8Array:

    counter reading
    allocated (known, reachable) 1500 MB
    process.memoryUsage().rss 82 MB
    process.memoryUsage().external 946 MB
    Win32 WorkingSetSize 82 MB
    Win32 PrivatePageCount 1725 MB (+1474 vs baseline)

    RSS moved 13 MB for 1,474 MB of committed retention. For external/ArrayBuffer-backed memory — precisely what a streaming proxy accumulates — RSS is not conservative, it's blind.

    And on working set specifically:

    Real retention of that size would show up in the working set.

    It doesn't. Same process, 1.5 GB live, after an explicit EmptyWorkingSet:

    2 after EmptyWorkingSet   ...  WS=     1  PrivCommit=  1731
    

    WorkingSet = 1 MB while 1,731 MB stays committed. Windows trims the working set freely; a process holding 1.5 GB of reachable data can read ~1 MB RSS. So rss ≈ 21 MB never established that retention wasn't there — it just wasn't evidence either way. The commit charge is what survives trimming.

    Concrete suggestion

    external tracked live retention exactly — 946 → 1200 → 1800 MB, matching what was actually held, and it comes from the same process.memoryUsage() call the endpoint already makes. /api/system/memory currently forwards rss, heapUsed, heapTotal but drops external and arrayBuffers.

    Exposing those two fields would give Windows users a discriminator that works on the bundled runtime today, without waiting for #33368. PrivatePageCount is the matching OS-level counter if you ever want a side-by-side.

    Repro, if useful:

    const keep = [];
    for (let i = 0; i < 1200; i++) { const b = new Uint8Array(1048576); b[0] = i & 255; keep.push(b); }
    console.log("alloc", process.memoryUsage());
    Bun.gc(true);
    console.log("gc   ", process.memoryUsage());  // heapUsed pinned; external correct; rss ~74MB

    Where this leaves the report

    I'm not re-asserting a JS-heap leak — that inference is withdrawn, and by the corrected reading I have no evidence yet that distinguishes app-level retention from the #314 native pattern. What I'd ask is that the attribution guidance not rest on RSS either, since the measurements above show it under-reports this allocation class by more than an order of magnitude.

    I've upgraded to 2.7.41 (thanks — responseState is there and populating) and restarted the clock. I'll follow up with a long-uptime sample carrying rss, heapUsed, heapTotal, jscHeap, responseState, PrivatePageCount, and active-vs-idle state, so the next round is measured against counters we both trust. Happy to have this retitled or narrowed to the observability points in the meantime.

  6. added a commit that references this issue on Jul 27, 2026
    261abb7
  7. lidge-jun commented on Jul 27, 2026

    @lidge-jun
    Owner

    Fixed on dev via PR #554: https://github.com/lidge-jun/opencodex/pull/554\n\nWhat changed:\n- /api/system/memory now exposes external, arrayBuffers, observedBytes, and observedMetric.\n- The watchdog and doctor now threshold against observed memory, defined as max(rss, external, arrayBuffers), so Windows/Bun external retention is no longer hidden behind RSS-only wording.\n- The GUI Memory card and docs/SOT now show the observed metric and include runtime counter caveats.\n\nVerification:\n- Local: memory-watchdog + doctor tests, GUI memory card tests, typecheck, GUI lint/build, privacy scan, diff check.\n- CI: Cross-platform CI ubuntu/macos/windows, npm-global matrix, React Doctor, enforce-target, label, and CodeRabbit all passed before squash merge.\n\nClosing this issue as the watchdog/observability gap is fixed. If a later long-running Windows sample shows a separate retention root cause, please open/reopen a focused issue with the new observed-memory counters.

  8. added a commit that references this issue on Sep 17, 2026
    cccf93d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-info

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions