Parsing many files with oxc-parser's parseSync in a synchronous loop retains the
native (Rust-side) memory of every parse until the event loop turns — the process RSS grows
by roughly the size of each file's serialized AST (often 10–20× the source size) per call,
reaching many GiB over tens of thousands of files. Forcing garbage collection does not
reclaim the memory. Yielding to the event loop does.
Observed on macOS arm64 and Linux x64, Node v22, oxc-parser 0.72.2 through 0.139.0.
50,000 parseSync calls over sixteen ~5 KB synthetic TypeScript modules
(macOS arm64, Node v22.13.1, oxc-parser 0.139.0):
| Mode | What it does | RSS |
|---|---|---|
sync |
tight synchronous loop | grows to 7.3 GiB (one sporadic partial reclamation late in the run) |
gc |
same, plus global.gc() every 1,000 parses |
grows monotonically to 10.3 GiB |
yield |
same, plus await setImmediate() every 1,000 parses |
flat at ~660 MiB |
The live data held by the loop itself is a single integer. All three modes do identical parse work.
parseSync returns a ParseResult, which is a NAPI class (#[napi] on a struct in
napi/parser/src/types.rs), not a plain object. The JS side receives a thin wrapper with
lazy getters; the Rust struct stays on the native heap and holds:
program_and_fixes: the entire AST serialized as a JSON string (built eagerly inparse_with_return, whether or not.programis ever read),- the module record, comments, and errors.
That native memory is only released by the NAPI finalizer that runs when the JS wrapper is garbage-collected — and per the Node-API docs, regular (non-basic) finalizers are scheduled to run after the GC cycle completes, on the event loop. Two consequences:
- A synchronous loop never lets finalizers run. The wrappers may be collected, but the native drops just queue up until the loop finally yields.
global.gc()doesn't help — it queues more finalizers without ever running them (thegcmode above actually retains more than plainsync).
V8 also has no idea this memory exists (it is not reported via
napi_adjust_external_memory), so nothing about heap pressure changes GC or scheduling
behavior.
The same loop implemented in pure Rust against oxc_parser + ESTree serialization runs
flat at ~110 MiB, so the parser itself cleans up correctly — the retention is specific to
the NAPI boundary.
npm install
npm run repro:sync # baseline: RSS grows unboundedly
npm run repro:gc # forced GC every 1k parses: still grows
npm run repro:yield # setImmediate every 1k parses: RSS stays flatEach mode prints RSS as it goes and a final summary line.
If you parse many files, yield to the event loop periodically so the deferred finalizers can run:
import { setImmediate as setImmediateP } from "node:timers/promises"
for (const [i, file] of files.entries()) {
parseSync(file.name, file.source)
if (i % 1000 === 0) await setImmediateP()
}In a real-world ~79k-file monorepo import scan this took peak RSS from ~7.3 GiB (OOM-killing an 8 GiB CI container) to well under 4 GiB.