Browser, DOM elements and garbage collection in JS

56 Views Asked by At

I am trying to understand memory leaks in JavaScript and I have come across this example:

let nodes = {
    owner: document.getElementById('owner')
};
document.getElementById('owner').remove();

To me it makes sense that the internal memory that stores this DIV element node can not be garbage collected because the "nodes" JS object's "owner" property still have a reference/pointer to it.

However I am trying to visualize how these things work and I am not sure if I get it correctly. I assume a DOM node is an object or some other special data structure.

I guess my question really is, does the browser/browser's DOM have references to the heap to this DOM node object, and does the JavaScript running within the browser also have references to the heap to this DOM node object? So does the browser and the javascript running within it share these memory addresses when dealing with DOM objects and global functions, or is this understanding bad?

My visualization:

enter image description here

0

There are 0 best solutions below