Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript, DOM and garbage collection

Status
Not open for further replies.

farboo

Programmer
Mar 22, 2008
3
US
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:
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) {}
	}
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?


 
Sorry - a slight correction. The removeChild command in ObjectDelete() should read:
Code:
pObject.parentNode.removeChild ([b]pObject[/b]) ;
 
Thanks Adam. Yes, I do have some circulars but I've gone in and cleaned them out. The full code looks something like this:

Code:
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]) ;
        }
[COLOR=red]
    if (pObject._Manager)
        {
        pObject._Manager.Manages = null ;
        delete pObject._Manager.Manages  ; // Completely purge the attribute

        // then do the same on the DOMObject
        pObject._Manager = null ; 
        delete pObject._Manager ; 
        }
[/color]
    pObject.parentNode.removeChild (pObject) ;

    try    {
        delete pObject ;   
        //Try this too to see if it makes 
        //any difference...
        }
    catch (Error) {}
    }


I've spent the last few days benchmarking this some more and it looks like the deletion works, it just doesn't work for a given segment of memory in all cases. If I start with 30,000 K and build up 100 objects, then delete them all I see a spike of around 3,000 K added and then within around 10 seconds it drops down to 31,000 K. If I run through this 5 or six times it steadily increases by 1,000 K each time, but after around 35,000 K it levels off. Thereafter it stays in the 35,000 range after repeating the same test over and over again.

I started from scratch and just implemented the code with no circulars at all just to compare the results and saw the same gradual bleed up to the same plateau, so it looks like the circular references are being cleaned out, but that padded zone of 5,000 K remains. It looks like I may have to just live with it - as long as it levels out at some reasonable point it should be OK.

Thanks again for the references.
 
How many machines have you tried it on... you may just have some faulty ram.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top