vmThreads + jsdom: every finished test file stays in memory (V8 retained maps)
Describe the bug
With pool: "vmThreads" and jsdom, memory in our test workers grows with every test file (roughly 2 MB per file over ~560 files) and never comes back down. We'd been hiding it by recycling workers with vmMemoryLimit.
Digging in with heap snapshots, it looks like V8's "retained maps" optimisation keeps each finished file's jsdom window alive. jsdom creates DOM objects with the worker's own Object.create (from outside the sandbox), so V8 thinks their hidden classes are still in use, and holds on to them and the old window with them.
Running with --retain-maps-for-n-gc=0 fixes it for us: heap stays flat, and peak memory with 2 workers went from ~5.7 GB to ~2.8 GB with no change in run time. setupVmWorker already sets --no-compilation-cache for a similar reason, so it might make sense to set this one there too.
Reproduction
Run this in an empty folder with Node 24. It generates 300 small DOM tests and logs how many vm contexts are alive when each file starts:
set -e
mkdir vitest-vm-retention && cd vitest-vm-retention
npm init -y > /dev/null
npm i -D [email protected] [email protected] > /dev/null 2>&1
cat > vitest.config.mjs <<'EOF'
import { defineConfig } from "vitest/config";
export default defineConfig({
test: { pool: "vmThreads", environment: "jsdom", globals: true, setupFiles: ["./setup.js"] },
});
EOF
# Logs how many vm contexts (sandboxes) are alive when each test file starts.
cat > setup.js <<'EOF'
import { appendFileSync } from "node:fs";
import v8 from "node:v8";
appendFileSync("contexts.log", `${v8.getHeapStatistics().number_of_native_contexts}\n`);
EOF
mkdir tests
for i in $(seq -w 1 300); do
cat > "tests/t$i.test.js" <<'EOF'
it("dom", () => {
const els = [];
for (let i = 0; i < 50; i++) {
const el = document.createElement("div");
el.id = "i" + i;
document.body.appendChild(el);
els.push(el);
}
let s = 0;
for (let k = 0; k < 20000; k++) s += els[k % 50].id.length;
expect(s).toBeGreaterThan(0);
});
EOF
done
report() { echo "$1: $(tail -1 contexts.log) sandboxes alive at the last file (max $(sort -n contexts.log | tail -1))"; }
rm -f contexts.log; npx vitest run --maxWorkers=1 > /dev/null; report "default"
rm -f contexts.log; node --retain-maps-for-n-gc=0 node_modules/vitest/vitest.mjs run --maxWorkers=1 > /dev/null; report "--retain-maps-for-n-gc=0"Output:
default: 301 sandboxes alive at the last file (max 301)
--retain-maps-for-n-gc=0: 18 sandboxes alive at the last file (max 47)System Info
System:
OS: macOS 26.6.2
CPU: (14) arm64 Apple M4 Pro
Memory: 3.82 GB / 48.00 GB
Shell: 5.9.2 - /opt/homebrew/bin/zsh
Binaries:
Node: 24.19.0 - ~/.asdf/installs/nodejs/24.19.0/bin/node
npm: 11.17.0 - ~/.asdf/plugins/nodejs/shims/npm
bun: 1.4.2 - /opt/homebrew/bin/bun
Deno: 2.9.2 - /opt/homebrew/bin/deno
Browsers:
Chrome: 154.0.8037.58
Firefox: 156.0.1
Safari: 26.6.2
npmPackages:
vitest: ^5.0.1 => 5.0.1Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Source: vitest-dev/vitest