473,729 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ 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(str CleanXMLPath);

//do some work here on the xmlDoc

XmlTextWriter wrtr = new XmlTextWriter(s trCleanXMLPathE ncoding.UTF8);
wrtr.Formatting = Formatting.Inde nted;
xmlDoc.WriteTo( wrtr);
wrtr.Close();
xmlDoc = null;
}

How do I get it to release the memory?
thanks!

Nov 12 '05 #1
13 22989
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 misunderstandin g, 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(XmlDocume nt xmlDoc = new XmlDocument())
{
xmlDoc.Load(str CleanXMLPath);

//do some work here on the xmlDoc

XmlTextWriter wrtr = new XmlTextWriter(s trCleanXMLPathE ncoding.UTF8);
wrtr.Formatting = Formatting.Inde nted;
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.Xml Document' to
'System.IDispos able'" 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 misunderstandin g, 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(XmlDocume nt xmlDoc = new XmlDocument())
{
xmlDoc.Load(str CleanXMLPath);

//do some work here on the xmlDoc

XmlTextWriter wrtr = new XmlTextWriter(s trCleanXMLPathE ncoding.UTF8);
wrtr.Formatting = Formatting.Inde nted;
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*********@Sa toriGroupInc.co m> wrote in message
news:uo******** ******@TK2MSFTN GP12.phx.gbl...
When I attempt to compile the code you suggested, I get the error that
"Cannot implicitly convert type 'System.Xml.Xml Document' to
'System.IDispos able'" 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 misunderstandin g, 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(XmlDocume nt xmlDoc = new XmlDocument())
{
xmlDoc.Load(str CleanXMLPath);

//do some work here on the xmlDoc

XmlTextWriter wrtr = new XmlTextWriter(s trCleanXMLPathE ncoding.UTF8);
wrtr.Formatting = Formatting.Inde nted;
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*********@Sa toriGroupInc.co m> wrote in message
news:#M******** *****@TK2MSFTNG P09.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1521
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" Process id... thread id... Click OK to terminate ... Click Cancel to Debug ... -------------------------------------------- Running environment:
4
2545
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 StreamReader(ns); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(sreader); Everything goes right while retrieving data to string and then loading to
2
6018
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 the document with user selected values; and save the document with a unique name generated at runtime. Everything works fine in the test environment. But before I deploy the application, I would like to ask one question - If two users...
3
1491
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. another question i have is re' datagrid i hope someone might be able to help with, say i have the xml <employee>
10
13973
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 one below, output from google map API), it always said invalid character at position 229, which I believe is the "ß" character. Can anyone point me to the right direction of how to load such documents using the xmldocument.load() method, or...
1
3081
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 example, if I have a 1 gigabyte file, would the Load method attempt read the entire 1 gigabyte file into memory when using a FileStream object?
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4528
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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 we have to send another system
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.