Thursday, February 03, 2005 - Posts

Forcing Garbage Collection on IE

The what?

I blogged a while back about a problem with an IE Memory leak with HTC's. The only solution on the MSDN ( Memory leak occurs when behaviors are dynamically removed ) was to change a setting in the Registry or force the page to reload.

The why?

Bascally the garbage collection was cleaning up th ehtc's until the page had been unloaded. And as far as I know there's know way of forcing IE to start garbage collection on the htc's.

This wasn't going to work as I didn't want to get all the users to fiddle with the registry and reloading the page was against what i was trying to achive which was seamless comunications between the client and the server.

The how?

The other day I thought... iFrames!!!! I could have a iframe object that I load of for heavy work with htc's that can then be unloaded, removed and rerendered.

 // Add Frame Function
function AddFrame(){
   oFrame = NewObj( "iFrame", {}, this, "" );
   oFrameDoc = window.frames[0].document;
   oFrameDoc.open();			
   oFrameDoc.write(GenStdHTML());
   NewObj( "script", {src:'cclgenlib.js'}, oFrameDoc, "", oFrameDoc  )
   oList = NewObj( "ccl:List", {}, oFrame.body, "", oFrameDoc  )
}

Add Frame renders a frame on the page and loads it the html from GenStdHTML. I had to add the script files hare as adding the js file in the GenStdHTML function cause the iframe to disapear! As you can see I am able to add another element to the iFrame (ccl:list) document this is another HTC control that is manulipated in the same way as if it was on the page contain g the iFrame.

 // Generate Base HTML for iFrame
function GenStdHTML(){
   var cHTML     = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
   cHTML        += '<html xmlns:ccl><head><title>CCL Widgets</title>';
   cHTML        += '<LINK href="style.css" type="text/css" rel="stylesheet">';
   cHTML        += '</head>';
   cHTML        += '<body></body></html>';
   return cHTML;
}
 // Remove Frame Function
function RemoveFrame(){		
   oFrameDoc.location = "blank";
   oFrameDoc = null;
   DelObj( oFrame, this );
   oFrame = null;
   AddFrame();
}

I set the location of the iFrame to "blank" so that it unloads and I then delete the iframe from the page.

 // Creates a new Element on screen
function NewObj( tag, a, p, t, d ){
   var o;
   if (!d){ d = document}
   o = d.createElement(tag);
   if (a)for (var att in a) o[att]=a[att];
   if (p) p.appendChild(o);
   if (t) o.appendChild(d.createTextNode(t));
   return o;
}

By doing this the memory usage on the application doesn't continually grow in the same way as it does normally.

Now playing: Pink Floyd - Knocking on Heaven's Door

with 4 Comments