WMI Memory leak solution

喜你入骨 提交于 2019-12-12 03:23:47

问题


I have a static method that invokes WMI management objects and serializes the result. It works well enough, but it leaks memory like a sieve. About 20k every time I run it. (even with all of the serialization turned off).

I've tested in .Net 2.0, 3.0, 3.5 and the memory leak is constant throughout.

Am I doing something wrong? Or is there a way to box this method so that what ever leak it has, is thrown out when it finishes. (like boxing it in a child process)

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
         {
             try
             {

                 using(ManagementObjectCollection moList = searcher.Get()){
                     foreach (ManagementObject mo in moList)
                     {
                         try
                         {
                             // Do serialization here.  Return object will be a List<GenereicObjectType>

                         }
                         catch (Exception ex)
                         {
                             Debug.Error(ex);
                         }
                         finally
                         {
                             if (mo != null)
                             {
                                 try { 
                                     mo.Dispose(); 

                                 }
                                 catch { }
                             }
                         }
                     }
                 }

             }
             catch (Exception ex)
             {
                 Debug.Error(ex);
             }

         }

来源:https://stackoverflow.com/questions/8922601/wmi-memory-leak-solution

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