c# - 如何获取 Windows 7 中缓存和释放的物理内存量?

标签 c# windows-7 performancecounter taskmanager

我试图在 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/

相关文章:

c# - 当应用程序是 C# 中的任务栏时,如何监听快捷方式

c# - 如何使用 MailKit 以相反的顺序获取 UID 列表?

windows-7 - 从 VBScript 提升权限

iphone - iPhone 上的性能计数器

.net - 到 PerformanceCounter 还是编写自定义监控?

.NET 4.0 进程执行暂停了几秒钟,与 Full GC 一致,由交换文件事件引起?

c# - SQL Server 管理对象

c# - 桌面上的透明表格

ruby-on-rails - 如何在 Windows 7 上的 Rails 3 应用程序中使用 "mysql2"gem?

java - 对于 Windows 7,推荐的 Java 版本是什么?