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.