Memory leaks in the browser

Source: Internet
Author: User
Tags closure garbage collection

What is a memory leak

A memory leak is a piece of allocated memory that is neither available nor recyclable until the browser process finishes. In C + +, memory leaks are a frequent occurrence because of the manual memory management. And now popular in C # and Java and other languages using automatic garbage collection method to manage memory, normal use of the case almost no memory leaks. Memory is also managed by the automatic garbage collection method in the browser, but a memory leak can occur because of a bug in the browser garbage collection method.

Memory Leaks Quick View

There are a variety of memory leaks in different browsers, and the main findings are currently:

1. Circular Reference

Browsers that have confirmed a leak: IE6.0 FF2.0

Circular references with DOM objects cause most of the current mainstream browser memory leaks here are two simple concepts

Reference: A. Attribute =b,a the reference to B

Circular reference: In short, if a refers to b,b and references A,a and B, it forms a circular reference.

A and B circular references:

var a=new Object;
var b=new Object;
a.r=b;
b.r=a;

A Loop quote yourself:

var a=new Object;
a.r=a;

Circular references are common and are harmless in most cases, but circular references cause memory leaks when there are DOM objects or ActiveX objects in the object that participates in the circular reference. If we replace any of the new object in the example with document.getElementById or document.createelement, a memory leak will occur.

Although this may seem very easy to understand, it is difficult to detect because of the closure involvement that complicates things, and some closure lead to circular references. The following is a very common dynamic binding event:

function bindEvent()
{
   var obj=document.createElement("XXX");
   obj.onclick=function(){
     //Even if it's a empty function
   }
}

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.