Javascript closure-induced IE memory leak analysis _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

function Fors () {
Obj_a = Obj_b;
Obj_b.attr = obj_a;
}

Copy Code code as follows:

function Fors () {
Obj_b = {};
Obj_b.attr = Obj_b;
}

Above are two very display circular references, ie generated a memory leak, due to the IE memory recovery mechanism, the lead to long-term memory consumption can not be released.

But the closure of the memory leak, some hidden. Because the closure of the circular reference, is indirect.
Copy Code code as follows:

function Iememery () {
var js_obj = document.createelement ("div");
Js_obj.oncontextmenu = function () {return false;}
}
<body onload= "Iememery ()" >

On the surface, there is no circular reference at all. But above is a closure, according to the characteristics of the closure, the internal function has access to the external function of the variable object. So when Iememery () is executed:
Js_obj is a reference to a DOM element, a DOM element that is in the Web page for a long time and does not disappear, and this DOM element has a property oncontextmenu, an internal function reference (closure), and the anonymous function has a hidden association with js_obj (scope chain)
So form a, circular reference. namely:
Js_obj.oncontextmenu Indirect reference to js_obj that is to say, the object of an attribute, and indirectly refers to their own.
As long as there is a circular reference, there will be a memory leak under IE. Open your Windows Task Manager, in IE continuously refresh the HTML page containing this code to see the iexploer process memory footprint, has been rising, and will not automatically recycle (reduce);
Solution:
Copy Code code as follows:

function Iememery () {

var js_obj = document.createelement ("div");
Js_obj.oncontextmenu = function () {return false;}; Js_obj.oncontextmenu = null;//Add this sentence, disconnect the reference}

When IE takes place JS object and Dom Object Direct circular reference, and after no reference point to them,
If it is IE 6, memory leaks until the IE process is closed
If it is IE 7, the memory leaks until you leave the current page
If it is IE 8, the GC collector reclaims their memory, regardless of whether the current is compatibility mode.
Previously, the GC collector in the IE JS engine can only handle JS objects and cannot handle 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.