views:

74

answers:

2

Hi all.

Is it possible to delete some images from the browser (firefox2) memory using javascript? By doing this, I want to save some precious browser memory and let my web app to work better.

I guess that if possible, it will be something like this:

delete (document.images[7]);


document.images[7].src = null;


document.images[7] = null;

Thanks guys!

+3  A: 

The most sensible thing would be to remove the Image element from the DOM.

var img = document.images[7]; img.parentNode.removeChild(img)

I don't know how much memory that is going to save you, if at all. The delete operator wouldn't do anything in this case because the HTMLCollection returned is ready-only.

Is there a particular reason you need to free up memory? Perhaps there are some memory leaks? Maybe this would help: http://www.ibm.com/developerworks/web/library/wa-memleak/.

jay_soo
A: 

You can't directly delete anything from the browser's memory cache. Such fine-grained access is not possible from Javascript. At best you can delete the image from the page being viewed by deleting any references to that particular image from the DOM tree.

You'd also have to delete it from another windows/tabs as well. At that point, if there's no more references to it, the browser's garbage collection MAY kick in and clean up, but you can't force the garbage collector to kick in. You could TRY by loading a bunch of other images/content, but then you're just wasting memory anyways.

Marc B