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

How to find systems cached and free memory using C#

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

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