Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@subzey
Created April 13, 2022 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save subzey/1522e6d926b24a10360ad6ec85ccfdac to your computer and use it in GitHub Desktop.
Save subzey/1522e6d926b24a10360ad6ec85ccfdac to your computer and use it in GitHub Desktop.
"<symbol> in Promise" leak
<!doctype html>
<script>
let singletonPromise;
function makeSomeRequest() {
if (!singletonPromise) {
singletonPromise = Promise.resolve('(Response from some request that only should be made once)')
}
return singletonPromise;
}
class LeakyClass {
constructor() {
// Makes the memory leak more apparent
this._heavyObject = new Array(8 * 1024 * 1024).fill(0);
}
async doAsyncStuff() {
console.log(await makeSomeRequest());
}
}
new LeakyClass().doAsyncStuff();
</script>
<p>Try refreshing this page with Chrome DevTools open. The <code>LeakyClass</code> instance would not be garbage collected. The heap size would be around 32 MB.</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment