Groups | Blog | Home
all groups > dotnet xml > august 2004 >

dotnet xml : free memory



frostbox NO[at]SPAM hotmail.com
8/23/2004 9:25:01 PM
The system.xml.xmldocument does not implement a method to dispose() or
otherwise free the memory after instantiation.

Another discussion thread concludes that everything is handled
automatically though garbage collection once memory runs out !! (this
can't be efficient?)

Why then does other system objects implement dispose() ?? (like
system.drawing.image)

Is there a way to forcefully free the memory used by an xmldocument
object ??

In he old days of ASP/MSXML2 one would always free mem using :
xml_obj = Nothing


Cowboy (Gregory A. Beamer) [MVP]
8/24/2004 8:05:36 AM
[quoted text, click to view]

The XMLDocument does not utilize unmanaged resources. While a Dispose() to
mark might be nice, it would really yield no benefit, as the GC is not going
to automagically clean it up any faster just because it is marked. The
primary reason for marking is to clean up the unmanaged resources.

[quoted text, click to view]

Jeffrey Richter has written a treatise on this. There were different types
of garbage collection schemes examined. While memory only seemed like a bad
scheme, in human thought patterns, it turned out to be the most efficient.

Having memory full is not a real issue, esp. if .NET is running on a .NET
server (ie, you are not also using it as SQL Server, a Domain Controlller
and your Exchange box). Even in these instances, the GC can sense when other
items are grabbing memory and run the GC. Whether the memory is full of junk
or not is inconsequential if you do not need. Since GC clears when you do,
it is still not a problem.

[quoted text, click to view]

There is an underlying hook into the unmanaged world through the windows
graphical subsystem. As long as the object itself touches the COM world
(windows API in many cases), Dispose() is necessary. Same with objects like
database connections.

[quoted text, click to view]

Only by taking control of the GC, and even that is sporadic. I doubt you can
control memory management any better, so it is more likely you will mess
this up to make your counters look better.

[quoted text, click to view]

You can null out the object in .NET, but that still will not make the GC
work any faster.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************

frostbox NO[at]SPAM hotmail.com
8/24/2004 12:04:23 PM
That answers my questions. Thanks.

I just guess the .NET GC must be a very intelligent component since it
'knows' what to clean before I do! Coming from C++ I'm not that used
to all Microsoft Magic yet.

Is there a generel rule to which instantiated objects should be
cleaned up manually, and which I should leave to the .NET GC ??

or how will I know true .NET objects from the Windows based ones ??

I guess this also means that .NET is not likely to be platform
independent in near future when part of the library is based on
Windows subsystem features (like system.drawing.image class).

Oleg Tkachenko [MVP]
8/24/2004 1:21:35 PM
[quoted text, click to view]

There is no unmanaged resources held by XmlDocument, it's just tree in
memory. It's released automatically.

[quoted text, click to view]

It's one of strategies of .NET garbage collector. It's quite effective.

[quoted text, click to view]

AFAIK system.drawing.image holds some unmanaged resources like native
windows handles or something like this. And so due to undetermenistic
garbage collection in .NET it needs another deterministic way to release
such objects. System.Xml is pure .NET, no native resources is used so no
need for any special stuff.

[quoted text, click to view]

myDoc = null;
In fact such assignment might even prolong lifetime of myDoc. .NET is
smart enough to detect when any object can be released already (e.g.
when it's not referenced anymore).

--
Oleg Tkachenko [XML MVP]
Oleg Tkachenko [MVP]
8/25/2004 10:56:20 AM
[quoted text, click to view]

Welcome to the managed world :)

[quoted text, click to view]

You don't need to bother about any cleaning unless you are using any
resources that need to be released in deterministic way (e.g. open file
or native handle). Then use IDisposable pattern. C# even has special
"using " statement to simplify its usage.

[quoted text, click to view]

You will :)

[quoted text, click to view]

It is already. Take a look at Mono.

[quoted text, click to view]

Don't mix one concrete implementation with standard API.

--
Oleg Tkachenko [XML MVP]
Søren Gullach
9/22/2004 12:03:01 AM
Michael has a point here, you all honner the smartness of the GC, but
XMLDocument has a memoryleak!!! Our application runs for about 7 hours, then
the PC runs out of virtual memory !! here is a sample code run with
boundscheker.

class CTest
{
public CTest(String FilePath)
{
int MaxLoop = 10000;
int step = MaxLoop/100+1;
for(int i=0; i<MaxLoop; i++)
{
if((i%step)==0)
Console.WriteLine("loop {0} of {1} {2}%",i,MaxLoop,(i*100.0)/MaxLoop);
try
{
XmlTextReader reader = new XmlTextReader(XML,XmlNodeType.Document,new
XmlParserContext(null,null,null,XmlSpace.None));
XPathDocument doc = new XPathDocument(reader);

// doc.Save(FilePath+".out");
// doc.RemoveAll();
// doc = null;
GC.Collect();
}
catch(Exception e)
{
Console.WriteLine("Error: "+e.Message);
Console.ReadLine();
return;
}
}
GC.Collect();
}
}
(how to add screen dump? paste do not work)

[quoted text, click to view]
Oleg Tkachenko [MVP]
9/22/2004 2:11:52 PM
[quoted text, click to view]

So the leak you say is in XPathDocument? Provide please full repro so we
can confirm it.

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button