Analysis of IE Memory leakage caused by Javascript closures _ javascript skills

Source: Internet
Author: User
Analysis of IE Memory leakage caused by Javascript closures. js_obj is a reference to a DOM element. The DOM element remains in the web page for a long time and will not disappear. A property of this DOM Element The Code is as follows:


Function fors (){
Obj_a = obj_ B;
Obj_ B .attr = obj_a;
}


The Code is as follows:


Function fors (){
Obj_ B = {};
Obj_ B .attr = obj_ B;
}


The above are two explicit circular references. Memory leakage occurs in IE. Due to the memory recycle mechanism of IE, it will occupy the memory for a long time and cannot be released.

But the memory leakage of the closure is somewhat concealed. The circular references of closures are indirect.

The Code is as follows:


Function iememery (){
Var js_obj = document. createElement ("p ");
Js_obj.oncontextmenu = function () {return false ;}
}


On the surface, there is no circular reference. But the above is a closure. According to the characteristics of the closure, internal functions have the right to access variable objects of external functions. So after iememery () is executed:
Js_obj is a reference to a DOM element. It will not disappear for a long time in the webpage. oncontextmenu, a property of this DOM element, is an internal function reference (closure ), this anonymous function has a hidden Association (scope chain) with js_obj)
So a circular reference is formed, namely:
Js_obj.oncontextmenu indirectly references js_obj. That is to say, an attribute of this object indirectly references itself.
As long as there is a circular reference, memory leakage will occur in IE. Open your windows Task Manager and refresh the html page containing the Code in IE to check whether the memory usage of the Iexploer process keeps increasing and does not automatically recycle (decrease );
Solution:

The Code is as follows:


Function iememery (){

Var js_obj = document. createElement ("p ");
Js_obj.oncontextmenu = function () {return false ;}; js_obj.oncontextmenu = null; // Add this sentence to disconnect the reference}


When JavaScript objects and dom objects are directly referenced cyclically in IE, and no reference points to them afterwards,
For IE 6, the memory is leaked until the IE process is disabled.
For IE 7, the memory is leaked until the current page is left.
For IE 8, the GC recycler recycles their memory, regardless of whether the current mode is compatibility or not.
The GC recycler in the earlier IE js engine can only process js objects, but cannot process DOM objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.