Skip to content

WebAssembly.compile retains ~10 MB per wasm compiler thread; RSS grows with core count and is never released #41438

Closed

Description

@bompus

What version of Bun is running?

1.4.2+744846f84 (also reproduced on 1.4.3-canary.1+76e9dcc6a)

What platform is your computer?

Microsoft Windows NT 10.0.26200.0 x64, AMD Ryzen 9 9950X3D (32 logical cores), 128 GB RAM

What steps can reproduce the bug?

WebAssembly.compile of a mid-sized module retains far more process memory than the module needs, and the memory is not returned when the WebAssembly.Module is dropped. Compiling the same module repeatedly (discarding each result) grows RSS until it plateaus at roughly numberOfWasmCompilerThreads × ~10 MB, so the retained amount scales with core count, not with the number of live modules.

mkdir wasm-compile-rss && cd wasm-compile-rss
bun add tree-sitter-wasms@0.1.11
bun repro.mjs

repro.mjs:

// Compile the same wasm module N times, discarding each; print RSS growth.
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);
const bytes = readFileSync(require.resolve("tree-sitter-wasms/out/tree-sitter-cpp.wasm"));
const gc = globalThis.gc ?? (globalThis.Bun ? () => Bun.gc(true) : () => {});
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const mb = (b) => Math.round(b / 1048576);
const runtime = process.versions.bun ? `bun ${process.versions.bun}` : `node ${process.versions.node}`;

const base = process.memoryUsage().rss;
console.log(`${runtime}, ${mb(bytes.length)} MB module, base rss ${mb(base)} MB`);
for (let i = 1; i <= 30; i++) {
  let mod = await WebAssembly.compile(bytes);
  mod = null;
  if (i % 5 === 0) {
    gc();
    await sleep(200);
    console.log(`after ${i} compiles: +${mb(process.memoryUsage().rss - base)} MB rss`);
  }
}

The module is tree-sitter-cpp.wasm from the tree-sitter-wasms package: 4.4 MB, of which 1.1 MB is the code section and 3.3 MB data.

What is the expected behavior?

RSS after the loop is close to the size of the compiled module and does not depend on the number of compiler threads. Node 24.20.0 LTS (node --expose-gc repro.mjs):

node 24.20.0, 4 MB module, base rss 41 MB
after 5 compiles: +6 MB rss
after 10 compiles: +6 MB rss
after 15 compiles: +6 MB rss
after 20 compiles: +6 MB rss
after 25 compiles: +6 MB rss
after 30 compiles: +6 MB rss

Node 26.8.1 Current ends at +17 MB.

What do you see instead?

bun 1.4.2, 4 MB module, base rss 53 MB
after 5 compiles: +170 MB rss
after 10 compiles: +266 MB rss
after 15 compiles: +277 MB rss
after 20 compiles: +311 MB rss
after 25 compiles: +322 MB rss
after 30 compiles: +333 MB rss

Same host, same scripts, four runtimes:

Measurement Node 24.20.0 (LTS) Node 26.8.1 (Current) Bun 1.4.2 Bun 1.4.3-canary.1
RSS growth, 30× compile + discard tree-sitter-cpp.wasm (4.4 MB) +6 MB +17 MB +333 MB +348 MB
RSS growth, one compile of the same module +5 MB +5 MB +30 MB +33 MB
RSS growth, 30× compile + discard webp_codec.wasm (0.55 MB, from playwright-core) +2 MB +2 MB +29 MB +36 MB
Retained per worker_threads Worker, 8 Workers each loading 8 web-tree-sitter grammars and parsing 20 files 9.5 MB 10.2 MB 29.5 MB 39.7 MB

None of the memory comes back after the modules are unreachable: keeping three compiled modules alive costs +143 MB, and dropping them plus Bun.gc(true) leaves +143 MB. process.memoryUsage().heapUsed and external stay near zero throughout, so nothing on the JS side sees the pressure.

Additional information

The retention is tied to the concurrent wasm compiler thread pool, not to the compiled code itself:

Setting (same loop, 30 compiles) RSS growth
default +336 MB
BUN_JSC_useConcurrentJIT=0 +21 MB
BUN_JSC_numberOfWasmCompilerThreads=1 +19 MB
BUN_JSC_useJIT=0 +17 MB
BUN_JSC_useBBQJIT=0 no change
BUN_JSC_useOMGJIT=0 no change
BUN_JSC_useWasmIPInt=0 no change
BUN_JSC_useWasmFastMemory=0 no change

A single compile of this module retains +30 MB at the default and +10 MB with any of the three settings that serialize compilation. The plateau of ~340 MB on a 32-thread machine is consistent with each of the ~31 compiler threads (numberOfWasmCompilerThreads defaults to computeNumberOfWorkerThreads(INT32_MAX, 2) - 1) holding on to ~10 MB after its first large compile, which then never gets decommitted. A machine with fewer cores would see a proportionally smaller number.

Where this bites: web-tree-sitter users load one grammar per language per worker_threads Worker. In an 8-worker indexer (the codegraph CLI) on this host, bun … index peaks at 1,100 MB against 645 MB for the same script on Node 24; with BUN_JSC_useConcurrentJIT=0 the 8-Worker row in the table above drops to 4.3 MB per Worker.

Only measured on Windows x64; not tested on Linux or macOS.

Activity

  1. robobun commented on Sep 5, 2026

    @robobun
    Collaborator

    Reproduced on Linux x64 (bun 1.4.1, 16 cores): the 30-compile loop ends at +150 MB RSS, and it all comes back at the 10 s mark, when the idle wasm compiler threads exit. Only the owning thread can collect its mimalloc heap, and an idle JSC worker thread never did.
    Fix in progress: #41449 (pins oven-sh/WebKit#567, which makes idle JSC worker threads release their heap after 100 ms). With it the same loop ends at +12 MB.

  2. robobun commented on Sep 5, 2026

    @robobun
    Collaborator

    The Bun side is #41452: it pins the preview build of oven-sh/WebKit#567 and adds a test that compiles a control-flow-heavy module 20 times with 8 compiler threads and polls RSS. On a 16-core Linux box the retained memory goes from +44 to +50 MiB (never released within 3 s) to +1 to +4 MiB within 1 s.

  3. added
    bugSomething isn't working
    wasmSomething that related to WASM or WASI support
    on Sep 20, 2026
  4. added a commit that references this issue on Oct 4, 2026
    1878660
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

    bugSomething isn't workingmemory usagewasmSomething that related to WASM or WASI support

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions