我试图在 C# 中模仿 Windows 7 任务管理器(仅限性能选项卡),虽然我已经设法获取了大部分值,但我无法获取物理内存的缓存字节值和空闲字节值。我知道他们的定义是:
- Cached refers to the amount of physical memory used recently for system resources.
- Free is the amount of memory that is currently unused or doesn't contain useful information (unlike cached files, which do contain useful information).
但我的方法都没有产生任务管理器中显示的确切值。例如:
缓存:
PerformanceCounter PC4 = new PerformanceCounter();
PC4.CategoryName = "Memory";
PC4.CounterName = "Cache Bytes";
PC4.InstanceName = "";
Console.WriteLine((Int32)(PC4.NextValue() / (1024 * 1024)));
免费:
ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();
object ob = 0;
foreach (ManagementObject result in results)
{
ob = result["FreePhysicalMemory"];
}//returns physical memory's <i>Available</i> byte, NOT <i>Free</i>
有什么我想念的吗?
最佳答案
您可以尝试查看 System.Diagnostics.Process 类。 我认为 Process.PagedMemorySize 是您感兴趣的。 以下代码示例将吐出一个正在运行的进程列表及其内存使用情况:
Process[] processlist = Process.GetProcesses();
foreach(Process p in processlist){
Console.WriteLine(“Process: {0} ID: {1} MEM:{2}”, p.ProcessName, p.Id, theprocess.PagedMemorySize);
}
如果 PagedMemorySize 不是唯一的尝试 VirtualMemorySize64
http://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx
关于c# - 如何获取 Windows 7 中缓存和释放的物理内存量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22050713/