PC Review


Reply
Thread Tools Rate Thread

Big Memory Leak in XmlDocument.Load("filename");

 
 
bleedledeep
Guest
Posts: n/a
 
      17th May 2005
I've been tracking down a memory leak using DevPartner 7.2 and what I
am seeing is that calling XmlDocument.Load() leaks A LOT of memory.

The following code is called when I click a button on my test form.
(this is test code I wrote to confirm the bug, not my actual code)


private void button1_Click(object sender, System.EventArgs e)
{
XmlDocument xd = new XmlDocument();
for ( int i = 0; i < 10; i++ )
{
xd.Load( "test.xml" ) ;
Thread.Sleep( 1000 );
}
}

"test.xml" is 8525 bytes.

Each time xd.Load( "test.xml" ) is called, approx 114,000 bytes are
leaked. When I force GC with the DevPartner GUI, all but one of those
are collected, but I am still out 114,000 bytes. I have tried putting
System.GC.Collect() after the Sleep call, but the behavior does not
change.

I am using 2003, does 2005 fix this? Is there a patch? A work around?

Any info greatly appreciated.

 
Reply With Quote
 
 
 
 
BertC
Guest
Posts: n/a
 
      17th May 2005
This might be a silly question, but are you setting xd to nothing
before calling the GC? Otherwise there will still be an active
reference, and the framework won't release the memory.

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      17th May 2005

"bleedledeep" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I've been tracking down a memory leak using DevPartner 7.2 and what I
> am seeing is that calling XmlDocument.Load() leaks A LOT of memory.
>
> The following code is called when I click a button on my test form.
> (this is test code I wrote to confirm the bug, not my actual code)
>
>
> private void button1_Click(object sender, System.EventArgs e)
> {
> XmlDocument xd = new XmlDocument();
> for ( int i = 0; i < 10; i++ )
> {
> xd.Load( "test.xml" ) ;
> Thread.Sleep( 1000 );
> }
> }
>
> "test.xml" is 8525 bytes.
>
> Each time xd.Load( "test.xml" ) is called, approx 114,000 bytes are
> leaked. When I force GC with the DevPartner GUI, all but one of those
> are collected, but I am still out 114,000 bytes. I have tried putting
> System.GC.Collect() after the Sleep call, but the behavior does not
> change.
>
> I am using 2003, does 2005 fix this? Is there a patch? A work around?
>
> Any info greatly appreciated.
>


They are not leaked, the GC did not yet recover them.
The GC will collect the unreferenced objects when HE decides it's time to do
so, calling GC.Collect() to force a collection is in general a very bad
idea.

Willy.


 
Reply With Quote
 
bleedledeep
Guest
Posts: n/a
 
      17th May 2005
I would like to think that calling Load() again would cause the xd to
clear out the old stuff and have only references to the new stuff, but,
that is an interesting thing to try:

If I do the following, then the 114,000s don't pile up, but I still
never get rid of the last one.

for ( int i = 0; i < 10; i++ )
{
XmlDocument xd = new XmlDocument();
xd.Load( "test.xml" ) ;
Thread.Sleep( 1000 );
xd = null;
System.GC.Collect();
}


Interesting to note that just calling XmlDocument xd = new
XmlDocument(); seems to leak memory, but it is a fixed 592 bytes -
doesn't matter how many times you call it.

 
Reply With Quote
 
bleedledeep
Guest
Posts: n/a
 
      17th May 2005
Oh, I agree that calling the System.GC.Collect() is bad, and it doesn't
really do it when you ask, but I threw it in there out of curiosity to
try and figure this out, I would never do that in real code.

Forcing the GC from inside the DevPartner does cause an immediate GC,
that is useful as well. That last 114,000 is never recovered.

The problem I have is that in my "real" app, I create 10-20 objects
that suffer this leak, and 114,000 * 20 == ouch.

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      17th May 2005

"bleedledeep" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I would like to think that calling Load() again would cause the xd to
> clear out the old stuff and have only references to the new stuff, but,
> that is an interesting thing to try:
>
> If I do the following, then the 114,000s don't pile up, but I still
> never get rid of the last one.
>
> for ( int i = 0; i < 10; i++ )
> {
> XmlDocument xd = new XmlDocument();
> xd.Load( "test.xml" ) ;
> Thread.Sleep( 1000 );
> xd = null;
> System.GC.Collect();
> }
>
>
> Interesting to note that just calling XmlDocument xd = new
> XmlDocument(); seems to leak memory, but it is a fixed 592 bytes -
> doesn't matter how many times you call it.
>


As I said in another response, this is a BAD thing to do, all you do here is
disturbing the normal working of the GC. This is not a memory leak, you
should definitely read something about the Garbage collection in .NET.

Willy.




 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      17th May 2005

"bleedledeep" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Oh, I agree that calling the System.GC.Collect() is bad, and it doesn't
> really do it when you ask, but I threw it in there out of curiosity to
> try and figure this out, I would never do that in real code.
>
> Forcing the GC from inside the DevPartner does cause an immediate GC,
> that is useful as well. That last 114,000 is never recovered.
>

Which is NOT TRUE, they will be recovered when the GC runs, another remark
is that you shouldn't use tools like profilers unless you have a real leak
(that is out of memory exceptions after x execution time).


> The problem I have is that in my "real" app, I create 10-20 objects
> that suffer this leak, and 114,000 * 20 == ouch.
>

Again, this is not a leak, learn the difference between normal behavior and
memory leaks.

Willy.


 
Reply With Quote
 
bleedledeep
Guest
Posts: n/a
 
      17th May 2005
It will get to out of memory if I create enough of these objects. The
real app is a service that runs for weeks at a time, you can watch it
accumulate memory using task manager. Its memory size should not grow
over the long run. It might over the short-term since, as you said,
the GC runs on its own schedule.

It was only after noticing this behavior that I started to profile it
to see where all that memory was going.

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      17th May 2005

"bleedledeep" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> It will get to out of memory if I create enough of these objects. The
> real app is a service that runs for weeks at a time, you can watch it
> accumulate memory using task manager. Its memory size should not grow
> over the long run. It might over the short-term since, as you said,
> the GC runs on its own schedule.
>
> It was only after noticing this behavior that I started to profile it
> to see where all that memory was going.
>


Noticing what behavior, out of memory exceptions being thrown? I don't
believe it, so please post a small sample that illustrates the issue. And if
it's a real leak GC.Collect() won't help anyway.


Willy.


 
Reply With Quote
 
gabe garza
Guest
Posts: n/a
 
      17th May 2005
That's not a leak.

If I create a class with the following:
class t
{
char buffer[592];
}

then I create c as a class of t

t c = new t();

I've allocated c and it has 592 bytes off the top of the bat cause of the
property of buffer.

Think about it, no class would be a length of zero because it wouldn't be
useful in a real world application.

class t
{
}

t c = new t();

how useful is c?



"bleedledeep" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I would like to think that calling Load() again would cause the xd to
> clear out the old stuff and have only references to the new stuff, but,
> that is an interesting thing to try:
>
> If I do the following, then the 114,000s don't pile up, but I still
> never get rid of the last one.
>
> for ( int i = 0; i < 10; i++ )
> {
> XmlDocument xd = new XmlDocument();
> xd.Load( "test.xml" ) ;
> Thread.Sleep( 1000 );
> xd = null;
> System.GC.Collect();
> }
>
>
> Interesting to note that just calling XmlDocument xd = new
> XmlDocument(); seems to leak memory, but it is a fixed 592 bytes -
> doesn't matter how many times you call it.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Google/Yahoo! toolbars seem to cause memory/resource "leak" in IE =?Utf-8?B?QnJhbSBXZWlzZXI=?= Windows XP Internet Explorer 0 19th Sep 2006 06:05 PM
Big Memory Leak in XmlDocument.Load("filename"); bleedledeep Microsoft C# .NET 11 23rd May 2005 08:39 PM
"file in use" from XmlDocument.Load =?Utf-8?B?ZWswMw==?= Microsoft ASP .NET 3 11th May 2005 08:51 AM
Insert > Autotext is missing "filename" and "filename and path"; how to get back? StargateFan Microsoft Word New Users 7 7th Feb 2005 12:17 AM
outlook 2003 memory leak? /messages display as"none" and eats up memory Rob R. Microsoft Outlook 0 5th Mar 2004 08:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:55 AM.