Javascript memory leak problem
The field in the browser, most of memory leaks may occur, but most common in IE, but also the most serious, in particular, there are many pages in Javascript when the interactive effects. Which relate to the cycle structure (cyclic structure), DOM object property, JavaScript object properties and the garbage collector (garbage collector).
Cycle structure (cyclic structure), a DOM object is the JavaScript object that contains parameters (event handler), JavaScript objects and DOM object contains attributes. (For example, a hyperlink to an A to add a onclick event function, this time A will have a corresponding event handler, and the JavaScript function has been the object of the onclick attribute A).
When the cycle structure (cyclic structure) if there is no other formation parameters of the object passed to the DOM or Javascript function object, JavaScript's garbage collector (an automatic memory manager) this will be the release of two objects of memory and re-configuration, But IE's DOM object properties and parameters can not be managed by the removal of JavaScript (DOM objects can not clear the attributes of the parameters), and his own memory management mechanisms and structures can not understand the cycle (cyclic structure) of the garbage collection mechanism. Therefore, when the cycle structure (cyclic structure) conditions for the formation of garbage collection, IE can not correctly recall the memory management, resulting in memory leaks. Of course, this is only in the recovery cycle of the structure of memory is not a great number of orders of magnitude, the only noticeable symptoms of memory leaks.
In addition, the structure as a result of the cycle appeared in the closure of the probability of a larger problem of memory leakage can not be ignored.
Solution are:
First, to avoid possible to remove or change the DOM object (remove node / reset innerHTML) appear in the cycle structure (cyclic structure);
Second, in the DOM object is to remove the former (remove node / reset innerHTML) empty his attributes (domNode.Jsfunction = null)
1. To avoid an expansion of the properties of DOM object parameters, if there should be clear in due course;
2. If an event handler after the removal of certain DOM objects may be called, must be clear;
3.Ajax in the XMLHttpRequest's onreadystatechange function call after the incident clearance.
4 ....
More than a purely personal understanding, we identify their own solutions, but also hope that the wing of a master.
Attached to a removal of a DOM object and its children and grandchildren of the event attributes of the object function
function purge (d) (
var a = d.attributes; / / get DOM object properties of all the events
if (a) (
var l = a.length;
for (var i = 0; i <l; i + = 1) (
var n = a [i]. name; / / get DOM object attribute name events such as onclick, onblur, etc.
if (typeof d [n] === 'function') (
d [n] = null; / / empty the DOM event object attribute value
)
)
)
a = d.childNodes; / / handle child elements
if (a) (
l = a.length;
for (i = 0; i <l; i + = 1) (
purge (d.childNodes [i]);
)
)
)
Douglas Crockford: JScript Memory Leaks
http://www.crockford.com/javascript/memory/leak.html
Justin Rogers: Understanding and Solving Internet Explorer Leak Patterns
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/IETechCol/dnwebgen/ie_leak_patterns.asp
Original Address: http://hi.baidu.com/webworker/blog/item/25f9f0dcf14d51a2cc116671.html
Related Posts of Javascript memory leak problem
-
Javascript in the browser environment (vi) incident
Matter Examples of a lot of events. Such as user input, the click of a button and so on. Can put a javascript function assigned to an event (this can be called the event listener or event handler), when the incident happened, and will perform this fu ...
-
ruby MBARI large patches Performance Evaluation Report
Before JavaEye news ruby memory leak culprit - the specter of a detailed analysis of the current pointer Ruby official version (MRI version) causes memory leaks. Brent Roman today issued a Super patch , Which contains 6 patch file to resolve the Ruby ...
-
Play flash page 3 (javascript script swfobject.js1)
/ ** * SWFObject v1.5: Flash Player detection and embed - http://blog.deconcept.com/swfobject/ * * SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License: * Http://www.opensource.org/licenses/mit-license.php * * / if (typeof deconc ...
-
javascript built-in functions Info
48. In the old browsers do not perform this JS :<!-- //--> 49. To quote a document-style JS: <script type="text/javascript" src="/blog/aaa.js"> </ script> 50. Specified in the script does not support the browser ...
-
SUI-JavaScript rich UI library integration solution
Introduction: SUI is not a class implementation of the UI, nor is it a standard set of UI markup language, it is just to help the existing UI frameworks (such as Ext, YUI like) to mark the way to the statement and the creation of UI. The names of the ...
-
EJB
public transient int counter; / / transient property private String firstname; / / persistent property @ Transient String getLengthInMeter () (...) / / transient property String getName () (...) / / persistent property @ Basic int getLength () (...) ...
-
jboss ejb3 Message Driven Bean
Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Messag ...
-
Some interview questions java
The first is the company give you a chance to meet, it is necessary to know to meet from time to equal the interview, and have a lot of companies to see you at the first time will give you a ready point of doing something trivial, these questions, al ...













Leave a Reply