【问题标题】:How to find systems cached and free memory using C#如何使用 C# 查找系统缓存和释放内存
【发布时间】:2015-08-06 16:06:31
【问题描述】:

我无法使用 C# 找到系统的缓存和空闲内存。帮帮我.......

【问题讨论】:

  • 您是否曾将性能计数器视为\Memory\Cache Bytes
  • @SonerGönül....是的,我用过它。它提供的缓存内存为 233.44 mb,但最初在任务管理器中显示为大约 3800 mb....巨大的差异
  • 使用 System.Management 到read this。下载 WMI Code Creator 并让它生成您需要的代码。

标签: c# caching memory


【解决方案1】:

Microsoft.VisualBasic.Devices 程序集引用添加到您的项目,然后您可以使用以下

        var Available = new ComputerInfo().AvailablePhysicalMemory;
        var Total = new ComputerInfo().TotalPhysicalMemory;
        var Cheched = Total - Available;

编辑:

以下代码适用于我,另请注意,可用金额包括免费金额,还包括 大部分的缓存金额。

        ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
        ManagementObjectCollection results = searcher.Get();

        //total amount of free physical memory in bytes
        var Available = new ComputerInfo().AvailablePhysicalMemory;
        //total amount of physical memory in bytes
        var Total = new ComputerInfo().TotalPhysicalMemory;

        var PhysicalMemoryInUse = Total - Available;
        Object Free = new object();
        foreach (var result in results)
        {
            //Free amount
            Free = result["FreePhysicalMemory"];
        }


        var Cached = Total - PhysicalMemoryInUse - UInt64.Parse(Free.ToString());

        Console.WriteLine("Available: " + ByteToGb(Available));
        Console.WriteLine("Total: " + ByteToGb(Total));
        Console.WriteLine("PhysicalMemoryInUse: " + ByteToGb(PhysicalMemoryInUse));
        Console.WriteLine("Free: " + ByteToGb(UInt64.Parse( Free.ToString())));
        Console.WriteLine("Cached: " + ByteToGb(Cached));

【讨论】:

  • Available 和 Total memory 完全正确,但 cached = Total-available 似乎是错误的....因为从上面缓存的 not = 7896-4056 给出了 3840 ..........跨度>
  • 是的...在 WINDOWS 8 中似乎很有趣,但在 WINDOWS 7 中,可用内存计算为可用内存.....
  • 我明白了,this 可能会帮助您调整 Windows7 的代码
  • 不错!你能提供一些关于你如何让它工作的细节吗?如果我的帖子回答了您的问题,请考虑接受。
猜你喜欢
  • 1970-01-01
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
相关资源
最近更新 更多