C# : Xml.Load(file) 的 close 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11015965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# : the close method of Xml.Load(file)
提问by R.Vector
I have written some code that loads an XML document using an XmlDocumentobject so as to count it's nodes. Here is the method:
我编写了一些使用XmlDocument对象加载 XML 文档的代码,以便计算它的节点数。这是方法:
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 XmlDocumentand frees its memory, releasing the RAM. It is like it's forgetting to remove its content from memory.
我面临的问题是,导入大文件时,它需要 700MB 的 RAM。如果我随后尝试对该文件进行一些操作,或者甚至从中读取以将其数据显示在ListView. 所以,我想知道,是否有一种方法可以关闭XmlDocument并释放其内存,释放 RAM。这就像忘记从内存中删除其内容一样。
采纳答案by Steven Doggart
No. The XmlDocumentclass 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:
不。这个XmlDocument类没有实现IDisposable,所以没有办法强制它随意释放它的资源。如果您真的需要立即释放 使用的内存XmlDocument,唯一的方法是执行以下操作:
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:
垃圾收集器在一个单独的线程上工作,所以它可能仍然不会立即发生。要强制垃圾收集同步发生,在继续执行代码之前,您还必须调用WaitForPendingFinalizers,如下所示:
nodes = null;
xml = null;
GC.Collect();
GC.WaitForPendingFinalizers();
XmlDocumentalways 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 XmlReaderclass 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.
XmlDocument总是一次将整个文档加载到内存中。如果您只是想以流的形式遍历文档中的节点,一次只加载一点,这就是XmlReader该类的用途。但是,您会以这种方式失去很多功能。例如,无法通过 XPath 选择节点,就像您在示例中所做的那样。使用XmlReader,您必须编写自己的逻辑来确定您在文档中的位置以及它是否与您要查找的内容匹配。
回答by Furqan Hameedi
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.
如果您不必操作 XML,只需使用XMLReader读取 XML ,这是一种方式,速度最快,内存占用较少的操作。
回答by Fr33dan
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 threadfor further discussion.
无需将对象设置为 null。GC 应该能够指出 Document 是否不再单独使用。这将在需要内存时自动发生,但如果您想立即清除它,请调用GC.Collect()。请参阅此线程以进行进一步讨论。

