I've been encountering Javascript Out of Memory errors with a Web App that involves loading a Google Map and continuously panning from one point to another. It typically takes around half a day before memory is depleted, but I'm aiming for much longer durability. I located the memory leak to be linked to the usage of the map.panTo method, although I suspect my handling of it might be the root cause (JavaScript isn't my strong suit). Could you take a look at this code and assist me in resolving this memory leak issue? In order to expedite debugging time, I have increased the interval for panning in this demo code (the leak can even be observed through Windows Task Manager). The app runs on ASP .Net 3.5 Web Forms without any PostBacks; the code entirely consists of HTML, CSS, and JavaScript. The memory leakage is most severe in IE (which is necessary for display purposes).
Edit:
- Experimented with various versions of the Google Maps API (2, 3.0, and 3.6).
- Realized that nesting the map in a Hashtable or Array within the example code is redundant and doesn't mitigate the memory leak.
- Looking to evade temporary remedies such as periodic page refreshes or resorting to utilizing the static Google Maps API.
- The bounty will be awarded to whoever manages to rectify the memory leak or offers substantial evidence demonstrating why it's impossible to eliminate (if applicable).
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">
//Tried both HashTable and Array (Both Lead to Leakage)...
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);
//Attempted setCenter instead of the preferred panTo, yet still encountering leaks!
m_arrMaps["ShiftingMap"].panTo(objLatLong);
if (decLat == 20.5) {
//Leakage Persists...
ResetTimer(39, -87);
}
else {
//Leakage Endures...
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>