I'm working on a page that dynamically loads objects over the lifetime of the session. It also cleans up after itself and deletes these objects when necessary. Here's the core code for the constructor and deleter:
The deletion code above works as expected - all of the children nodes appear to be deleted, then the parent object deletes itself. When I check in the DOM they are no longer there. However, when I pull up windows task manager and iterate a couple hundred/thousand times through creating then deleting objects on the page, I get a steady climb in memory use. This is true on all of the browsers I've tested in, including IE6/7, firefox, safari, avant and opera. I thought that garbage collection might take some time to complete, but after several minutes I may see a gradual change in memory, but it never gets back to the same range as a fresh page. I don't expect it to go back to the original memory size, but it's adding whole megabytes at a time and not deallocating most of it when I attempt to delete objects. Since the page is being designed for an AJAX-based interface it could be around for several hours and just keep loading up items ad hoc, so I've got to have a way of bringing memory use down as much as possible.
Does anyone have any suggestions on a more bullet proof way of purging DOM objects with javascript?
Code:
function ObjectCreate (pContent)
{
var oDiv = document.createElement ("DIV") ;
oDiv.style.position = "absolute" ;
document.body.appendChild (oDiv) ;
oDiv.innerHTML = pContent ;
return oDiv ;
}
function ObjectDelete (pObject)
{
while (pObject.childNodes.length)
{
// Make a recursive call on the
// children of this object all the
// way down the chain to delete
// everything in the same way
ObjectDelete (pObject.childNodes [pObject.childNodes.length-1]) ;
}
pObject.parentNode.removeChild (pDiv) ;
try {
delete pObject ;
//Try this too to see if it makes
//any difference...
}
catch (Error) {}
}
Does anyone have any suggestions on a more bullet proof way of purging DOM objects with javascript?