Skip to content

Instantly share code, notes, and snippets.

@jazeee
Created March 17, 2018 00:44
Show Gist options
  • Save jazeee/2c27abf61f0bca18f4c1c6de4ec7fbba to your computer and use it in GitHub Desktop.
Save jazeee/2c27abf61f0bca18f4c1c6de4ec7fbba to your computer and use it in GitHub Desktop.
Memory Leak Example
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
</head>
<body>
<button id="test">Click me!</button>
<button id="replace-button">Replace Click me!</button>
</body>
<script>
{
let clickMeButton = document.getElementById("test");
clickMeButton.onclick = () => {
console.log("Clicked");
let {longString = ""} = clickMeButton;
longString += new Array(32 * 1024 * 1024 + 1).join("x");
clickMeButton.longString = longString;
clickMeButton.textContent = `Now holds ${(longString.length / 1024 / 1024).toFixed(1)} MiB`
console.log(replaceButton.textContent);
};
const replaceButton = document.getElementById("replace-button");
replaceButton.onclick = () => {
const textNode = document.createTextNode("Button Replaced");
document.body.replaceChild(textNode, clickMeButton);
// Uncomment to fix leak:
// clickMeButton = null;
}
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment