JavaScript garbage Collection (iii)--memory leaks

Source: Internet
Author: User

One, JavaScript memory monitoring Tool

Before discussing memory leaks, let's introduce several JavaScript memory monitoring tools.

IE sieve and jsleaksdetector (these two can be downloaded in the following attachment), Firefox leak monitor,chrome porfiles and so on.

1, Leak Monitor very hard to find the download link, but my version of Firefox is not installed.

2, Chrome porfiles, click on the F12 can be seen, as shown:

3, IE jsleaksdetector, I use the Win7 system, behind the discovery of 64-bit browser does not support, only open 32-bit IE8 have this plugin. This plugin will not work.

4, IE sieve, the next demonstration is to use this tool.

The header of each column in the bottom right table represents the following:

    1. Usage Memory use
    2. Delta memory increase or decrease. Green is memory reduction, red is memory increase
    3. #inUse HTML nodes in use, such as Div
    4. #leaks a leaked HTML node, such as a div

Second, circular reference
<id= "Leak1"></p>    <   type= "button"  id= "Btncycle"  value= "Circular Reference" onclick = "Cycle_leak ()" />
var host = {mycycle: '};         function Cycle_leak () {            var cycle = document.getElementById (' leak1 ');             = cycle;            Cycle.setattribute (' cycle ', host);        }

1, set a variable host, one of its properties mycycle assigned to a P object with ID LEAK1

2. Set a custom attribute for the P object with ID LEAK1 as the host variable

Using Tools to view:

1. Click the button to cycle the reference

    

2. Click the button on the top left to scan now

    

3. Click the button in the top left corner show in use

    

4. You can see the P tag of ID=LEAK1, there is a circular reference

  

Iii. memory leaks due to closures
<type= "button"  id= "Btnclosure"  value= "Closure" onclick = "Closure_leak ()" />    <  ID= "Pmemory"></p>
//Closed Package        functionClosure_leak () {varel = document.getElementById (' pmemory '); El.onclick=function() {alert ( This. ID); }            //Removing Nodes            if(El.removenode) {El.removenode (true); }Else if(El.parentnode) {el.parentNode.removeChild (EL); }            //page Refreshlocation.reload (); }

Here is one thing to note, my original computer is IE9,IE9 no longer have this problem, back to IE8 only to see the leak.

El is a DOM element, and El references its listener function for the Click event, and the function also references the EL element through its scope chain. Specific as follows:

1, Closure_leak () the scope object created when executing is called Scopea

2. Scopea reference DOM Object El

3. Dom object El Reference function functions () {alert (this.id);}

4. Function functions () {alert (this.id)} reference Scopea

5, remove node and page refresh is to see the leak node in sieve

Using Tools to view:

1. Click the button to close the package

2. On the right memory usage you can see that leaks is 1

  

3. Click the button show Leaks

  

4. Display the information of the leaking node, do not know why the ID is not displayed, I clearly have the ID of the

  

This tool can be used to see the leak, but it is not very useful, a lot of leaks on the internet I can not use this tool to withdraw. This tool I also only simple display uses, the concrete operation also needs the further research.

Sieve and Jsleaksdetector Tools download:

http://download.csdn.net/download/loneleaf1/8008215

Demo Download:

http://download.csdn.net/detail/loneleaf1/8031073

Resources:

Http://kb.cnblogs.com/page/74836/GC and JS memory leaks

http://www.iteye.com/topic/172891 Sieve Software Usage Introduction

http://blog.csdn.net/samxx8/article/details/7486372 JavaScript Memory Monitoring Tool

Analysis of http://www.cnblogs.com/hyddd/archive/2013/02/07/2908598.html JavaScript garbage collection

Http://blog.jobbole.com/49084/JavaScript Garbage Collection

http://kb.cnblogs.com/page/137723/reducing JavaScript garbage collection [Translate]

Http://www.jianshu.com/p/84a8fd5fa0ee JavaScript Memory Management

http://www.cnblogs.com/rainman/archive/2009/03/07/1405624.html JavaScript memory leaks

http://www.blogjava.net/JAVA-HE/archive/2009/10/27/299856.html JavaScript Memory leaks

Http://www.cnblogs.com/lhb25/archive/2009/08/09/1542386.html memory leak mode in JavaScript

http://adamlu.com/?p=418 JavaScript memory leaks

JavaScript garbage Collection (iii)--memory leaks

Related Article

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.