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:
- Usage Memory use
- Delta memory increase or decrease. Green is memory reduction, red is memory increase
- #inUse HTML nodes in use, such as Div
- #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