I have been experiencing Javascript Out of Memory errors with a Web App that loads a Google Map and continuously pans from one point to the other. It takes about a half day before it runs out of memory, but I would really like it to last far longer. I have identified that the memory leak occurs when the map.panTo method is used, although I very much suspect that it could be the way I'm using it (Javascript is not my strong suit). Can you please look at this code and help me fix this memory leak? I have sped up the interval that it pans in this demo code for sake of time during debugging (the leak is obvious even by checking the process in Windows Task Manager). This runs off of ASP .Net 3.5 Web Forms, but there are no PostBacks and the code is completely HTML, CSS, and Javascript. It leaks the worst in IE (which is what I need to use to display this).
Edit:
- I have tried using different versions of the Google Maps API (2, 3.0, and 3.6).
- I know that I don't need to put the map in the example code into a Hashtable or Array, but that doesn't affect the memory leak anyways.
- I'm trying to avoid a hack, such as refreshing the page every so often or using the static Google Maps API.
- Bounty will go to whoever figures out how to fix the memory leak or whoever clearly identifies and proves why it can't be fixed (if that is the case).
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Map Shifter</title>
<style type="text/css">
#divMap { height: 400px; width:400px; position:absolute; top:10%; left:10%; border: 2px solid #ff1100; }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=2&sensor=false"></script>
<script type="text/javascript" language="javascript">
//Have tried both HashTable and Array (Both Leak)...
var m_arrMaps = new HashTable(); //new Array();
var m_Timer;
function HashTable() {
return this;
}
function LoadMapAndStartShifting() {
var objMapCenterPoint = new google.maps.LatLng(20.5, -156);
var objMapOptions = { zoom: 9, center: objMapCenterPoint, mapTypeId: google.maps.MapTypeId.ROADMAP,
scaleControl: false, streetViewControl:false, zoomControl: false, mapTypeControl: false, panControl: false
}
var map = new google.maps.Map(document.getElementById("divMap"), objMapOptions);
m_arrMaps["ShiftingMap"] = map;
setTimeout("ShiftMap(20.5, -156);", 700);
}
function ShiftMap(decLat, decLng) {
var objLatLong = new google.maps.LatLng(decLat, decLng);
//Have tried setCenter instead of preferred panTo and it still leaks!
m_arrMaps["ShiftingMap"].panTo(objLatLong);
if (decLat == 20.5) {
//Leaks...
ResetTimer(39, -87);
}
else {
//Still leaks...
setTimeout("ShiftMap(20.5, -156);", 700);
}
}
function ResetTimer(decLat, decLng) {
m_Timer = setTimeout("ShiftMap(" + decLat + ", " + decLng + ");", 700);
}
</script>
</head>
<body onload="LoadMapAndStartShifting();">
<form id="form1" runat="server">
<div id="divMap"></div>
</form>
</body>