GetElementsByTagName Causes Memory Leaks

Hi Everyone,

I am having a problem with a memory issue in the compact framework. I have tracked the memory leak down to GetElementsByTagName in the xmldocument class. Everytime this method is called our application leaks more memory. Below is some sample code that will blowup if you leave it running on a device:

private XmlDocument oXMLDoc = new XmlDocument();

oXMLDoc.LoadXml("<RUBBISHDOC></RUBBISHDOC>");

do

{

GetElements();

}

while (true);

}

private void GetElements()

{

XmlNodeList olist = oXMLDoc.GetElementsByTagName("NoElementsWillMatch");

}

Basically all I am doing is creating an xmldoc with some rubbish element in it.

Then I am looping around looking for a tag name that doesn't exist (which returns an empty list as you would expect).

NOTE: This code isn't real code - it is a simple sample I created to demonstrate the issue...we go looking for elements that 'may' exist in our app which is quite large - we can make our app blow up after about 5 minutes.....so its a fairly critical issue for us.

I would expect for the above code to be able to run for ever since the xmlnodelist should go out of scope and the GC should clean it up.

Can anyone tell my why this runs out of memory (If you leave it running on the device you will get an outofmemory exception once the memory fills up (takes about 30 minutes depending on the device).

We tested using vs2005, C#, v2 of .net, intermec 700 ppc (but others do it also).

Any suggestions welcomed - note we are heavily reliant on xml so moving away from the xml dom would be a big issue for us.

Regards

Mike O'Leary



Answer this question

GetElementsByTagName Causes Memory Leaks

  • Praveen Dayanithi

    For those of you who want to continue to use GetElementsByTagName, try pushing it down into a function and calling that function. When the function returns, garbage collection works and clears the nasty memory leak. However, if you can't push it down into a function, then you're totally hosed. Or if you've got to pick up more than 50 elements/nodes within that function and then call another function with GetElementsByTagName in it, then you're doubly screwed because that compounds the memory leak. I'm not sure why this wasn't fixed. It's been around a long time and the GetElementsByTagName is a great way to gain access to just the nodes/elements you want to pound on. I've had to resort to writing my own node crawler in a few cases, but that sucks when something like GetElementsByTagName is already around. Oh well, apparently doctor watson doesn't capture this issue very well and no one is seeing the visibility of this statistic in the dev group.



  • Cristian Rosa

    Hi Mike , Mine is diifferent issue ... i nowhere use GetElementsByTagName ..but there is memory leak in my application.I wanted to know how you got bottom to memory leak...did u use any tool

    Thanks in advance.


  • Sam Jost

    Also please note ... I have tried forcing a garbage collection - this has no impact on the sample and it still runs out of memory.

     I have also just tried the CF 2 SP1 - the problem still exists in that release.

     


  • Jeroen Bransen - J-Thread

    Finally got to the bottom of this memory leak. Basically the issue is explained in the following article and is not specific to the CF - it also occurs on the desktop (even in apps like IE).

    Explanation of whats happening:

    http://blogs.msdn.com/eriksalt/archive/2005/07/20/GetElementsByTagName.aspx

    Bottom line is that if you are using .GetElementsByTagName (in the xmldoc) a lot then you will be leaking memory. Use .SelectNodes instead and the problem disappears.

    NOTE: I initially thought that selectnodes was also leaking memory but after further investigation it stabilizes are several uses and remains consistant...so that one gets the thumbs up.

    Regards

    Mike O'Leary


  • h1

    Hi Viral,

    I've tried setting :

    XmlNodeList olist = oXMLDoc.GetElementsByTagName("NoElementsWillMatch");

    olist = null;

    This has no impact - the application still runs out of memory when left to run.

    Does anyone have any other ideas


  • ChrisMcCabe

    Hello Mike,

    You are correct. It should run forever but i think the problem is that GC will not collect at the same time when it goes out of scope. We cannot predict when GC will run.

    According to me, what you can check for is assigning null explicitly after that statement in GetElements(...) . Just a guess.

    Regards,

    Viral


  • lms07424

    Hi Kripa,

    I just used testing processes to narrow down the area and then concentrated on the code in that area - not very scientific. However in .net 2 SP2 there is some good remote monitoring tools to check for memory leaks - download SP2 and take a look. Main areas to concentrate on from experience for memory leaks is XML, Bitmaps and SQLCE - if you are using any of them then look closely at their code as they are potential leak areas.

    Regards

    Mike


  • GetElementsByTagName Causes Memory Leaks