Le Moustique

Hello,

I Have a large XML file where i use Xpath for retrieve some data.

Now that works just fine. But my memory is increasing each time i open a new XML file.

Is it possible to dispose the XPathDocument

It is NOT possible to somthing like this:

XPathDocument XDoc = new XPathDocument([Location]);

........ Searching in document ....

((IDisposable)XDoc).Dispose();

Is there a way to decrease my memory

Please help me out and ... Thank you for your time.



Re: XML and the .NET Framework XPathDocument and IDisposable

Sergey Dubinets - MSFT

The better way to write this would be:

using(XPathDocument XDoc = new XPathDocument([Location])) {

// ........ Searching in document ....
}

This doesn't mean that document would be collected immediately, but it would be collected next time GC happens.

You can ensure GC run by calling:

GC.Collect();

But it runs asynchronously any way so to be sure that GC finished you can call:

GC.WaitForPendingFinalizers();

This wouldn't speed up your program, but sometimes is useful for perf testing.

Trust system to do its work!

http://msdn2.microsoft.com/en-gb/library/system.gc.waitforpendingfinalizers.aspx