本文介绍了javascript内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些javascript(与谷歌地图api一起使用),我正在IE和Chrome上测试,并且只注意到IE中的内存泄漏症状:当我连续刷新页面时,IE中使用的内存量不断增长(快速) ),但在Chrome中它保持不变。如果没有发布所有代码(因为它很长),我可以得到一些关于要注意什么的建议吗?什么可能导致内存在页面刷新时在IE中保持这样的增长?

I have a some javascript (used with google maps api) that I am testing on IE and Chrome and noticed memory leak symptoms in IE only: when I refresh the page continuously, the amount of memory used in IE keeps growing (fast), but in Chrome it stays constant. Without posting all of the code (as it is rather long), can I get some suggestions as to what to look out for? What could cause the memory to keep growing like this in IE on page refreshes?

就像我说我知道它没有代码很难,但我想看看是否有任何代码通用建议首先起作用。谢谢。

Like I said I know its hard without code, but I'd like to see if any generic advice works first. Thanks.

更新:感谢目前为止的回复。作为一个完整性检查,我运行了谷歌地图apiHello World代码看看IE中会发生什么(代码如下所示)。在IE中运行此代码时,当我一遍又一遍地刷新页面时,内存会不断增长和增长。这是内存泄漏吗?这似乎不是预期的功能......

Update: thanks for the responses so far. As a sanity check, I ran the google maps api "Hello World" code from google to see what would happen in IE (the code is shown below). When running this code in IE, when I keep refreshing the page over and over again, the memory keeps growing and growing. Is this a memory leak? This doesnt seem like intended functionality...

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }

</script>
</head>
<body onload="initialize()">
  <div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>

更新2 :所以没有办法获得这个谷歌Hello World映射api代码在IE中运行而不泄漏内存?我注意到,如果我在上运行相同的实验,则似乎没有泄漏。 ..如果有人可以帮我修改你好的世界代码以便它不会在IE中泄漏,那将会很棒。这样我可以构建它(我不介意使用JQuery,如果这会有所帮助,但我在Hello World代码上尝试了它,它仍然在IE中泄漏)。再次感谢

Update 2: So is there no way to get this google Hello World map api code to run without leaking memory in IE? I noticed that if I run the same experiment on maps.google.com there doesn't seem to be a leak...It would be great if someone could help me modify the hello world code so that it does not leak in IE; this way I can build off of it (I don't mind using JQuery if this would help, but I tried it on the Hello World code and it was still leaking in IE). Thanks again

推荐答案


  • 使用测试您的代码,它为您的代码中的问题提供了很好的建议。

  • 还有一个! (它搬到了,关于该工具的其他下载和描述名为drip:)

  • 还有一篇。

  • jQuery有一些内置机制来避免内存泄漏。有很多关于这个问题的文件。


  • 提醒:谷歌宣布他们将放弃对IE6的支持。

  • (2013年7月更新)

    • Use JSLint.com to test your code it gives great recommendations about probably problems in your code.
    • There is also a memory leak detector by Microsoft! (It moved here http://www.outofhanwell.com/ieleak/index.php?title=Main_Page , other download and description about the tool called drip: http://www.smallworkarounds.net/2009/04/jquery-leaking-memory-be-careful-while.html)
    • There is also an article by Corckford about mem leaks.
    • jQuery has some build in mechanisms to avoid memory leaks. There are a lot of documentations out there about this problems.
    • Other memory detector by Microsoft
    • Remind: Google announced that they will drop support for IE6.
    • Google Chrome has build in memory profiling (Update on July 2013)
    • 更新:

      我用似乎存在类似内存泄漏的问题。使用自动刷新运行代码几分钟后,内存使用量稳步上升。

      I tested the code above with drip.exe and it seems that there is really something like a memory leak. The memory usage went steadily up while running the code with auto refreshing for some minutes.

      更新2:

      我认为这是错误:

      这篇关于javascript内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 09:21
查看更多