来自 VisualBasic.Devices 的 TotalPhysicalMemory 与 WMI Win32_PhysicalMemory 略有不同 [英] TotalPhysicalMemory from VisualBasic.Devices slightly different from WMI Win32_PhysicalMemory

查看:67
本文介绍了来自 VisualBasic.Devices 的 TotalPhysicalMemory 与 WMI Win32_PhysicalMemory 略有不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的应用程序需要知道可用的总 RAM.就我而言,至少存在 4 种不同的方式:

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

我喜欢前两个,但是它们在我的机器上给出的结果略有不同(2 个 2GB,每个 2GB,Windows 8.1 64 位).

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

我用来从 VisualBasic dll 获取它的代码:

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
    }
}

我用来从 Windows 管理中获取它的代码:

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
    }
}

差异略小于 2 MB,2064384 字节或 2016 kB.我的问题是:为什么会这样?

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

我的猜测是:

  • VisualBasic ComputerInfo 返回从操作系统看到的内存透视,而 WMI 使用制造商的硬编码"值
  • VisualBasic ComputerInfo 返回真正存在的内存(它看来,市售的记忆棒很少有确切的字节数),而 WMI 使用制造商硬编码"价值观
  • 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

感谢您的回复.

推荐答案

这可能与您的情况相关:

This may be relevant for your situation:

总物理内存

物理内存的总大小.请注意,在某些情况下,此属性可能不会返回物理内存的准确值.例如,如果 BIOS 正在使用某些物理内存,则它是不准确的.要获得准确的值,请改用 Win32_PhysicalMemory 中的容量属性.

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.

来源

这篇关于来自 VisualBasic.Devices 的 TotalPhysicalMemory 与 WMI Win32_PhysicalMemory 略有不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆