来自 VisualBasic.Devices 的 TotalPhysicalMemory 与 WMI Win32_PhysicalMemory 略有不同
TotalPhysicalMemory from VisualBasic.Devices slightly different from WMI Win32_PhysicalMemory
所以我的应用程序需要知道可用的 RAM 总量。就我而言,至少存在 4 种不同的方式:
- 查询 Windows 管理工具 (WMI)
- Get it from Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory
- Query WinAPI
- Use P-Invoke
我喜欢前两个,但是它们在我的机器上给出的结果略有不同(2 根 2GB,Windows 8.1 64 位)。
我用来从 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管理处获取的代码:
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。
我的问题是:为什么会这样?
我的猜测是:
- VisualBasic ComputerInfo returns 从 OS 中看到的内存
透视图,而 WMI 使用制造商 'hardcoded' 值
- VisualBasic ComputerInfo returns 真正存在的内存(它
看来,市售的记忆棒很少有
确切的字节数),而 WMI 使用制造商 'hardcoded'
值
感谢您的回复。
这可能与您的情况相关:
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.
所以我的应用程序需要知道可用的 RAM 总量。就我而言,至少存在 4 种不同的方式:
- 查询 Windows 管理工具 (WMI)
- Get it from Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory
- Query WinAPI
- Use P-Invoke
我喜欢前两个,但是它们在我的机器上给出的结果略有不同(2 根 2GB,Windows 8.1 64 位)。
我用来从 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管理处获取的代码:
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。 我的问题是:为什么会这样?
我的猜测是:
- VisualBasic ComputerInfo returns 从 OS 中看到的内存 透视图,而 WMI 使用制造商 'hardcoded' 值
- VisualBasic ComputerInfo returns 真正存在的内存(它 看来,市售的记忆棒很少有 确切的字节数),而 WMI 使用制造商 'hardcoded' 值
感谢您的回复。
这可能与您的情况相关:
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.