2010-12-07 43 views
5

Trong ứng dụng C# .Net MVC2, chúng ta có một hàm đơn giản để lấy id bộ xử lý như là một phần của lược đồ xác định máy chủ web. Phần có liên quan là:ManagementObjectSearcher out of memory excepiton

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

nó đã hoạt động tốt trong dev và trên một số máy chủ web chạy cassini và IIS. Tuy nhiên, trong lần cài đặt mới nhất trên máy Server 2008, nó sẽ ném ra một Out of Memory Exception trong lần gọi đầu tiên để lấy id bộ xử lý. Bất kỳ lời khuyên nào về nguyên nhân có thể hoặc cách tốt hơn để đạt được điều này.

Nhiều Cảm ơn

Chỉnh sửa để bao gồm các 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) 
+1

Hiển thị dấu vết ngăn xếp của ngoại lệ. –

Trả lời

7

Tôi chỉ chạy vào một vấn đề tương tự, cố gắng để truy vấn Win32_Service. Tôi đã đi đến giao diện điều khiển WMI theo Administrative Tools\Computer Management. Màn hình chính hiển thị lỗi Out of Memory cho Win32_Process. Tôi đã kết thúc khởi động lại dịch vụ WMI ("Windows Management Instrumentation") và đã khắc phục sự cố.

+2

Có một lỗi đã biết trong các phiên bản WMI gần đây khi truy vấn Win32_Service. Xem, ví dụ: http://brooke.blogs.sqlsentry.net/2010/02/win32service-memory-leak.html. Tôi không chắc chắn rằng nó áp dụng cho các lớp WMI khác. –

+0

Tôi đã gặp phải vấn đề tương tự. Khởi động lại dịch vụ WMI ("Windows Management Instrumentation") và đã khắc phục được sự cố. Cảm ơn, – sean717

+0

Tôi cũng chạy vào truy vấn này 'Win32_Printer' – Cameron

Các vấn đề liên quan