Created
December 9, 2022 08:36
-
-
Save d33pfri3d/9802beaac41aef6cd117284905679bb7 to your computer and use it in GitHub Desktop.
JS Mem Heap
This file contains hidden or 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
let array = []; | |
// This function adds an object to the array, but the object has a reference | |
// to the array itself, creating a circular reference. | |
function addToArray() { | |
let obj = {}; | |
obj.array = array; | |
array.push(obj); | |
} | |
// If this function is called repeatedly, it will create more and more objects | |
// that are added to the array, but they will never be garbage collected because | |
// they have a reference to the array, and the array has a reference to them. | |
// This will cause the heap to fill up with unused objects and eventually cause | |
// a memory leak. | |
addToArray(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment