Groups | Blog | Home
all groups > dotnet xml > may 2004 >

dotnet xml : XMLDocument Load method not releasing memory


Matthew Wieder
5/7/2004 3:28:50 PM
In my C# application, I have class which has method that opens an XML
document, modifies it and saves it out. I run that method for several
different XML documents. What I've found is that the Load emthod on the
MXLDocument isntance loads the document into memory (as it should) but I
have no way of releaseing that memory throughout the application, and I
run out of memory. The documents I'm loading are abou 10 MB in size,
and I run out of memory before 50 of them are loaded.
Here's the code for the method:
public void Clean(string strCleanXMLPath)
{
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(strCleanXMLPath);

//do some work here on the xmlDoc

XmlTextWriter wrtr = new XmlTextWriter(strCleanXMLPathEncoding.UTF8);
wrtr.Formatting = Formatting.Indented;
xmlDoc.WriteTo(wrtr);
wrtr.Close();
xmlDoc = null;
}

How do I get it to release the memory?
thanks!
v-kevy NO[at]SPAM online.microsoft.com
5/8/2004 2:48:48 AM
Hi

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that memory of the XmlDocument was not
released after using in your app. If there is any misunderstanding, please
feel free to let me know.

As far as I know, the memory has to be released automatically when the
object reference is out of scope. The Garbage Collection of .NET
framework's CLR will do this for us. If the memeory is not released, please
try to use the using statement in C#, so that we can make sure that the
memeory is enforced to be released when out of scope. Here is an example:

public void Clean(string strCleanXMLPath)
{
using(XmlDocument xmlDoc = new XmlDocument())
{
xmlDoc.Load(strCleanXMLPath);

//do some work here on the xmlDoc

XmlTextWriter wrtr = new XmlTextWriter(strCleanXMLPathEncoding.UTF8);
wrtr.Formatting = Formatting.Indented;
xmlDoc.WriteTo(wrtr);
wrtr.Close();
xmlDoc = null;
}
}


HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Oleg Tkachenko [MVP]
5/9/2004 10:01:02 AM
[quoted text, click to view]

Well, basically GC should take care if it's out of scope or nulled. Make
sure you don't have some live reference to the XmlDocument or its nodes
elsewhere. You can try some .NET profiler tool too.
--
Oleg Tkachenko [XML MVP]
Matthew Wieder
5/10/2004 9:20:12 AM
When I attempt to compile the code you suggested, I get the error that
"Cannot implicitly convert type 'System.Xml.XmlDocument' to
'System.IDisposable'" If XmlDocument does not implement IDisposable,
then perhaps there is a larger issue here that the XmlDocument never
goes out of scope?
thanks,
-Matthew

[quoted text, click to view]
Daniel Cazzulino [MVP XML]
5/10/2004 6:15:46 PM
Any variable goes out of scope if you don't keep references to it (for
example in some global cache).
IDisposable is not implemented in the XmlDocument as its resources are pure
..NET object and references that are automatically freed by the garbage
collector.
You don't even need to set it to null before exiting the method.
Can you show us the full code that "leaks"?

--
Daniel Cazzulino [MVP XML]
Clarius Consulting SA
http://weblogs.asp.net/cazzu
http://aspnet2.com
[quoted text, click to view]


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.679 / Virus Database: 441 - Release Date: 07/05/2004

Matthew Wieder
5/11/2004 9:09:33 AM
I provided the code in my original post. Just call that method 100
times with a 10 MB xml file and watch the memory grow.

[quoted text, click to view]
Matthew Wieder
5/11/2004 1:27:28 PM
Just to confirm it's an issue on my end, please run with an xml file
over 10 MB.
thanks!

[quoted text, click to view]
Daniel Cazzulino [MVP XML]
5/11/2004 5:16:34 PM
Here (http://aspnet2.com/kzu/weblog/verano.zip) you have a real 150Mb XML
file compressed in 6Mb. I use it for testing all the time.

--
Daniel Cazzulino [MVP XML]
Clarius Consulting SA
http://weblogs.asp.net/cazzu
http://aspnet2.com

[quoted text, click to view]


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.679 / Virus Database: 441 - Release Date: 08/05/2004

Oleg Tkachenko [MVP]
5/11/2004 8:12:32 PM
[quoted text, click to view]

Well, I just run that code 100 times with 4 Mb xml file under really
heavy load. No memory problems. In fact I'd be surprised to see memory
leak in such simple and common code.
You may want to try some .NET profiler to see what's going on (there are
lots of such tools available including freeware, e.g. take a look at
http://download.microsoft.com/download/4/4/2/442d67c7-a1c1-4884-9715-803a7b485b82/clr%20profiler.exe).
--
Oleg Tkachenko [XML MVP]
v-kevy NO[at]SPAM online.microsoft.com
5/12/2004 6:11:26 AM
Hi Matthew,

I have tried the 155Mb Xml file that Oleg has provided on my machine.
However, I cannot repro this issue. So I think this might be machine
specific.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Kay Dee
6/16/2004 3:04:54 PM
I am also having a similar problem. My memory accumulates and does not
get garbage collected. If I add System.GC.Collect() though, my memory
accumulation stops. This does not seem right though. If anyone has any
help, it is greated appreciated.

Thanks! (kit_mx1@hotmail.com)


*** Sent via Devdex http://www.devdex.com ***
Oleg Tkachenko [MVP]
6/17/2004 10:43:03 AM
[quoted text, click to view]

That's how GC works. The collection process doesn't run if you have
enough memory available.

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button