Skip to content

Instantly share code, notes, and snippets.

@d33pfri3d
Created December 9, 2022 08:36
Show Gist options
  • Save d33pfri3d/9802beaac41aef6cd117284905679bb7 to your computer and use it in GitHub Desktop.
Save d33pfri3d/9802beaac41aef6cd117284905679bb7 to your computer and use it in GitHub Desktop.
JS Mem Heap
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