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
Tags: garbage collection mechanism (RSS), attribute (RSS), attributes (RSS), onclick event (RSS), hyperlink (RSS), memory leak problem (RSS), memory leaks (RSS), javascript function (RSS), javascript object (RSS), garbage collector (RSS), dom object (RSS), management mechanisms (RSS), cycle structure (RSS), automatic memory (RSS), orders of magnitude (RSS), memory manager (RSS), dom objects (RSS), noticeable symptoms (RSS), memory management (RSS), interactive effects (RSS)
Permalink: http://www.codeweblog.com/javascript-memory-leak-problem/





















