473,473 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 22871
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Uldis V. | last post by:
I get an unhandled exception, when I try to execute XmlDocument.Load(...) in my C# Windows application: -------------------------------------------- "Common Language Runtime Debugging Services"...
4
by: Foo | last post by:
Hi I have a problem, while retrieving xml data through network. I use Load(Stream) method for this, but this doesn't work: NetworkStream ns = client.GetStream(); StreamReader sreader = new...
2
by: Nikhil Patel | last post by:
Hi all, I use a Word Xml Template in ASP.Net application. Basically when a user clicks on a certain button on a web form, I open the template using XmlDocument.Load method; replace some strings in...
3
by: Dave Brown | last post by:
Hi All, Is it possible to examine the progress of a XMLDocument.Load method ? Sometimes my documents might be quite large so I want to give the user some indication of the size and progress. ...
10
by: lamxing | last post by:
Dear all, I've spent a long time to try to get the xmldocument.load method to handle UTF-8 characters, but no luck. Every time it loads a document contains european characters (such as the...
1
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
The description of the XMLDocument.Load method doesn't quite answer the question. When passing in a FileStream object to the Load method, does it load the entire document into memory? For...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.