Memory leaks in JS

Source: Internet
Author: User

First, what is a memory leak

A memory leak refers to a piece of allocated memory that cannot be used or recycled until the browser process ends. In C + +, memory leaks are a frequent occurrence because of the manual management of memory. And now popular C # and Java and other languages use automatic garbage collection method to manage memory, in the case of normal use, there will be little memory leaks. The browser also uses the automatic garbage collection method to manage memory, but due to a bug in the browser garbage collection method, a memory leak occurs.

Two, memory leakage of several cases

1, when the page element is removed or replaced, if the element binding event is still not removed, in IE will not be properly handled, the first time to manually remove the event, otherwise there will be a memory leak.

<div id= "mydiv" >    <input type= "button" value= "click Me" id= "mybtn" ></div><script type= "text/ JavaScript ">    var btn = document.getElementById (" mybtn ");    Btn.onclick = function () {        document.getElementById ("mydiv"). InnerHTML = "processing ...";    } </script>

Should be changed to the following

<div id= "mydiv" >    <input type= "button" value= "click Me" id= "mybtn" ></div><script type= "text/ JavaScript ">    var btn = document.getElementById (" mybtn ");    Btn.onclick = function () {        btn.onclick = null;        document.getElementById ("Mydiv"). InnerHTML = "processing ...";    } </script>

or using event delegates

<div id= "mydiv" >    <input type= "button" value= "click Me" id= "mybtn" ></div><script type= "text/ JavaScript >    Document.onclick = function (event) {        event = Event | | window.event;        if (event.target.id = = "Mybtn") {            document.getElementById ("mydiv"). InnerHTML = "processing ...";        }    </script>


2.

var A=document.getelementbyid ("xx");
var B=document.getelementbyid ("xxx");
A.r=b;
B.r=a;

var A=document.getelementbyid ("xx"); a.r=a;

For purely ECMAScript objects, as long as there are no other objects referencing objects A, B, which means they are only references to each other, they are still recognized and processed by the garbage collection system. However, in Internet Explorer, if any object in a circular reference is a DOM node or an ActiveX object, the garbage collection system does not find that the cyclic relationship between them is isolated from other objects in the system and frees them. Eventually they will be kept in memory until the browser is closed.

3.
    function Bindevent () {     var obj=document.createelement ("XXX");     Obj.onclick=function () {         //even if it ' s a empty function     }}

Closures can maintain local variables within a function so that they are not released. The above example defines an event callback, because it is defined within the function, and the internal function----------the reference to the event callback forms a closure solution, the event handler is defined externally, and the closure is unblocked.
function Bindevent () {     var obj=document.createelement ("XXX");     Obj.onclick=onclickhandler; } function OnClickHandler () {     //do something}

Or, in the external function that defines the event handler, remove the reference to the DOM (in the case of the JavaScript authoritative guide, in closures, the unused properties in the scope can be removed to reduce memory consumption.) )

    function Bindevent () {     var obj=document.createelement ("XXX");     Obj.onclick=function () {         //even if it ' s a empty function     }     obj=null;}

4.

A = {p: {x:1}};b = A.p;delete a.p;

After executing this code, the value of b.x is still 1. Because the deleted attribute reference still exists, in some implementations of JavaScript, there may be a memory leak due to this non-rigorous code. So when destroying an object, iterate through the properties in the property, and then delete it.

5. Automatic type boxing Conversion

Look at the online information, said the following code in the IE series will lead to memory leaks, first mention a god, the specific leakage or not first regardless

var s= "Lalala"; alert (s.length);

s itself is a string instead of an object, it has no length property, so when the length is accessed, the JS engine automatically creates a temporary string object that encapsulates s, and this object is bound to leak. The bug is pretty easy to solve, and remember that all value types do. Explicitly convert before operation:

var s= "Lalala"; Alert (new String (s). length);

6. Some DOM operations

The unique problem with the IE series is simply that, in the appendchild;ie7 of DOM elements that are not on the DOM tree, it seems that in order to improve memory leaks, IE7 uses an extreme solution: to reclaim all the elements of the DOM tree while leaving the page.

This article starts: http://www.cnblogs.com/sprying/archive/2013/05/31/3109517.html

Memory leaks in JS

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.