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

Home Posts Topics Members FAQ

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

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.

Nov 17 '05 #1
11 10116
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.

Nov 17 '05 #2

"bleedledeep" <bl*********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.
Nov 17 '05 #3
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.

Nov 17 '05 #4
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.

Nov 17 '05 #5

"bleedledeep" <bl*********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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.


Nov 17 '05 #6

"bleedledeep" <bl*********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.
Nov 17 '05 #7
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.

Nov 17 '05 #8

"bleedledeep" <bl*********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.
Nov 17 '05 #9
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" <bl*********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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.

Nov 17 '05 #10

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...

"bleedledeep" <bl*********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.

I sure wish it was better documented when HE was apt to decide to call the
GC routines.

-Mark
Nov 17 '05 #11

"Willy Denoyette [MVP]" <wi*************@telenet.be> schrieb im Newsbeitrag
news:OB**************@TK2MSFTNGP14.phx.gbl...
another remark is that you shouldn't use tools like profilers unless you
have a real leak
What the heck of a dirty opinon is what I'm reading here?
Did you ever read the book "George Orwell - 1984"?
Why shouldn't a programmer be allowed to observate his code even without
having heavy trouble with it.
You're telling something like: Don't figure out how nuclear bombs may harm
you unless you die...
Again, this is not a leak, learn the difference between normal behavior
and memory leaks.


Indeed, It will hardly be leak in this propably well tested class but
wouldn't it be better
from you to direct him instead of telling him, that he is wrong?
It's always amazing how bad 3 stupid letters may influence a persons
behavoir.

Microsoft is a Companay in the US not a Religion with head quarters in the
Vatican.

Greets, Sebastian Dau
Nov 17 '05 #12

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

Similar topics

9
by: mahurshi | last post by:
i have a quick question i am putting a debug flag in my program (i really dont need this feature, but i figured it might be useful when i get into trouble) so i want to check if argv is the...
5
by: Richard | last post by:
I added a bitmap file to my project in the Soultion Explorer window. I set the Build Action property on the bitmap file to Embedded Resource. The Project Properties, File Properties help item says...
9
by: ganesh4all | last post by:
Hi: I am trying to connect to DB with the following command db2 connect to <dbnameuser <usernameusing <password> It throws the following exception SQL1337N The service "db2inst1" was not...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
11
by: kimiraikkonen | last post by:
Hi, Vb.net 2005 express i've been working, the problem is: I use openfiledialog to browse files, i created some code to execute file with OK button, no problem. But if i press "cancel" button as...
6
by: MACKTEK | last post by:
Intro: I have recently started programming in C++/CLI. I have done a lot of research on the net trying to locate an answer to this problem, but there seems to be a lack of Tutorials and books on...
1
by: perlvasu | last post by:
6 jobs are running at the same time and accessing same dbm file for writing and reading. These are daily jobs and failing only some times not regularly. So i thought of this is just because of dead...
1
by: Rishi Boparai | last post by:
Hi, in Visual Studios there's this tool called PictureBox rite...but in Visual Web developer i can't seem to find it.. i'm using Visual Web Developer...VB language..to make a watermarking...
1
by: blurryanthem | last post by:
Hello, I am trying to create a program that would replicate different selected files "x" times into a specified directory and save them with filename indexes. Then after create a log list of the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.