IE8 memory leaks (memory continues to grow) and solutions _javascript tips

Source: Internet
Author: User

A regular local update was used for the page when it was recently developed, results in ie6,7 and Firefox, all normal, and in the IE8 after a few hours on the browser crashed, showing the memory overflow, I thought it was bad code write to lead to memory leaks, but ie6,7 and normal, investigated a bit, It turned out to be a IE8 bug.

Problem points

In IE8, the memory used to generate a particular DOM node will not be freed, even if the memory of those nodes is removed.

Memory-leaking node types include: Form, button, input, select, textarea, A, IMG, and OBJEC

Most of the other node types are not compromised, such as span, Div, p, table, and so on.

This problem occurs only in IE8, and other browsers do not occur.

If the user presses the F5,IE8 will refresh the page, first it will unload window.top, this time will release memory. If the page is an IFRAME, then unload this iframe, there is no response. It appears that only window.top is unload and memory is released.

Example

Example 1

Execute the following code, IE8 will leak memory.

function Leak1 () { 
var node = document.getElementById ("To_area"); 
node.innerhtml = " 
 

Attention:

* This example adds a node, so it will leak.

* There is a div,id in the "To_area".

* To remind you, there are no closures and circular references.

Example 2

The following code does not use innerHTML, but it still leaks

function Leak2 () { 
var node = document.getElementById ("From_area"). CloneNode (true); 
Node.id = "New_area"; 
Document.body.appendChild (node); 
Document.body.removeChild (node); 
node = null; 
}

Attention:

* From_area is the ID of the form, and there are no closures and circular references.

Example 3

This is the simplest and most direct example:

function Leak4 () { 
var node = document.createelement ("IMG"); 
Document.body.appendChild (node); 
Document.body.removeChild (node); 
}

Attention:

* If you replace IMG with span, there will be no leaks.

These examples only leak memory in IE8, and I tested it in Windows XP, Windows Vista, Windows Server 2008, Windows Server 2008 R2, and IE8 in Windows 7, and used IE8 in IE7 Compatibility mode and Standard mode, each of which will be compromised.

Test page

About leaks

Memory size increases over time, but this does not directly cause the browser to crash. The memory used by the browser seems to be capped, and it appears to restrict the memory used by DHTML from some internal means.

When the memory reaches the upper limit, the browser automatically handles it, such as a pop-up dialog box, showing insufficient memory.

The same problem with the IFRAME was found on its own test (under IE8)

Add: IFrame Memory release

EXT Core Developer Jack's answer is that Tabpanelitem does not perform special processing on the elements that are customized to the tab when it shuts down, which must be done outside the control. On the other hand, the relevant information that IE in the recovery of the elements of the IFRAME bugs, in general should be the element's SRC attribute value to "Abort:blank" and manually removed it from the DOM tree, You can then empty and invoke CollectGarbage () the variable that references it in the script to avoid memory leaks caused by the IFRAME not being recycled properly.

 <script> function Clearram () {var frame = document.getElementById ("ifr_content"); f
RAME.SRC = ' About:blank ';
Frame.contentWindow.document.write (");//Empty the contents of the frame frame.contentWindow.document.clear (); Frame.contentWindow.close (); Avoid frame memory leaks if (navigator.userAgent.indexOf (' msie ') >= 0) {if (collectgarbage) {collectgarbage ();//ie-specific free memory//delete
Original tag var tags = document.getElementById ("Ifrset");
Tags.removechild (frame);
Add Frameset frame var _frame = document.createelement (' frame ');
_FRAME.SRC = ';
_frame.name = ' content ';
_frame.id = ' ifr_content ';
Tags.appendchild (_frame); 5 Seconds SetInterval (function () {if (navigator.userAgent.indexOf (' msie ') >= 0) {if (collectgarbage) {//ale RT (1) collectgarbage (); IE-Specific Free memory}}, 5000) </script> 

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.