Skip to content

Instantly share code, notes, and snippets.

@alexeychikk
Created December 2, 2020 10:20
Show Gist options
  • Save alexeychikk/36fda660bbbaca1dc7ce0c14042e8195 to your computer and use it in GitHub Desktop.
Save alexeychikk/36fda660bbbaca1dc7ce0c14042e8195 to your computer and use it in GitHub Desktop.
memoryLeakTest.js
// RUN WITH
// node --expose_gc memoryLeakTest.js
class A {
async b() {
return { foo: "bar".repeat(1000) };
}
async c() {
return Promise.resolve({ foo: "bar".repeat(1000) });
}
}
const a = new A();
(async () => {
for (let i = 0; i < 10000000; i++) {
await a.b();
}
global.gc();
await sleep();
console.log("GC\nb() results:\n", process.memoryUsage());
for (let i = 0; i < 10000000; i++) {
await a.c();
}
global.gc();
await sleep();
console.log("GC\nc() results:\n", process.memoryUsage());
for (let i = 0; i < 10000000; i++) {
await a.b();
}
global.gc();
await sleep();
console.log("GC\nb() results:\n", process.memoryUsage());
})();
function sleep() {
return new Promise((resolve) => setTimeout(resolve, 0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment