TotalPhysicalMemory from VisualBasic.Devices sligh

2019-09-06 10:14发布

So my application requires knowing total RAM available. At least 4 different ways exist, as far as I'm concerned:

I like the first two, hovewer they give slightly different results on my machine (2 sticks of 2GB each, Windows 8.1 64 bit).

The code I use to get it from VisualBasic dll:

class Program
{
    private static readonly Lazy<ComputerInfo> ComputerInfo = new Lazy<ComputerInfo>();
    public static ulong TotalRam => ComputerInfo.Value.TotalPhysicalMemory;

    static void Main(string[] args)
    {
        Console.WriteLine("Total RAM from ComputerInfo: {0} bytes", TotalRam);
        // Result: Total RAM from ComputerInfo: 4292902912 bytes
    }
}

The code I use to get it from Windows Management:

class Program
{
    public static IEnumerable<object> GetResults(string win32ClassName, string property)
    {
        return (from x in new ManagementObjectSearcher("SELECT * FROM " + win32ClassName).Get().OfType<ManagementObject>()
                select x.GetPropertyValue(property));
    }
    public static ulong? TotalInstalledBytes
    {
        get
        {
            var values = GetResults("Win32_PhysicalMemory", "Capacity");
            ulong? sum = null;
            foreach (var item in values)
            {
                var casted = item as ulong?;
                if (casted.HasValue)
                {
                    if (sum == null) sum = 0;
                    sum += casted.Value;
                }
            }
            return sum;
        }
    }
    static void Main(string[] args)
    {
        Console.WriteLine("Total RAM from WMI: {0} bytes", TotalInstalledBytes);
        // Result: Total RAM from WMI: 4294967296 bytes
    }
}

The difference is slightly less than 2 MB, 2064384 bytes or 2016 kB exactly. My question is: why is that so?

My guesses would be either:

  • VisualBasic ComputerInfo returns memory seen from the OS perspective, while WMI uses the manufacturer 'hardcoded' values
  • VisualBasic ComputerInfo returns memory that is really there (it appears, that the memory sticks commercially availabe rarely have the exact amount of bytes), while WMI uses the manufacturer 'hardcoded' values

Thanks for responses.

标签: c# wmi ram
1条回答
趁早两清
2楼-- · 2019-09-06 10:55

This may be relevant for your situation:

TotalPhysicalMemory

Total size of physical memory. Be aware that, under some circumstances, this property may not return an accurate value for the physical memory. For example, it is not accurate if the BIOS is using some of the physical memory. For an accurate value, use the Capacity property in Win32_PhysicalMemory instead.

Source

查看更多
登录 后发表回答