读取远程计算机上的内存量时出现问题

发布于 2024-10-30 12:53:02 字数 999 浏览 0 评论 0原文

我正在尝试确定计算机中安装的物理内存量。为了完成此任务,我使用 WMI(通过 .net 4.0)及其服务。问题是,无论远程计算机有多少内存,返回的值都是 4GB。这已经在三台远程计算机上进行了测试:

  • 虚拟机,1GB RAM,Windows 2003
  • 物理机,2GB RAM,Windows XP
  • 物理机,2GB RAM,Windows 7 64位

我自己正在运行物理机,4GB RAM,Windows 7 64位。

显示代码:

uint phisicalMemorySize = 0;

ConnectionOptions co = new ConnectionOptions();
co.Username = null;

ManagementScope ms = new ManagementScope("\\\\" + computerName, co);
ObjectQuery q = new ObjectQuery("select TotalPhysicalMemory from Win32_ComputerSystem");
ManagementObjectSearcher os = new ManagementObjectSearcher(ms, q);
ManagementObjectCollection moc = os.Get();

foreach (ManagementObject o in moc)
{
    phisicalMemorySize += Convert.ToUInt64(o["TotalPhysicalMemory"], CultureInfo.InvariantCulture);
}

我还尝试使用 select Capacity from Win32_PhysicalMemory从 Win32_OperatingSystem 选择 TotalVisibleMemorySize 作为查询,但无济于事。最后 phisicalMemorySize 将始终为 4GB。

I am trying to determine an amount of physical memory installed in a computer. To acomplish this I am using WMI (through .net 4.0) and it's services. The problem is that no matter what amount of memory remote computer has, value returned is 4GB. This has been tested with three remote computers:

  • Virtual machine, 1GB RAM, Windows 2003
  • Physical machine, 2GB RAM, Windows XP
  • Physical machine, 2GB RAM, Windows 7 64bit

I myself am running physical machine, 4GB RAM, Windows 7 64bit.

Showing the code:

uint phisicalMemorySize = 0;

ConnectionOptions co = new ConnectionOptions();
co.Username = null;

ManagementScope ms = new ManagementScope("\\\\" + computerName, co);
ObjectQuery q = new ObjectQuery("select TotalPhysicalMemory from Win32_ComputerSystem");
ManagementObjectSearcher os = new ManagementObjectSearcher(ms, q);
ManagementObjectCollection moc = os.Get();

foreach (ManagementObject o in moc)
{
    phisicalMemorySize += Convert.ToUInt64(o["TotalPhysicalMemory"], CultureInfo.InvariantCulture);
}

I have also tried using select Capacity from Win32_PhysicalMemory
and select TotalVisibleMemorySize from Win32_OperatingSystemas queries but to no avail. At the end phisicalMemorySize would allways be 4GB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏佲洛 2024-11-06 12:53:02

TotalPhysicalMemory 在 MSDN 库文档中有一个响亮的免责声明:

物理内存的总大小。是
意识到,在某些情况下,
该属性可能不会返回
准确的物理值
记忆。例如,它不是
如果 BIOS 使用某些功能,则准确
物理内存。为了准确
值,使用容量属性
改为 Win32_PhysicalMemory。

Afaik,所有现代机器都会将其 BIOS 复制到内存中。我认为容量属性是机器中可用的内存量,而不是现有的内存量。在任何 32 位操作系统上,这都是 2 GB;在 64 位操作系统上,32 位进程则为 4 GB。例如,与 List<>.Capacity 与 Count 属性进行比较。

我得到了一个不错的 TotalPhys 价值,3 GB,我知道我的笔记本电脑上有这个。容量为2GB,与操作系统匹配。 WMI 有时确实会变得不稳定,它并不完美。

使用 WMI 代码创建器实用程序以获得第二意见。我认为它是一个 .NET 1.1 程序,因此如果您在 64 位操作系统上运行它,请注意它的结果。如果您使用 Visual Studio 2010,请注意项目中的平台目标设置。它默认为 x86,因此即使在 64 位操作系统上,您也可以在 32 位模式下运行。项目 + 属性、构建选项卡、平台目标设置。

TotalPhysicalMemory has a loud disclaimer in the MSDN Library docs:

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.

Afaik, all modern machines copy their BIOS to memory. I think the Capacity property is how much memory is usable in the machine, not how much is present. Which is 2 gigabytes on any 32-bit operating system, 4 gigabytes for a 32-bit process on a 64-bit operating system. Compare to, say, the List<>.Capacity vs Count property.

I'm getting a decent value for TotalPhys, 3 gigabytes which I know I have on my laptop. Capacity is 2 gigabytes, matches the operating system. WMI does get flaky sometimes, it is hardly perfect.

Use the WMI Code Creator utility to get a second opinion. I think it is a .NET 1.1 program so beware of its results if you run it against a 64-bit operating system. If you use Visual Studio 2010 then watch out for the Platform Target setting in your project. It defaults to x86 so you'll run in 32-bit mode, even on a 64-bit operating system. Project + Properties, Build tab, Platform target setting.

陪你搞怪i 2024-11-06 12:53:02

发现问题了。是用

ManagementScope ms = new ManagementScope("\\\\" + computerName, co);

线来的。正确的应该是

ManagementScope ms = new ManagementScope("\\\\" + computerName + "\\root\\CIMV2", co);

它看起来默认为本地计算机。

感谢 Hans 向我推荐了 WMI Code Creator。这个工具确实帮了很大的忙。

Found the problem. It is with the

ManagementScope ms = new ManagementScope("\\\\" + computerName, co);

line. The correct would be

ManagementScope ms = new ManagementScope("\\\\" + computerName + "\\root\\CIMV2", co);

It looks like it defaulted to the local computer.

Thanks to Hans who pointed me to WMI Code Creator. That tool realy helped a great deal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文