如何使用C#获取操作系统的免费物理内存? [英] How to get free physical memory of operating system using C#?

查看:103
本文介绍了如何使用C#获取操作系统的免费物理内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想用c#获取os的FreePhysicalMemory。

i使用WMI查询Win32_OperatingSystem类,但它没有给出准确的值

PLZ帮帮我

TIA



我尝试了什么:



以下是我的代码



 ObjectQuery wql = new ObjectQuery(SELECT * FROM Win32_OperatingSystem) ; 
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();
foreach(结果中的结果)
{
amountFree = Convert.ToInt32(result [FreePhysicalMemory]);
}

解决方案

试试这个:

 ManagementObjectSearcher search = new ManagementObjectSearcher(root \\ CYV2,从Win32_OPeratingSystem中选择TotalVisibleMemorySize,FreePhysicalMemory); 

foreach(ManagementObject x in search.Get())
{

ulong totalMemory =(ulong)x [TotalVisibleMemorySize];
ulong freeMemory =(ulong)x [FreePhysicalMemory];
}

freeMemory以KB格式返回,在我的系统上似乎正确。很难准确,因为系统可用内存随着进程的分配而变化,并且释放内存作为其正常活动的一部分。


引用:

我使用WMI查询Win32_OperatingSystem类但它没有给出准确的值你怎么知道它不准确?



可能有两个原因导致结果不符合预期:



  1. 您错过了该值以千字节为单位

  2. 当系统用于预取时,可用内存可能少于预期( Prefetcher - Wikipedia [ ^ ])


Hi all,
I want to fetch the FreePhysicalMemory of os using c#.
i used WMI query for Win32_OperatingSystem class but it is not giving the accurate value
plz help me
TIA

What I have tried:

below is my code

ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get(); 
foreach (var result in results)
{
 amountFree = Convert.ToInt32( result["FreePhysicalMemory"]);
}

解决方案

Try this:

ManagementObjectSearcher search = new ManagementObjectSearcher("root\\CIMV2", "Select TotalVisibleMemorySize, FreePhysicalMemory from Win32_OPeratingSystem");

foreach (ManagementObject x in search.Get())
    {

    ulong totalMemory = (ulong)x["TotalVisibleMemorySize"];
    ulong freeMemory = (ulong)x["FreePhysicalMemory"];
    }

freeMemory is returned in KB, and seems about right on my system. It's difficult to be precise, because the system free memory varies all the time as processes allocate and free memory as a part of their normal activity.


Quote:

i used WMI query for Win32_OperatingSystem class but it is not giving the accurate value

How do you know that it is not accurate?

There may be two reasons that the result is not as expected:


  1. You missed that the value is in kilobytes
  2. There may be less free memory than expected when used by the system for prefetching (Prefetcher - Wikipedia[^])


这篇关于如何使用C#获取操作系统的免费物理内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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