Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the situation of JavaScript memory leak

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report