Skip to content

About

Repro: oxc-parser parseSync retains native memory in synchronous loops (deferred NAPI finalizers)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

oxc-parser parseSync native memory retention in synchronous loops

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.

Results

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.

Why this happens

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 in parse_with_return, whether or not .program is 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:

  1. A synchronous loop never lets finalizers run. The wrappers may be collected, but the native drops just queue up until the loop finally yields.
  2. global.gc() doesn't help — it queues more finalizers without ever running them (the gc mode above actually retains more than plain sync).

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.

Replicate

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 flat

Each mode prints RSS as it goes and a final summary line.

Workaround

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.

About

Repro: oxc-parser parseSync retains native memory in synchronous loops (deferred NAPI finalizers)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages