Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions test/bundler/bun-build-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,7 @@ test.skipIf(!isDebug && !isASAN)(
const dir = tempDirWithFiles("bun-build-inline-sourcemap-leak", {
"entry.ts": "export const a = 1;\n/* " + Buffer.alloc(30 * 1024 * 1024, "x").toString() + " */\n",
"run.ts": `
const rss = process.platform === "darwin" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss;
const entry = process.argv[2];
async function build() {
const res = await Bun.build({ entrypoints: [entry], sourcemap: "inline" });
Expand All @@ -1344,10 +1345,10 @@ test.skipIf(!isDebug && !isASAN)(
}
for (let i = 0; i < 2; i++) await build();
await settle();
const before = process.memoryUsage.rss();
const before = rss();
for (let i = 0; i < 8; i++) await build();
await settle();
const after = process.memoryUsage.rss();
const after = rss();
console.log(JSON.stringify({ before, after, growth: after - before }));
`,
});
Expand Down Expand Up @@ -1412,6 +1413,7 @@ test.skip("Bun.build NumberRenamer does not leak intermediate NumberScope.name_c
const dir = tempDirWithFiles("bun-build-number-renamer-leak", {
"entry.js": entry,
"run.ts": `
const rss = process.platform === "darwin" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss;
const entry = process.argv[2];
async function build() {
// No identifier minification → NumberRenamer path (not MinifyRenamer).
Expand All @@ -1425,10 +1427,10 @@ test.skip("Bun.build NumberRenamer does not leak intermediate NumberScope.name_c
// steady-state so the measured window only reflects per-build retention.
for (let i = 0; i < 2; i++) await build();
await settle();
const before = process.memoryUsage.rss();
const before = rss();
for (let i = 0; i < 20; i++) await build();
await settle();
const after = process.memoryUsage.rss();
const after = rss();
console.log(JSON.stringify({ before, after, growth: after - before }));
`,
});
Expand Down
4 changes: 2 additions & 2 deletions test/cli/hot/hot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ it(
bundleIn,
`// ${long_comment}
//
console.error("RSS: %s", process.memoryUsage().rss);
console.error("RSS: %s", process.platform === "darwin" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint() : process.memoryUsage.rss());
throw new Error('0');`,
);
await using bundler = spawn({
Expand Down Expand Up @@ -745,7 +745,7 @@ throw new Error('0');`,
writeHotFileAtomicSync(
bundleIn,
`// ${long_comment}
console.error("RSS: %s", process.memoryUsage().rss);
console.error("RSS: %s", process.platform === "darwin" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint() : process.memoryUsage.rss());
//
${Buffer.alloc(counter * 2, " ").toString()}throw new Error(${counter});`,
);
Expand Down
8 changes: 6 additions & 2 deletions test/cli/run/cjs-fixture-leak-small.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions test/cli/run/esm-bug-leak-fixture.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions test/cli/run/esm-fixture-leak-small.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions test/cli/run/require-cache-bug-leak-fixture.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 16 additions & 12 deletions test/cli/run/require-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe.concurrent("require.cache", () => {
"require-cache-bug-leak-fixture.js": `
const path = require.resolve("./index.js");
const gc = global.gc || globalThis?.Bun?.gc || (() => {});
const rss = process.platform === "darwin" && typeof Bun !== "undefined" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss;
const noChildren = module.children = { indexOf() { return 0; } }; // disable children tracking
function bust() {
const mod = require.cache[path];
Expand All @@ -79,14 +80,14 @@ describe.concurrent("require.cache", () => {
bust();
}
gc(true);
const baseline = process.memoryUsage.rss();
const baseline = rss();
for (let i = 0; i < 500; i++) {
require(path);
bust(path);
}
gc(true);
const rss = process.memoryUsage.rss();
const diff = rss - baseline;
const after = rss();
const diff = after - baseline;
console.log("RSS diff", (diff / 1024 / 1024) | 0, "MB");
console.log("RSS", (diff / 1024 / 1024) | 0, "MB");
if (diff > ${isASAN ? 400 : 100} * 1024 * 1024) {
Expand Down Expand Up @@ -122,6 +123,7 @@ describe.concurrent("require.cache", () => {
"require-cache-bug-leak-fixture.js": `
const path = require.resolve("./index.js");
const gc = global.gc || globalThis?.Bun?.gc || (() => {});
const rss = process.platform === "darwin" && typeof Bun !== "undefined" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss;
function bust() {
delete require.cache[path];
}
Expand All @@ -131,14 +133,14 @@ describe.concurrent("require.cache", () => {
bust();
}
gc(true);
const baseline = process.memoryUsage.rss();
const baseline = rss();
for (let i = 0; i < 400; i++) {
await import(path);
bust(path);
}
gc(true);
const rss = process.memoryUsage.rss();
const diff = rss - baseline;
const after = rss();
const diff = after - baseline;
console.log("RSS diff", (diff / 1024 / 1024) | 0, "MB");
console.log("RSS", (diff / 1024 / 1024) | 0, "MB");
if (diff > ${isASAN ? 320 : 64} * 1024 * 1024) {
Expand Down Expand Up @@ -171,6 +173,7 @@ describe.concurrent("require.cache", () => {
"require-cache-bug-leak-fixture.js": `
const path = require.resolve("./index.js");
const gc = global.gc || globalThis?.Bun?.gc || (() => {});
const rss = process.platform === "darwin" && typeof Bun !== "undefined" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss;
function bust() {
delete require.cache[path];
}
Expand All @@ -180,14 +183,14 @@ describe.concurrent("require.cache", () => {
bust();
}
gc(true);
const baseline = process.memoryUsage.rss();
const baseline = rss();
for (let i = 0; i < 250; i++) {
await import(path);
bust(path);
}
gc(true);
const rss = process.memoryUsage.rss();
const diff = rss - baseline;
const after = rss();
const diff = after - baseline;
console.log("RSS diff", (diff / 1024 / 1024) | 0, "MB");
console.log("RSS", (diff / 1024 / 1024) | 0, "MB");
if (diff > ${isASAN ? 320 : 64} * 1024 * 1024) {
Expand Down Expand Up @@ -231,6 +234,7 @@ describe.concurrent("require.cache", () => {
"require-cache-bug-leak-fixture.js": `
const path = require.resolve("./index.js");
const gc = global.gc || globalThis?.Bun?.gc || (() => {});
const rss = process.platform === "darwin" && typeof Bun !== "undefined" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss;
function bust() {
const mod = require.cache[path];
if (mod) {
Expand All @@ -245,14 +249,14 @@ describe.concurrent("require.cache", () => {
bust();
}
gc(true);
const baseline = process.memoryUsage.rss();
const baseline = rss();
for (let i = 0; i < 400; i++) {
require(path);
bust(path);
}
gc(true);
const rss = process.memoryUsage.rss();
const diff = rss - baseline;
const after = rss();
const diff = after - baseline;
console.log("RSS diff", (diff / 1024 / 1024) | 0, "MB");
console.log("RSS", (diff / 1024 / 1024) | 0, "MB");
if (diff > ${isASAN ? 320 : 64} * 1024 * 1024) {
Expand Down
5 changes: 5 additions & 0 deletions test/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2221,3 +2221,8 @@ export function compileFixture(sourcePath: string, options: { flags?: string[] }
compiledFixtures.set(cacheKey, outPath);
return outPath;
}

export const rss: () => number =
process.platform === "darwin" && typeof Bun.unsafe.memoryFootprint === "function"
? (Bun.unsafe.memoryFootprint as () => number)
: process.memoryUsage.rss;
9 changes: 7 additions & 2 deletions test/js/bun/archive-extract-leak-repro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { mkdtempSync, rmSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";

const rss =
process.platform === "darwin" && typeof Bun.unsafe.memoryFootprint === "function"
? Bun.unsafe.memoryFootprint
: process.memoryUsage.rss;

const dir = mkdtempSync(join(tmpdir(), "archive-leak-"));

const files = {
Expand All @@ -26,8 +31,8 @@ for (let round = 0; round < 20; round++) {
}

Bun.gc(true);
const rss = process.memoryUsage.rss();
console.log(`Round ${round + 1}: RSS = ${formatMB(rss)}`);
const rssNow = rss();
console.log(`Round ${round + 1}: RSS = ${formatMB(rssNow)}`);
}

rmSync(dir, { recursive: true });
5 changes: 3 additions & 2 deletions test/js/bun/archive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ describe("Bun.Archive", () => {
// iterations would leak ~190MB. A 64MB threshold comfortably separates
// "fixed" (stable RSS) from "leaking".
const code = /* ts */ `
const rss = process.platform === "darwin" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss;
function ustarHeader(name, size) {
const h = Buffer.alloc(512);
h.write(name, 0, 100, "utf8");
Expand Down Expand Up @@ -691,10 +692,10 @@ describe("Bun.Archive", () => {

for (let i = 0; i < 100; i++) await once();
Bun.gc(true);
const before = process.memoryUsage.rss();
const before = rss();
for (let i = 0; i < 1500; i++) await once();
Bun.gc(true);
const growthMB = (process.memoryUsage.rss() - before) / 1024 / 1024;
const growthMB = (rss() - before) / 1024 / 1024;
console.log("RSS growth: " + growthMB.toFixed(1) + " MB");
if (growthMB > 64) throw new Error("leaked " + growthMB.toFixed(1) + " MB");
`;
Expand Down
5 changes: 3 additions & 2 deletions test/js/bun/css/small-list-grow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ test("CSS bundler doesn't over-allocate SmallList when growing past the first he
const lastSel = `.r${numRules - 1}-s${selectorsPerRule - 1}`;

const fixture = /* js */ `
const baseline = process.memoryUsage.rss();
const rss = process.platform === "darwin" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss;
const baseline = rss();
const result = await Bun.build({
entrypoints: [${JSON.stringify(path.join(String(dir), "wide.css"))}],
});
Expand All @@ -46,7 +47,7 @@ test("CSS bundler doesn't over-allocate SmallList when growing past the first he
}
const out = await result.outputs[0].text();
Bun.gc(true);
const after = process.memoryUsage.rss();
const after = rss();
console.log(JSON.stringify({
deltaMB: (after - baseline) / 1024 / 1024,
hasFirst: out.includes(${JSON.stringify(firstSel)}),
Expand Down
5 changes: 3 additions & 2 deletions test/js/bun/css/token-list-backtracking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ function spawnMinify(css: string) {
bunExe(),
"-e",
`const c = require("bun:internal-for-testing").cssInternals;
const rss = process.platform === "darwin" && typeof Bun.unsafe.memoryFootprint === "function" ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss;
const css = ${JSON.stringify(css)};
const rssBefore = process.memoryUsage.rss();
const rssBefore = rss();
let threw = false;
try { c.minifyTest(css, ""); } catch { threw = true; }
const deltaMB = (process.memoryUsage.rss() - rssBefore) / 1024 / 1024;
const deltaMB = (rss() - rssBefore) / 1024 / 1024;
if (deltaMB > 256) throw new Error("memory grew by " + deltaMB.toFixed(0) + "MB");
console.log("done threw=" + threw);`,
],
Expand Down
Loading
Loading