How to get the size of available system memory?

后端 未结 5 1032
遥遥无期
遥遥无期 2020-12-02 15:27

Is it possible to get the size of system available memory in C#.NET? if yes how?

5条回答
  •  星月不相逢
    2020-12-02 16:12

    From EggHeadCafe after googling for 'c# system memory'

    You will need to add a reference to System.Management

    using System;
    using System.Management;
    
    namespace MemInfo
    {
        class Program
        {       
            static void Main(string[] args)
            {
                ObjectQuery winQuery = new ObjectQuery("SELECT * FROM Win32_LogicalMemoryConfiguration");
    
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(winQuery);
    
                foreach (ManagementObject item in searcher.Get())
                {
                    Console.WriteLine("Total Space = " + item["TotalPageFileSpace"]);
                    Console.WriteLine("Total Physical Memory = " + item["TotalPhysicalMemory"]);
                    Console.WriteLine("Total Virtual Memory = " + item["TotalVirtualMemory"]);
                    Console.WriteLine("Available Virtual Memory = " + item["AvailableVirtualMemory"]);
                }
                Console.Read();
            }
        }
    }
    

    Output:

    Total Space = 4033036

    Total Physical Memory = 2095172

    Total Virtual Memory = 1933904

    Available Virtual Memory = 116280

提交回复
热议问题