1

Im having trouble with my javascript application leaking memory. I've looked up things online and I seem to have closed out any memory alocation in the setInterval loop by using the

variableX = null

technique. But the application is still leaking memory somehow, can anyone point out what might be causing this leak? Here is my code :

<!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
 <meta charset="utf-8">
 <title>Simple markers</title>
 <style>
 html, body, #map-canvas {
 height: 100%;
 margin: 0px;
 padding: 0px
 }
 </style>
 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
 <script>
 function initialize() {
 var myLatlng = new google.maps.LatLng(43.46949, -80.54661);
 var lat = 0;
 var long = 0;
 var mapOptions = {
 zoom: 16,
 center: myLatlng,
 mapTypeId: google.maps.MapTypeId.ROADMAP
 }
 function loadXMLDoc() {
 var xhr;
 if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
 xhr = new XMLHttpRequest();
 } else { // code for IE6, IE5
 xhr = new ActiveXObject("Microsoft.XMLHTTP");
 }
 xhr.open("GET", "coordinates.txt", true);
 xhr.onreadystatechange = function () {
 if (this.readyState == 4) {
 var textfileString = this.responseText;
 var longLatString = textfileString.split(',');
 lat = parseFloat(longLatString[0])
 long = parseFloat(longLatString[1])
 textfileString = null
 longLatString = null
 }
 }
 xhr.send();
 xhr = null
 }
 var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
 var marker = new google.maps.Marker({
 position: myLatlng,
 map: map,
 title: 'Hello World!'
 });
 setInterval(function () {
 loadXMLDoc()
 var newLatLng = new google.maps.LatLng(lat, long);
 marker.setPosition(newLatLng);
 newLatLng = null
 }, 16);
 }
 google.maps.event.addDomListener(window, 'load', initialize);
 </script>
</head>
<body>
 <div id="map-canvas"></div>
</body>
</html>
cookie monster
11k4 gold badges34 silver badges45 bronze badges
asked Jan 18, 2014 at 22:47
12
  • What is the evidence that it is leaking memory? Commented Jan 18, 2014 at 22:49
  • Running this in a browser within my application, and when the application is left running it shows memory continuously increasing in task manager and if left for a very long time the program will eventually crash. Commented Jan 18, 2014 at 22:52
  • 2
    Wait... you're making a new XHR request every 16 milliseconds? Commented Jan 18, 2014 at 22:55
  • I'm using the Webkit browser for C# winforms found here: webkitdotnet.sourceforge.net Commented Jan 18, 2014 at 22:57
  • ...and FYI, your lat and long are not going to be received yet from the XHR request when the var newLatLng = new google.maps... runs Commented Jan 18, 2014 at 22:58

1 Answer 1

3

You're sending a new XMLHttpRequest without waiting for the old one to complete. If the response takes longer than 16 milliseconds to be received (which it probably will), the "request queue" will grow, causing a memory leak.

Sending requests nonstop is a bad idea. If you need a constant stream of coordinates, pack multiple coordinates into one response and send a new request only when your previous one has completed:

function getCoordinates() {
 ...
 xhr.onreadystatechange = function() {
 ...
 setTimeout(getCoordinates, 30000);
 }
}

Better yet, use Websockets.

answered Jan 18, 2014 at 23:08
Sign up to request clarification or add additional context in comments.

3 Comments

May not be a bad idea to clarify that the setTimeout should be behind the readystate==4 condition. You do imply it in your first sentence though.
I assumed 16ms would be enough time to process the majority of requests because its just pulling information from a local file? Also doing it this way defeats the purpose of the app, its supposed to be updating the user realtime of gps coordinates being sent, having position updates delayed 30 seconds isn't acceptable in this application.
@user1296932: Then use Websockets. HTTP requests aren't "realtime".

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.