ManagementObjectSearcher内存不足异常

时间:2010-12-07 14:38:51

标签: c# out-of-memory system.management

在C#.Net MVC2应用程序中,我们有一个简单的函数来获取处理器ID作为识别Web服务器的方案的一部分。相关部分是:

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select ProcessorId from Win32_Processor"))
        {
            foreach (ManagementObject share in searcher.Get())
            {
                foreach (PropertyData PC in share.Properties)
                {
                    return PC.Value.ToString();
                }
            }
        }

它在开发和运行cassini和IIS的许多Web服务器上都运行良好。但是,在Server 2008计算机上的最新安装中,它会在第一次调用时抛出Out of Memory Exception以获取处理器ID。任何关于可能的原因或更好的方法来达到上述目的的建议。

非常感谢

编辑以包含堆栈跟踪:

System.Management.ManagementException: Out of memory 


at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
   at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
   at GIB.Helpers.SoftwarehouseLicenseAttribute.GetProcessorID() in C:\Users\Dog\Documents\Visual Studio 2010\Projects\GIB\GIB\Helpers\SoftwarehouseLicense.cs:line 177
   at GIB.Helpers.SoftwarehouseLicenseAttribute.setup() in C:\Users\Dog\Documents\Visual Studio 2010\Projects\GIB\GIB\Helpers\SoftwarehouseLicense.cs:line 75
   at GIB.Controllers.HomeController.Setup() in C:\Users\Dog\Documents\Visual Studio 2010\Projects\GIB\GIB\Controllers\HomeController.cs:line 37
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

1 个答案:

答案 0 :(得分:8)

我遇到了类似的问题,试图查询Win32_Service。我去了Administrative Tools\Computer Management下的WMI控制台。主屏幕显示Win32_Process的内存不足错误。我最终重新启动了WMI服务(“Windows Management Instrumentation”)并解决了这个问题。