Search code examples
c#.netwindowswmiout-of-memory

win32_processor out of memory


I want to get id processor in .NET with WMI but when I'm using the get() method from the ManagementObjectSearcher, I'm getting an out of memory exception ...

If you want to take a look from the code see below :

   ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "select * from Win32_Processor");

            foreach (ManagementObject share in searcher.Get())
                foreach (PropertyData PC in share.Properties)
                    if (PC.Name.Equals("ProcessorId"))
                        return (string)PC.Value;

            return null;

This code works on others computers but not on mine ...

I'm using windows 7.

What is the problem ?

I tried to restart WMI service and that not resolve my problem :(


Solution

  • There are several reasons which could cause out of memory exception.

    1. possible memory leak in WMI, source: http://brooke.blogs.sqlsentry.net/2010/02/win32service-memory-leak.html
    2. check whether you have permission(s), that would explain why does your code work on some computers and why doesn't on yours.
    3. run your code as Administrator (for debugging start VS as Administrator)
    4. Here is an other code snippet, try this one as well... who knows

    Sample:

    public static String GetCPUId()
    {
        String processorID = "";
    
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(
            "Select * FROM WIN32_Processor");
    
        ManagementObjectCollection mObject = searcher.Get();
    
        foreach (ManagementObject obj in mObject)
        {
            processorID = obj["ProcessorId"].ToString();
        }
    
        return processorID;
    }
    

    Source: WIN32_Processor::Is ProcessorId Unique for all computers