Javascript memory leaks
1. What is memory leakage?
Memory leakage means that the memory allocated to the application cannot be re-allocated, even if the memory is no longer used. Normally, the garbage collector recycles DOM elements and event processors when they are not referenced or accessed. However, memory leakage in earlier versions of IE (IE7 and earlier) is very likely to occur, because the Memory Manager cannot correctly understand the Javascript lifecycle and does not recycle the memory before the lifecycle is broken (you can assign a value to null.
2. Why do you need to pay attention to it?
In large Web ApplicationsProgramMemory leakage is a common unintentional programming error. Memory leakage will reduce the performance of Web applications, until the wasted memory exceeds the system's allocation, the application will not be available. As a web developer, developing an application that meets functional requirements is only the first step. performance requirements are equally important to the success of Web applications, moreover, it may cause application errors or browser crashes.
3. What are the main causes of Memory leakage in JavaScript?
1) circular reference
A simple example: a DOM object is referenced by a javascript object, and the same or other JavaScript objects are referenced at the same time. This DOM object may cause memory leakage. Reference of this DOM object will not be reclaimed by the garbage collector when the script is stopped. To break the circular reference, the reference of the DOM element object or DOM object must be assigned null.
2) JavaScript Closure
Many implementations depend on JavaScript closures due to limitations in the Javascript scope. If you want to know more about the closure, please check my previousArticleJavascript scope and closure.
The closure can cause memory leakage because the internal method maintains a reference to the external method variables, so although the method returns, the internal method can continue to access the private variables defined in the external method. The best practice for JavaScript programmers is to disconnect all event processors before page reloading.
3) Dom Insertion Sequence
When two DOM objects in different ranges are appended together, a temporary object is created. When the DOM object changes the scope to document, the temporary object will be useless. That is to say, DOM objects should be added from the top DOM elements on the current page until the remaining DOM elements are added. In this way, they always have the same scope and do not generate temporary objects.
4) how to detect?
Memory leakage is generally difficult for developers to detect because they are caused by a large numberCodeBut it does not affect the function of the program before the system memory is insufficient. This is why someone collects application performance metrics during a long test period to test performance.
The simplest way to detect memory leakage is to use the task manager to check memory usage. Open the application in the new tab of the Chrome browser and check whether the memory usage is increasing. Other debugging tools, such as chrome Developer Tools, provide memory monitors. This is a tutorial on the features of heap Analysis on Google's website.
Refer:
1. http://javascript.crockford.com/memory/leak.html
2. http://msdn.microsoft.com/en-us/library/Bb250448
3. http://www.ibm.com/developerworks/web/library/wa-memleak/
(Oschina. Net compilation)
Transferred from:Http://kb.cnblogs.com/page/141401/