内存泄漏的XmlDocument() [英] Memory Leak with XmlDocument()

查看:188
本文介绍了内存泄漏的XmlDocument()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信,我有我的一些代码,使用XmlDocument类内存泄漏。

I believe I have memory leak with some of my code that uses the XmlDocument class.

我的程序在Windows 6.1.4设备(C#)中运行,并读取从另一台服务器上的数据库,看是否在设备上安装任何程序需要卸载,然后从一个XmlDocument读取来获取在卸载的程序的名称。然后该程序相匹配的列表,并相应地卸载,如果需要的话。这个过程是无限循环,并在后台运行,但什么我注意到的是,记忆随着时间的推移慢慢地爬行,程序最终捕获了一个 OutOfMemoryException异常

My program runs on a Windows 6.1.4 device (C#) and reads from a database on another server to see if any programs installed on the device need to be uninstalled and then reads from an XmlDocument to get the names of the programs that are uninstallable. The program then matches the lists and uninstalls accordingly, if necessary. This process is looped infinitely and runs in the background but what I'm noticing is that the memory creeps up slowly over time and the program eventually catches an OutOfMemoryException.

如果我注释掉一切,并在循环中什么也不做,内存保持对周围2MB consistantly。如果我离开的一切,但下面的代码注释掉那么内存使用率上升每分钟0.05兆左右持续。这些结果与具有1秒的循环睡眠。经常睡眠速度大约10分钟。任何想法,以什么可能会造成泄漏,如果它有什么用XmlDocument类?

If I comment out everything and do nothing in the loop, the memory stays right around 2MB consistantly. If I leave everything but the following code commented out then the memory usage goes up .05 megabytes every minute or so continuously. Those results are with having the loop sleep for 1 second. The regular sleep speed is about 10 minutes. Any thoughts as to what could be causing the leak and if it has anything to do with the XmlDocument class?

foreach (string programName in uninstallPrograms)
{
    XmlDocument xmlDoc1 = new XmlDocument();
    xmlDoc1.LoadXml("<wap-provisioningdoc>" +
          "  <characteristic type=\"UnInstall\">" +
          "    <characteristic type=\"" + programName + "\">" +
          "     <parm name=\"uninstall\" value=\"1\"/>" +
          "    </characteristic>" +
          "  </characteristic>" +
          "</wap-provisioningdoc>");

    xmlDoc1 = ConfigurationManager.ProcessConfiguration(xmlDoc1, true);

    cmdStr += "DELETE FROM DEVICE_APPS WHERE ID = " + deviceAppIDList[count++] + "; ";

    xmlDoc1 = null;
}

// Check for pre-installed apps to uninstall
count = 0;

XmlDocument xmlDoc2 = new XmlDocument();

xmlDoc2.LoadXml("<wap-provisioningdoc><characteristic-query type=\"UnInstall\"/>" +
        "</wap-provisioningdoc>");

/**** The line below seems to be the cause of the memory leak ****/
//xmlDoc2 = ConfigurationManager.ProcessConfiguration(xmlDoc2, true);

XmlNodeList xmlNodeList = xmlDoc2.SelectNodes("wap-provisioningdoc/" +
        "characteristic[@type='UnInstall']/characteristic/@type");

xmlDoc2 = null;



cmdStr 并最终习惯和设置到的String.Empty 在循环的结束。起初,我并没有 xmlDoc中= NULL; 在我的代码,但它并没有帮助无论哪种方式。我试着加入 GC.Collect的(); 在我的循环的结束,这似乎有助于减缓泄漏,但它并没有完全解决。另外,我读过它不是这样用好的做法

cmdStr does eventually get used and is set to string.Empty at the end of the loop. At first I had didn't have xmlDoc = null; in my code but it didn't help either way. I've tried adding GC.Collect(); at the end of my loop and that seemed to help slow down the leak but it doesn't fix it entirely. Plus I've read it's not good practice to use it anyway.

编辑:所以,这似乎是ConfigurationManager中线上,我注释掉在我上面的代码有内存泄漏。当我注释掉的代码行的内存泄漏停止。当我取消注释该行启动备份。有什么我需要做调用ProcessConfiguration以释放内存后做什么?

So it seems to be the ConfigurationManager line that I commented out in my code above that has the memory leak. As soon as I comment out that line of code the memory leak stops. It starts back up when I uncomment this line. Is there something I need to do after making the call to ProcessConfiguration to release the memory?

此外,我使用ConfigurationManager中运行时版本1.1 Microsoft.WindowsMo​​bile.Configuration命名空间0.4322以来System.Configuration不存在CF

Also, I am using the Microsoft.WindowsMobile.Configuration namespace for ConfigurationManager runtime version 1.1.4322 since System.Configuration does not exist in CF.

推荐答案

貌似这个问题的答案是做它本身使用 DMProcessConfigXML()。使用这种方法不会导致内存泄漏。所以必须有未正确释放其资源包装内的东西。

Looks like the answer to this is to do it natively using DMProcessConfigXML(). Using this method does not cause a memory leak. So there must be something within the wrapper that is not releasing its resources properly.

这篇关于内存泄漏的XmlDocument()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆