About ie Memory leakage and javascript Memory release _ Javascript tutorial

Source: Internet
Author: User
Javascript: about memory leakage of ie and the release of javascript Memory, Javascript tutorial

Recently, as a company's business system, the company requires that it be as close as possible to c/s, that is, like c/s. Click the text box to bring up the relevant content of this project, select input.
I used the pop-up window. In the subwindow, double-click the selected project and return the selected value to the parent form.
After the system is complete, the customer uses the M memory configuration, so after opening 30-40 forms, the virtual memory usage of ie reaches nearly 200 MB, which slows down the system and the javascript operation.

After a Google search, I realized that it may be caused by the memory leakage of ie. For more information, see related articles in www.cnblogs.com.

I use the task manager to open a pop-up window. The ie memory is increased by 1-3 MB, and then the window is closed. Sometimes the memory is not released, and sometimes dozens of k is released. It seems that the problem lies in the memory release.

Then, based on the idea of releasing memory, search and find methods to solve the problem. I found an undisclosed javascript function CollectGarbage, which is used to release the memory. Before the completion of all the pop-up windows, I set all the variables of the custom javasp P to null and call the CollectGarbage function.

In javascript, the variable is set to null, and javascript does not release the memory. When the variable is defined again, the memory where the variable is located will be overwritten. If it is not set to null, a new memory space will be opened when javascript defines the variable again.

After the above processing is used, open the window again. The ie memory is increased by 1-3 MB each time, but after closing the window, then, ie will release a certain amount of data that exists between kb and 2 MB. Has played a certain role.
Since I use third-party controls on the page, I do not control the memory in javascript in the third-party controls.


1. example of how to release javascript Memory


Set the parameters of all upper-level functions to null and use CollectGarbage to release the memory.


Example
Script

// 32 M
Function AllocMem ()
{
Var str = "12345678 ";
For (var I = 3; I <24; I ++)
Str + = str;
Return str;
}

Function A ()
{
A = null;
Return r;
Function r ()
{
}
}


Var f = A (AllocMem ());
Alert (1 );
CollectGarbage ();
// Obviously, it has been released.
R = null;
Alert (2 );
CollectGarbage ();

Script

----------
The memory release rules (script layer) are well understood.
(How to allocate menus for each layer? M memory. Only the task manager can see the situation)


Note:
CollectGarbage () usually introduces the memory at the core, because it is called only when the IE or NS program ends. This is safe.

 


Note:
1) if you keep the object reference in the window in another window, the memory is not released even if the window is closed.

As you might know, windows opened with window. open () may share a process with its opener (_ blank or _ new window may not ). that is, even if you see those two windows on the desktop, if you look at the process table in the Task Manager, you may only see one iworkflow E. EXE running. memory may only be released when the process is terminated

2) worse, if you keep the reference of a DOM object and close the window where the object is located, IE will crash and report a memory error (or request, restart)

I wocould say this looks like a bug, you might want to report to Microsoft
Http://www.cnblogs.com/chillsrc/archive/2006/12/28/606544.html

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.