Garbage collection mechanism
JavaScript does not need to free up memory manually, it uses an automatic garbage collection mechanism (garbage collection). This variable is freed from memory when an object is useless, that is, when the object is referenced in the program without a variable.
Circular references
Three objects A, B, C
a property of a->b-> c:a refers to B, and the same C is referenced by the property of B. If a is cleared, then B and C are also released.
A->b->c->B: Here a property of C is added to reference the B object, and if this is clear a, then B and C will not be freed because a circular reference is generated between B and C.
1 varA = {};2A.pro = {a:100 };3A.pro.pro = {b:100 };4A =NULL ; 5 //in this case, {a:100} and {b:100} are also freed. 6 7 varobj = {};8Obj.pro = {a:100 };9Obj.pro.pro = {b:200 };Ten varboth =Obj.pro.pro; Oneobj =NULL; A //in this case {b:200} is not freed, and {a:100} is freed.
Circular references and closures
1 function outer () {2 var obj = {}; 3 function 4 // reference to obj object 5 }6 Obj.inner = inner; 7 }
Dom Circular Reference
function Setupleak () { myglobalobject = document.getElementById ("Leakeddiv"); document.getElementById ("Leakeddiv"). Expandoproperty = myglobalobject; }
Memory leaks under IE
In the JS programming under IE, the following programming methods will cause the problem of not releasing the memory even if IE is turned off, the following classification gives:
1. The attribute added to the DOM object is a reference to an object. Example:
var MyObject = {};
document.getElementById (' Mydiv '). MyProp = MyObject;
Workaround:
Write in the Window.onunload event: document.getElementById (' mydiv '). MyProp = null;
2. The DOM object and the JS object refer to each other. Example:
function Encapsulator (Element) {
This.elementreference = element;
Element.myprop = this;
}
New Encapsulator (document.getElementById (' mydiv '));
Workaround:
Write in the OnUnload event: document.getElementById (' mydiv '). MyProp = null;
3. Bind the event with Attachevent to the DOM object. Example:
function DoClick () {}
Element.attachevent ("onclick", DoClick);
Workaround:
Written in the OnUnload event: element.detachevent (' onclick ', doclick);
4, from outside to inside execution appendchild. This will not be released even if the call to RemoveChild. Example:
var parentdiv = document.createelement ("div");
var childdiv = document.createelement ("div");
Document.body.appendChild (PARENTDIV);
Parentdiv.appendchild (CHILDDIV);
Workaround:
Execute appendchild from inside to outside:
var parentdiv = document.createelement ("div");
var childdiv = document.createelement ("div");
Parentdiv.appendchild (CHILDDIV);
Document.body.appendChild (PARENTDIV);
5. Repeatedly overriding the same attribute can cause memory to be heavily occupied (but memory will be freed after IE is closed). Example:
for (i = 0; i < i++) {
Hostelement.text = "ASDFASDFASDF";
}
This is the equivalent of defining 5,000 properties!
To summarize, memory leaks fall into four categories:
1. Circular references (Circular References) -ie The object instances produced by the COM component of the browser and the object instances produced by the Web script engine are references to each other, causing a memory leak.
This is also the most common and main way of leaking in web pages;
2, intrinsic function Reference (Closures) -closures can be regarded as a special form of cyclic application which causes a lot of problems at present. Depending on the specified keyword and syntax structure,
The closures call is more easily discovered by us;
3, page cross-leakage (cross-page Leaks) -page cross-leakage is actually a small leakage, it is usually in your browsing process, due to the internal object thin meter caused. Below we
The question of the DOM insertion Order is discussed, and in that case you will find that only a small amount of code can be changed, and we will avoid the effect of object thinning on object construction;
4, seemingly leaking (pseudo-leaks) -This is not a real sense of leakage, but if you do not understand it, you may be in your available memory resources become less and more when the very
Degree depressed. To illustrate this problem, we'll throw a lot of memory "leaks" by rewriting the contents of the script element.
Details can be referred to: http://www.cnblogs.com/carekee/articles/1733847.html
JavaScript memory leaks