472,145 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

XMLDocument Load method not releasing memory

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!

Nov 12 '05 #1
13 22532
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."

Nov 12 '05 #2
Matthew Wieder wrote:
xmlDoc = null;
}

How do I get it to release the memory?


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]
http://blog.tkachenko.com
Nov 12 '05 #3
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

Kevin Yu [MSFT] wrote:
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."


Nov 12 '05 #4
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
"Matthew Wieder" <De*********@SatoriGroupInc.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
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

Kevin Yu [MSFT] wrote:
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."

---
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
Nov 12 '05 #5
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.

Daniel Cazzulino [MVP XML] wrote:
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"?


Nov 12 '05 #6
Matthew Wieder wrote:
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.


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/downlo...0profiler.exe).
--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #7
Just to confirm it's an issue on my end, please run with an xml file
over 10 MB.
thanks!

Oleg Tkachenko [MVP] wrote:
Matthew Wieder wrote:
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.

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/downlo...0profiler.exe).


Nov 12 '05 #8
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

"Matthew Wieder" <De*********@SatoriGroupInc.com> wrote in message
news:#M*************@TK2MSFTNGP09.phx.gbl...
Just to confirm it's an issue on my end, please run with an xml file
over 10 MB.
thanks!

Oleg Tkachenko [MVP] wrote:
Matthew Wieder wrote:
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.

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/downlo...0profiler.exe).

---
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
Nov 12 '05 #9
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."

Nov 12 '05 #10
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! (ki*****@hotmail.com)
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #11
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! (ki*****@hotmail.com)
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #12
Kay Dee wrote:
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.


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

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #13
Kay Dee wrote:
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.


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

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Uldis V. | last post: by
2 posts views Thread by Nikhil Patel | last post: by
3 posts views Thread by Dave Brown | last post: by
1 post views Thread by =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.