Last active
July 9, 2020 19:00
-
-
Save nolanlawson/e0587eaf24f549f4f8255c475b62f304 to your computer and use it in GitHub Desktop.
Demo heap snapshot retainment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Demo heap snapshot retainment</title> | |
</head> | |
<body> | |
<h1>Demo heap snapshot retainment</h1> | |
<button>Add a memory leak</button> | |
<script> | |
(() => { | |
const weakMap = new WeakMap() | |
class BarObject { | |
constructor() { | |
this.circularReference = () => console.log(this) | |
} | |
} | |
class FooObject { | |
constructor() { | |
const barObject = new BarObject() | |
this.barObject = barObject | |
weakMap.set(barObject, this) | |
} | |
} | |
document.querySelector('button').addEventListener('click', () => { | |
const objectReferencedByClosure = new FooObject() | |
window.addEventListener('resize', () => { | |
console.log(objectReferencedByClosure) | |
}) | |
}) | |
})() | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment