views:

677

answers:

5

I have an AJAX-based website using JavaScript on the client. Certain operations on the site cache large result sets from service calls in the browser (i.e. hundreds of megabytes). These are throw-away results. They will be viewed for a short time and then need to be cleared from memory.

I've written a small test site that loads a bunch of junk in memory and then calls JavaScript's delete method. This works great in Firefox (memory almost instantly gets returned). Internet Explorer 8 (haven't tried 7) doesn't free the memory until the page is refreshed or closed.

Does anyone know how to drop IE's memory usage using JavaScript/Ajax (no page refreshes)?

Below is my sample client code:

function load() {
 var x = ['dfjasdlfkjsa;dflkjsad;flkjsadf;lj'];

 for( var i = 0; i < 10000000; ++i ) {
  x.push('asdfasfasfsfasdfkasjfslafkjslfjsalfjsaldfkjasl;dfkjsadfl;kjsdflskajflskfjslakfjaslfkjsaldfkjsaldfksdfjk');
 }

 alert('deleting');   // <--- memory usage around 500mb
 delete x;            // <--- immediate results in Firefox 3.5 (not IE8)
 alert('done');
}

UPDATE: Setting the variable to 'null' does not immediately clear the memory (as that is left up to the garbage collector). Also, setting a variable null only gets a single reference where there might be multiple references.

+1  A: 

Instead of using the delete method, set the variable to null. This seems to be the best way to clear up memory cross-browser (delete is notoriously flaky). It works the same as my other answer here.

alert('deleting');   // <--- memory usage around 500mb
x = null;
alert('done');
tj111
+1  A: 

And if you try "x= null;" instead of delete ?

patriiice
But what about the case where I have multiple variables pointing to the same object? Null will just terminate the single reference.
j0rd4n
@j0rdan when you have multiple references to an object, garbage collector will obviously not garbage collect that object until all of the references are seized to exist. `null`'ing all references to an object merely lets GC know that **it is available for garbage collection and can be wiped out next time GC runs**
kangax
+1  A: 

Just assign null to the variable:

x = null;
CMS
But what about the case where I have multiple variables pointing to the same object? Null will just terminate the single reference.
j0rd4n
Yes, you're right, if that's the case, since the delete operator doesn't works quite well on IE, you could nullify all the references you create to the object, that's the price of having a buggy garbage collector... if you can, encourage your users to use a good browser :-)
CMS
Amen to that brother...
j0rd4n
+1  A: 

I realize you mentioned the data is throw away, but, when you delete the nodes, even if just setting x to null, you need to make certain that no event handlers are attached to any of the nodes, otherwise that node cannot be garbage collected.

Here is a function I use in my own code: http://javascript.crockford.com/memory/leak.html

James Black
+1  A: 

IE (well, technically, JScript) has an undocumented CollectGarbage method, which supposedly forces garbage collector to run immediately. You might want to play with that, but from my experience, nulling references is enough most of the time.

kangax