C# : the close method of Xml.Load(file)

淺唱寂寞╮ 提交于 2019-12-19 08:00:37

问题


I have written some code that loads an XML document using an XmlDocument object so as to count it's nodes. Here is the method:

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

The problem that I am facing is, when importing a large file, it takes like 700MB of RAM. If I then try to do some operation on the file, or even read from it to display its data in a ListView, the application takes like 2GB of RAM. So, I was wondering, is there a method that closes the XmlDocument and frees its memory, releasing the RAM. It is like it's forgetting to remove its content from memory.


回答1:


No. The XmlDocument class does not implement IDisposable, so there is no way to force it to release it's resources at will. If you really need to immediately free the memory used by XmlDocument, the only way to do that would be to do the following:

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

The garbage collector works on a separate thread, so it may still not happen immediately. To force the garbage collection to occur synchronously, before continuing execution of your code, you must also call WaitForPendingFinalizers, as such:

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

XmlDocument always loads the entire document into memory at once. If you simply want to iterate through the nodes in the document as a stream, only loading a bit at a time, that is what the XmlReader class is for. However, you lose a lot of functionality that way. For instance, there is no way to select nodes via XPath, as you where doing in your example. With XmlReader, you have to write your own logic to determine where in the document you are and whether that matches what you are looking for.




回答2:


If you do not have to Manipulate the XML, just read the XML using XMLReader, that is oneway and the fastest, with less memory intense operation.




回答3:


There is no need to set your object to null. The GC should be able to indicate if the Document isn't being used any further on it's own. This will happen automatically as memory is needed but if you want to clear it immediately call GC.Collect(). See this thread for further discussion.



来源:https://stackoverflow.com/questions/11015965/c-sharp-the-close-method-of-xml-loadfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!