mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Co-authored-by: 190n <7763597+190n@users.noreply.github.com> Co-authored-by: Ashcon Partovi <ashcon@partovi.net> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Co-authored-by: Don Isaac <donald.isaac@gmail.com> Co-authored-by: chloe caruso <git@paperclover.net>
41 lines
1.3 KiB
JavaScript
Generated
41 lines
1.3 KiB
JavaScript
Generated
// Create a worker with extremely large source code which completes instantly and the `eval` option
|
|
// set to true. Ensure that the Blob created to hold the source code is not kept in memory after the
|
|
// worker exits.
|
|
const { Worker } = require("node:worker_threads");
|
|
|
|
const eachSizeMiB = 100;
|
|
const iterations = 5;
|
|
|
|
function test() {
|
|
const code = " ".repeat(eachSizeMiB * 1024 * 1024);
|
|
return new Promise((resolve, reject) => {
|
|
const worker = new Worker(code, { eval: true });
|
|
worker.on("exit", () => resolve());
|
|
worker.on("error", e => reject(e));
|
|
});
|
|
}
|
|
|
|
async function reallyGC() {
|
|
for (let i = 0; i < 3; i++) {
|
|
await Bun.sleep(5);
|
|
Bun.gc(true);
|
|
}
|
|
}
|
|
|
|
// warmup
|
|
await test();
|
|
await reallyGC();
|
|
|
|
const before = process.memoryUsage.rss();
|
|
for (let i = 0; i < iterations; i++) {
|
|
await test();
|
|
await reallyGC();
|
|
}
|
|
const after = process.memoryUsage.rss();
|
|
// The bug is that the source code passed to `new Worker` would never be freed.
|
|
// If this bug is present, then the memory growth likely won't be much more than the total amount
|
|
// of source code, but it's impossible for the memory growth to be less than the source code size.
|
|
// On macOS before fixing this bug, deltaMiB was around 503.
|
|
const deltaMiB = (after - before) / 1024 / 1024;
|
|
if (deltaMiB >= eachSizeMiB * iterations) throw new Error(`leaked ${deltaMiB} MiB`);
|