All,
I wonder why this is a memory leak:
window.onload = function outerFunction(){
var obj = document.getElementById("app")
obj.onClick = function innerFunction(){
alert("Hi, I will leak");
}
}
<button id="app">Click Me</button>
This example above says: a javaScript object(obj
) contains a reference to a DOM object(referenced by the id "app"). The DOM element, in turn, has a reference to the javascript obj
, the resulting circular reference between the javascript object and the DOM object causes a memory leak.
And it says the solution is:
at the end of function outerFunction
, set the obj = null
My confuse is:
Why it says the DOM has reference to that obj
? I can not understand this. And why this is a memory leak?