Memory management issue involving ShadowRealm and heap snapshots requires deep Node.js internals knowledge.
The issue involves an Out of Memory (OOM) error when creating many ShadowRealms and taking heap snapshots in Node.js. The interaction between ShadowRealm creation and heap snapshotting appears to prevent effective garbage collection. This requires understanding of Node.js memory management and ShadowRealm internals.
24.13.1, 25.8.1
Linux KContainer 5.10.0-12-amd64 #1 SMP Debian 5.10.103-1 (2022-03-07) x86_64 x86_64 x86_64 GNU/Linux
No response
The below PoC can reproduce the result
import { writeHeapSnapshot } from 'v8';
// This script simulates creating many ShadowRealms and taking heap snapshots
async function main() {
if (typeof ShadowRealm === 'undefined') {
console.error('ShadowRealm is not available in this Node build');
process.exit(1);
}
const realms = [];
for (let i = 0; i < 20000; i++) {
const realm = new ShadowRealm();
realms.push(realm);
// Take heap snapshots periodically
if (i % 500 === 0) {
writeHeapSnapshot(`heap-${i}.heapsnapshot`);
console.log('Created ShadowRealm and heap snapshot at iteration', i);
}
}
console.log('Total ShadowRealms created:', realms.length);
}
main();
No special condition
Even though many objects are allocated, the runtime should handle memory pressure gracefully. The interaction between ShadowRealm creation and heap snapshotting appears to prevent effective garbage collection.
root@KContainer:~/49572# node --experimental-shadow-realm --max-old-space-size=1024 test-heapdump-shadow-realm.mjs
Created ShadowRealm and heap snapshot at iteration 0
Created ShadowRealm and heap snapshot at iteration 500
<--- Last few GCs --->
[6100:0x7db2000] 40950 ms: Mark-Compact 995.6 (1033.9) -> 987.2 (1052.6) MB, pooled: 0 MB, 438.54 / 0.00 ms (average mu = 0.552, current mu = 0.272) allocation failure; scavenge might not succeed
[6100:0x7db2000] 41755 ms: Mark-Compact (reduce) 1002.5 (1054.6) -> 996.6 (1028.6) MB, pooled: 0 MB, 109.72 / 0.01 ms (+ 572.7 ms in 115 steps since start of marking, biggest s
Claim this issue to let others know you're working on it. You'll earn 25 points when you complete it!