Fix memory leak when printing any error's source code. (#12831)

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
dave caruso
2024-07-26 14:14:16 -07:00
committed by GitHub
parent 7aa05ec542
commit bf8b6922bb
6 changed files with 72 additions and 20 deletions

View File

@@ -0,0 +1,23 @@
import { test, expect } from "bun:test";
const perBatch = 2000;
const repeat = 50;
test("Printing errors does not leak", () => {
function batch() {
for (let i = 0; i < perBatch; i++) {
Bun.inspect(new Error("leak"));
}
Bun.gc(true);
}
batch();
const baseline = Math.floor(process.memoryUsage.rss() / 1024);
for (let i = 0; i < repeat; i++) {
batch();
}
const after = Math.floor(process.memoryUsage.rss() / 1024);
const diff = ((after - baseline) / 1024) | 0;
console.log(`RSS increased by ${diff} MB`);
expect(diff, `RSS grew by ${diff} MB after ${perBatch * repeat} iterations`).toBeLessThan(10);
}, 10_000);