6

In a C# .Net MVC2 app we have a simple function to get the processor id as part of a scheme to identify the web server. The relevant part is:

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();
                }
            }
        }

it has been working fine in dev and on a number of web servers running cassini and IIS. However on the latest installation on a Server 2008 machine it is throwing an Out of Memory Exception on the first call to get the processor id. Any advise on a possible cause or better way to achieve the above.

Many Thanks

Edit to include the stack trace:

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)
Matthew Hood
  • 958
  • 3
  • 13
  • 22

1 Answers1

9

I just ran into a similar issue, trying to query the Win32_Service. I went to the WMI console under Administrative Tools\Computer Management. The main screen showed an Out of Memory error for Win32_Process. I ended up restarting the WMI service ("Windows Management Instrumentation") and that fixed the issue.

Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
Ted Elliott
  • 3,415
  • 1
  • 27
  • 30
  • 3
    There's a known bug in recent versions of WMI when querying for Win32_Service. See, for example, http://brooke.blogs.sqlsentry.net/2010/02/win32service-memory-leak.html. I'm not sure that it applies to other WMI classes. – Roger Lipscombe Jan 07 '11 at 19:06
  • I ran into the same problem. Restarted the WMI service ("Windows Management Instrumentation") and that fixed the problem. Thanks, – sean717 Feb 22 '11 at 20:23
  • I also ran into this querying `Win32_Printer` – Cameron Jul 06 '11 at 19:45
  • I also ran into problem for "SELECT CommandLine FROM Win32_Process WHERE ProcessId = 2000" – M.Mahdipour Jun 26 '16 at 02:40