Click on the navigation bar, the reason for memory increment under IE8 and its solution (mainly around whether it is caused by an IFRAME)

Source: Internet
Author: User
Tags garbage collection blank page

In recent projects, you have encountered this problem: click on the navigation bar IE8 memory increment
How to fix it.
The first thing to understand is 2 concepts:memory overflow and memory leaks;
Memory overflow out of memory, refers to the program in the application of the RAM, there is not enough memory space for its use, there is an out-of-memory; for example, an integer is applied, but a long can be stored to save it.
Memory leak memories leak, refers to the program after the application of memory, can not free up the requested memory space, a memory leak damage may be ignored, continuous click, memory is increasing. So memory leaks accumulate consequences very serious, no matter how much memory, sooner or later will be occupied by the light.
2 Relationship: Memory leak will eventually cause out of memory.
Memory leaks can be divided intoClass 4
1.Frequent memory leaks。 The code that occurs in memory leaks is executed multiple times, causing a memory leak each time it is executed.
2. Accidental memory leaks. Code that occurs with a memory leak occurs only under certain circumstances or during operation. The occurrence and the incidental sex are opposite. For a given environment, the occasional may become a frequent occurrence. So test environments and test methods are critical to detecting memory leaks.
3. Disposable memory leaks. The code that occurs with a memory leak is only executed once, or because of an algorithmic flaw, there is always a piece of memory that leaks. For example, allocating memory in the class's constructor does not release the memory in the destructor, so a memory leak occurs only once.
4.An implicit memory leak。 The program keeps allocating memory while it is running, but it does not release memory until the end. Strictly speaking, there is no memory leak, because the final program frees up all the requested memory. But for a server program that needs to run for days, weeks, or months, not releasing memory in time can also result in the eventual exhaustion of all of the system's memory. So, we call this kind of memory leak as an implicit memory leak.
Unfortunately, there are frequent memory leaks in our project.
The following is an analysis of our company's frequent memory leaks: (Our company's content is basically through the IFRAME nested, it is mainly to troubleshoot is not this reason.) )
First: Every click, the memory will increase, if you add a lot, on behalf of the page itself does not conform to specifications, such as the introduction of a large number of duplicate JS files, and so on, circular reference DOM node; Please solve this problem, and then continue down.
Second: Keep clicking, keep clicking, see if it will appear to increase to a certain extent, will no longer increase. If so, it means that the memory cleanup is working. If not, do this first: When the IFRAME is closed, you need to clear the DOM element inside and have the IFRAME point to a blank page. such as the following code:

var el = document.getElementById ("IFrame1");
            if (EL) {
                iframe = El.contentwindow;                
                Clear document
                el.src = ' About:blank ';
                try {
                    iframe.document.write (');
                    Iframe.document.clear ();
                    CollectGarbage ();
                } catch (e) {};
                Clear Node
               var _parentelement = El.parentnode;
                if (_parentelement) {
                    _parentelement.removechild (EL);            
               }
            }

Third: After using the above method, reloading the IFRAME will release a certain amount of memory (can be seen from the process of memory changes), but the new memory after loading more than the memory freed, and then after multiple loading will cause a memory overflow.
Four: First see is not IE bug, with the next CollectGarbage () function for garbage collection. If this function has no effect, it may indicate that the memory overflow is not only in IE, Firefox and Google Browser have, but the degree of overflow is relatively light.
V: Use the $.get replace IFRAME to dynamically load the HTML to see if the memory continues to increase.
The code is as follows:

$.get (SRC, function (data) {///initial a.html include Div#iframe
                    $ ("#center"). empty ();
                    $ ("#center"). HTML (data);
                });

If it is, it has nothing to do with the IFRAME. Please perform the first link in the page itself is not normative problem.

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.