Home  >  Article  >  Web Front-end  >  About ie memory leaks and javascript memory release

About ie memory leaks and javascript memory release

黄舟
黄舟Original
2016-12-14 15:58:28864browse

Recently I am working on a company's business system. The company requires it to be as close to c/s as possible, that is, just like c/s, click on the text box to pop up the relevant content of this project for selection and input.
I used a pop-up window, then double-clicked the selected item in the child window and returned the selected value to the parent form.
After the system is completed, during the customer's use, since the customer uses a 512m memory configuration, after opening 30--40 forms, the virtual memory usage of IE reaches nearly 200m, thus making the system Slow, the running of javascript also slows down.

After searching on Google, I found out that it might be caused by a memory leak in IE. For details, please refer to relevant articles in www.cnblogs.com.

I use the task manager and open a pop-up window, and the memory of IE increases by 1-3m, and then I close the window. Sometimes the memory is not released, and sometimes it is only released for tens of kilobytes. It seems that the problem lies in memory release.释 Then release the idea of ​​memory and search for search methods to solve this problem. I found an undisclosed JavaScript function CollectGarbage, which is used to release memory. I set all the self-defined javasctip variables to null before all pop-up windows end, and call the CollectGarbage function.

                  If you set a variable to null in JavaScript, JavaScript will not release the memory. When the variable is defined again next time, the memory where the variable is located will be overwritten. If it is not set to null, JavaScript will open up a new memory space when defining the variable again.

After using the above processing, when you open the window again, the memory of IE still increases by 1-3m each time, but after closing the window, IE will release a certain amount of memory ranging from 500k to 2m. Played a certain role.

Since I use third-party controls in the page, how the memory in the JavaScript in the third-party controls is managed is not under my control. V a 1.javaScript memory release method Example



The parameters of all superior functions are set to NULL, and the collectgarbage is used to release the memory.


Example

<script> <p> <br> //32M </p> function AllocMem() <p> { <br> var str="12345678"; <br> for(var i=3;i<24;i++ ) <br/> str+=str; <br/> return str; <br/> } <br/> function A(a) <br/> a=null; <br/> return r; <br/> function r() <br/> { <br/> } <br/> } <br/> <br/> var f=A(AllocMem()); <br/> alert(1 ); <br/> CollectGarbage(); <br/> //Obviously, it has been released. <br/> r=null; <br/> alert(2); <br/> CollectGarbage(); <br/> </script>

----------
I already understand the memory release rules (script layer) very well. Thoroughly.
(Each level of menu allocates ?M of memory. You can only see the situation by looking at the task manager)


Note:
CollectGarbage() usually releases memory when the core, because it is called when the IE or NS program ends .This is the safest way



Note:
1) If you keep the reference of the object in the window in another window, even if you close the window, the memory will not be released

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 IEXPLORE.EXE running. Memory may only be released when the process is terminated

2) What’s worse is that if you keep a reference to a DOM object and close the window where the object is located, IE will crash and report a memory error (or require a restart)

I would say this looks like a bug, you might want to report to Microsoft

Please pay attention to the PHP Chinese website (www.php.cn) for more related articles!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn