What is the situation of JavaScript memory leak
In this issue, the editor will bring you what the JavaScript memory leak is like. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
1. Unexpected global variable.
Function foo (arg) {bar = "this is a hidden global variable";}
2. Closure. Closures can maintain local variables within a function so that they cannot be released.
Function bindEvent () {var obj=document.createElement ('xxx') obj.onclick=function () {/ / Even if it is an empty function}}
When the event callback is defined in the above example, the closure is formed because the function is defined within the function, and the internal function-event callback refers to the external function.
3. There are no cleaned DOM element references.
Var elements = {button: document.getElementById ('button'), image: document.getElementById (' image'), text: document.getElementById ('text')}; function doStuff () {image.src =' http://some.url/image'; button.click (); console.log (text [XSS _ clean]);} function removeButton () {document.body.removeChild (document.getElementById ('button')) / / at this point, there is still a global # button reference / / elements dictionary. The button element is still in memory and cannot be recycled by GC. }
4. Forgotten timer or callback function.
Var someResource = getData (); setInterval (function () {var node = document.getElementById ('Node'); if (node) {/ / deal with node and someResource [XSS _ clean] = JSON.stringify (someResource));}, 1000). This is what the JavaScript memory leak is like. If you happen to have similar doubts, please refer to the above analysis. If you want to know more about it, you are welcome to follow the industry information channel.