views:

725

answers:

2

i've written a gadget for Windows Sidebar. This essentially means that it is a miniature web-page, that runs for months on end.

After a few weeks the memory usage (working set) of the sidebar.exe process that houses 3rd party gadgets runs into the hundreds of megabytes.

Without a way to identify the source of memory leaks, i simply assume it is the rumored XMLHttpRequest closure problem. Although in my case i'm not doing it asynchronously. So i guess it's just JAX rather than AJAX.

The javascript function involving the web hit:

function FetchXML(method, url)
{
   var xmlHttp;
   try
   {
      // Firefox, Opera 8.0+, Safari  
      xmlHttp=new XMLHttpRequest();  
   }
   catch (e)
   {  // Internet Explorer  
      try
      {
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
      }
      catch (e)
       {
         try
         {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
         }
         catch (e)
         {
            throw "XMLHttp not supported"
         }
      }
   }

   xmlHttp.open(method, url, false);
   xmlHttp.send(null);  
   if (xmlHttp.status != 200)
   {
      throw "Server returned status code "+xmlHttp.status.toString();
   }

   if (xmlHttp.responseXML.parseError.errorCode != 0)
   {
      throw "Error in returned XML: "+xmlHttp.responseXML.parseError.reason;
   }

   var responseXML = xmlHttp.responseXML;
   xmlHttp = null;
   return responseXML;
}

Does this look like it could ever be the source of a memory leak?


i fear that without an actual closure i'm back to square one.

A: 

This is a bit of a late answer but I noticed this had gone unanswered. Looking at your code, you're running synchronously and there are no circular references. I doubt that is the source of the memory leak and it's likely to be somewhere else in your code. I've come across memory leaks in Windows Desktop Gadgets before and the biggest one I've found is when dynamically adding script tags to the document (for instance, when using JSON callback methods from a web service).

Incidentally, the browser checks you're running are almost completely redundant. IE7, the lowest version of IE allowable on Vista, introduced the XMLHttpRequest() object (although it can be disabled by a user or system administrator). I would recommend just using the following single line to replace it:

xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
Andy E
A: 

Also, DOM objects and JavaScript objects live in different memory spaces, so if you have circular references like

  table = [];
  table[0] = document.getElementById('myDiv');
  table[0].ownerTable = table;

then neither the array nor the div will ever get garbage collected, even if all other references to the two objects have gone out of scope.

Mike Stay