2015-05-25 43 views
2

可用內存我想不出能找到使用C#。幫助我的系統的緩存和可用內存....... How to find cached and free memory 如何找到系統緩存,並使用C#

+2

你有沒有看性能計數器爲'\內存\緩存Bytes'? –

+0

@SonerGönül....是的,我使用過它。它給緩存內存爲233.44 MB,但最初顯示在任務管理器中,大約爲3800 MB ......巨大差異 – METALHEAD

+0

使用System.Management來[閱讀此](https: //msdn.microsoft.com/en-us/library/aa394314%28v=vs.85%29.aspx)。下載WMI Code Creator並讓它生成您需要的代碼。 –

回答

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

enter image description here

+0

可用和總內存是完全正確的,但緩存=總可用似乎錯誤....因爲從上述緩存不= 7896-4056其中給出3840 ........ – METALHEAD

+0

雅......看來gud in WINDOWS 8,但在WINDOWS 7的自由內存計算即將作爲可用內存..... – METALHEAD

+0

我明白了,[this](http://brandonlive.com/2010/02/21/measuring-memory-usage-in-windows -7 /)可能會幫助你調整Windows7的代碼 – HadiRj

相關問題