In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the causes of javascript memory leakage". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Reasons for javascript memory leakage: 1, improper use of global variables; 2, improper use of closures; 3, delayers or timers have not been cleared; 4, no clean DOM element references (events are not cleared when dom is emptied or deleted).
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
A memory leak refers to a piece of allocated memory that cannot be used or recycled until the browser process ends. That is, due to negligence or error, the program fails to release memory that is no longer in use. Memory leak does not mean the physical disappearance of memory, but after the application allocates a certain section of memory, due to design errors, it loses control of the section of memory before releasing it, resulting in a waste of memory. Here are some common causes of memory leaks.
1. Global variable
JavaScript can handle undeclared variables: a reference to an undeclared variable creates a new variable in the global object. In the browser environment, the global object is window.
Function foo () {name = 'front-end';} / / actually mounts the name variable on the window object function foo () {window.name = 'front-end';} / or function foo () {this.name = 'front-end';} foo () / / actually the this here is the window object pointing to.
This inadvertently creates an unexpected global variable. To prevent this error, add 'use strict;'' at the top of your Javascript file. This opens up a more rigorous mode of parsing JavaScript to prevent unexpected situations. Or pay attention to the definition of variables!
two。 Closure
Closures: anonymous functions can access variables in the parent scope.
Var names = (function () {var name = 'js-say'; return function () {console.log (name);}}) ()
Closures will cause the lifecycle of object references to deviate from the context of the current function. If closures are not used properly, they can lead to circular reference, similar to deadlocks, which can only be avoided and cannot be solved later, and memory leaks will occur even if garbage collection occurs.
3. A forgotten delay / timer
In our daily needs, we may often try setInterval/setTimeout, but we usually forget to clean it up after using it.
Var someResource = getData (); setInterval (function () {var node = document.getElementById ('Node'); if (node) {/ / processing node and someResource node [XSS _ clean] = JSON.stringify (someResource));}}, 1000)
The this in setInterval/setTimeout points to the window object, so the internally defined variables are also mounted globally; the someResource variable is referenced in if, and someResource cannot be released if setInterval/setTimeout is not cleared; in the same way, setTimeout is the same. So we need to remember to go to clearInterval/clearTimeout after using it.
4. DOM element references without cleanup (events are not cleared when dom is cleared or deleted) 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. } this is the end of the content of "what are the reasons for javascript memory leaks". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.