7

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.

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>

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

hhj
  • 501
  • 2
  • 8
  • 13
  • 1
    IE 6 had a very specific leak issue where, if you did not null out all the event handlers (onclick, onmousever, onload) on HTML elements you were creating and destroying dynamically, those references would never get garbage collected. That was fixed in later versions. So if you're experiencing this in IE6, that's likely your problem (though Google's code should be above this issue). Remember that the browser will run its garbage collector when it feels like it, and almost never when you expect it. Does memory usage drop if you wait? – Andrew Jun 07 '10 at 13:16
  • I am actually using IE 7, and the memory does not drop when waiting...how would I "null out all the event handlers" when I am simply attaching it to ? – hhj Jun 07 '10 at 13:23
  • 1
    If you're using IE 7, this issue shouldn't affect you. The problem before was if you had any elements that had events tied to them (if the map has drag-and-drop functionality, then it does) -- you'd have to loop through all the elements that had event handlers and say `document.getElementById('mydiv').onclick=null;` etc. But you're completely right. In the case of your code above, there's nothing wrong you're doing that I can see. It must be an issue with the Google code -- the worst answer I can possibly give. – Andrew Jun 07 '10 at 15:16

4 Answers4

7

Update:

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.

Update 2:

I think this is the bug: http://code.google.com/p/gmaps-api-issues/issues/detail?id=1555&can=1&q=unload&colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Internal%20Stars

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
powtac
  • 40,542
  • 28
  • 115
  • 170
  • what leads you to believe that the linked gmaps api issue is the problem? it doesn't seem to have much to do with IE memory leaks. We're encountering the same issue in IE7, it seems to be a showstopper for that browser... – matao Aug 30 '11 at 00:14
  • @matao the tests with drip are very meaningful – powtac Aug 30 '11 at 09:41
  • @matao, as I said in my Update section, drip shows that the mem consumption went up while refreshing and since there is no other code than the g maps, the leak has to be somewhere there. I'm sure in the meanwhile this problem was solved by g, or the suggestion of Andrew according to the docs should be used: `onunload="GUnload()"` – powtac Aug 30 '11 at 11:01
  • ah, yes that's the same issue we're encountering, memory usage seems to increase by ~10MB with every refresh of a page containing g maps, even the simple examples. This is with latest v3 in IE7, so I think they just left it in the too-hard basket. our recom to client is restart browser periodically, or use Firefox :-( – matao Sep 01 '11 at 05:22
  • we're using v3 of the api, I thought GUnload was deprecated for v3? but we'll give it a try, why not? Just curious as the same behavior is observed on the google maps homepage, and if it isn't solved there i don't like our chances... – matao Sep 02 '11 at 07:21
1

One well-known source of IE memory leaks is the (deliberate or accidental) trapping of Javascript "stuff" in closures (functions) bound as event handlers to DOM elements. Most frameworks try hard to clean out event handlers explicitly for that reason.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

You need to also execute GUnload before leaving the page. Simply add an "unload" event:

<body onload="initialize()" onunload="GUnload()">

Read more about this leak at the Google Maps API

Andrew
  • 14,204
  • 15
  • 60
  • 104
0

Most of the answers so far will help you get there so i am adding a link to an article on IBM Dev Works for Memory leak patterns in JavaScript (you might find it helpful)

http://www.ibm.com/developerworks/web/library/wa-memleak/

Andreas
  • 5,305
  • 4
  • 41
  • 60