C #: close method for Xml.Load (file)

I wrote code that loads an XML document using an object XmlDocumentto treat it as nodes. Here is a way:

XmlDocument xml = new XmlDocument();
xml.Load(textBox1.Text);
XmlNodeList nodes = xml.SelectNodes("//File");
foreach (XmlNode node in nodes)
{
    number_of_childs++;
}

The problem I encountered is that importing a large file requires about 700 MB of RAM. If I then try to perform some operation on a file or even read from it to display its data in ListView, the application takes about 2 GB of RAM. So, I was wondering if there is a method that closes XmlDocumentand frees memory, freeing up RAM. It looks like he forgets to delete his content from memory.

+5
source share
3 answers

. XmlDocument IDisposable, . , XmlDocument, - :

nodes = null;
xml = null;
GC.Collect();

, . , WaitForPendingFinalizers, :

nodes = null;
xml = null;
GC.Collect();
GC.WaitForPendingFinalizers();

XmlDocument . , , XmlReader. . , XPath, . XmlReader , , , , .

+12

XML, XML, XMLReader, oneway , .

+2

There is no need to set your object to null. The Civil Code should be able to indicate whether the Document will be used further. This will happen automatically as needed in memory, but if you want to clear it, immediately call GC.Collect () . See this thread for further discussion.

+1
source

All Articles