IE,firefox内存溢出原因与解决方法

来源:互联网 发布:便签的元数据已损坏 编辑:程序博客网 时间:2024/06/05 11:37

JavaScript 中的内存泄漏
JavaScript 是一种垃圾收集式语言,这就是说,内存是根据对象的创建分配给该对象的,并会在没有对该对象的引用时由浏览器收回。JavaScript 的垃圾收集机制本身并没有问题,但浏览器在为 DOM 对象分配和恢复内存的方式上却有些出入。
Internet Explorer 和 Mozilla Firefox 均使用引用计数来为 DOM 对象处理内存。在引用计数系统,每个所引用的对象都会保留一个计数,以获悉有多少对象正在引用它。如果计数为零,该对象就会被销毁,其占用的内存也会返回给堆。虽然这种解决方案总的来说还算有效,但在循环引用方面却存在一些盲点。

原因

1)循环引用导致了内存泄漏

[xhtml] view plain copy
  1. <html>  
  2. <body>  
  3. <mce:script type="text/javascript"><!--  
  4. document.write("circular references between JavaScript and DOM!");  
  5. var obj;  
  6. window.onload = function(){  
  7.     obj=document.getElementById("DivElement");  
  8. document.getElementById("DivElement").expandoProperty=obj;  
  9.     obj.bigString=new Array(1000).join(new Array(2000).join("XXXXX"));  
  10. };  
  11. // --></mce:script>  
  12. <div id="DivElement">Div Element</div>  
  13. </body>  
  14. </html>  


2)由外部函数调用引起的内存泄漏
[xhtml] view plain copy
  1. <html>  
  2. <head>  
  3. <mce:script type="text/javascript"><!--  
  4. document.write(" object s between JavaScript and DOM!");  
  5. function myFunction(element)  
  6. {  
  7.     this.elementReference = element;  
  8.     // This code forms a circular reference here  
  9.     //by DOM-->JS-->DOM  
  10.     element.expandoProperty = this;  
  11. }  
  12. function Leak() {  
  13.     //This code will leak  
  14.     new myFunction(document.getElementById("myDiv"));  
  15. }  
  16. // --></mce:script>  
  17. </head>  
  18. <body onload="Leak()">  
  19. <div id="myDiv"></div>  
  20. </body>  
  21. </html>  


3)闭包引起的内存泄漏

[javascript] view plain copy
  1. function parentFunction(paramA){  
  2.     var a = paramA;  
  3.     function childFunction(){  
  4.         return a + 2;  
  5.     }  
  6.     return childFunction();  
  7. }  


4)由事件处理引起的内存泄漏模式
[xhtml] view plain copy
  1. <html>  
  2. <body>  
  3. <mce:script type="text/javascript"><!--  
  4. document.write("Program to illustrate memory leak via closure");  
  5. window.onload=function outerFunction(){  
  6.     var obj = document.getElementById("element");  
  7.     obj.onclick=function innerFunction(){  
  8.     alert("Hi! I will leak");  
  9.     };  
  10.     obj.bigString=new Array(1000).join(new Array(2000).join("XXXXX"));  
  11.     // This is used to make the leak significant  
  12. };  
  13. // --></mce:script>  
  14. <button id="element">Click Me</button>  
  15. </body>  
  16. </html>  


解决方法
1)打破循环引用
[xhtml] view plain copy
  1. <html>  
  2. <body>  
  3. <mce:script type="text/javascript"><!--  
  4. document.write("Avoiding memory leak via closure by breaking the circular reference");  
  5.     window.onload=function outerFunction(){  
  6.     var obj = document.getElementById("element");  
  7.     obj.onclick=function innerFunction()  
  8.     {  
  9.         alert("Hi! I have avoided the leak");  
  10.         // Some logic here  
  11.     };  
  12.     obj.bigString=new Array(1000).join(new Array(2000).join("XXXXX"));  
  13.     obj = null; //This breaks the circular reference  
  14.     };  
  15. // --></mce:script>  
  16. <button id="element">"Click Here"</button>  
  17. </body>  
  18. </html>  

 


2)添加另一个闭包

[xhtml] view plain copy
  1. <html>  
  2. <body>  
  3. <mce:script type="text/javascript"><!--  
  4. document.write("Avoiding a memory leak by adding another closure");  
  5. window.onload=function outerFunction(){  
  6.     var anotherObj = function innerFunction(){  
  7.         // Some logic here  
  8.         alert("Hi! I have avoided the leak");  
  9.     };  
  10.     (function anotherInnerFunction(){  
  11.         var obj =  document.getElementById("element");  
  12.         obj.onclick=anotherObj  
  13.     })();  
  14. };  
  15. // --></mce:script>  
  16. <button id="element">"Click Here"</button>  
  17. </body>  
  18. </html>  


3)避免闭包自身
[xhtml] view plain copy
  1. <html>  
  2. <head>  
  3. <mce:script type="text/javascript"><!--  
  4. document.write("Avoid leaks by avoiding closures!");  
  5. window.onload=function(){  
  6.     var obj = document.getElementById("element");  
  7.     obj.onclick = doesNotLeak;  
  8. }  
  9. function doesNotLeak(){  
  10.     //Your Logic here  
  11.     alert("Hi! I have avoided the leak");  
  12. }  
  13. // --></mce:script>  
  14. </head>  
  15. <body>  
  16. <button id="element">"Click Here"</button>  
  17. </body>  
  18. </html>  


4)考虑用CollectGarbage()
[javascript] view plain copy
  1. jcl.MemFree = function(Mem){  
  2.     Mem = null;  
  3.     CollectGarbage();  
  4. };  


检测软件
sIEve: 他是基于ie的内存泄露检测工具,需要下载运行,http://home.wanadoo.nl/jsrosman/

Leak Monitor: 他是基于firefox的内存泄露检测工具,https://addons.mozilla.org/firefox/2490/

个人建议

内存回收机制本身有问题,所以开发人员开发的时候尽量减少内存溢出。不要盲目的追求完美!


来源网址:http://blog.csdn.net/spring21st/article/details/5658309

原创粉丝点击