gpt4 book ai didi

c# - 如何使用 C# 查找系统缓存和空闲内存

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:01 25 4
gpt4 key购买 nike

我无法使用 C# 找到系统的缓存和空闲内存。帮帮我...... How to find cached and free memory

最佳答案

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));

enter image description here

关于c# - 如何使用 C# 查找系统缓存和空闲内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30433879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com