C#擷取記憶體資訊

來源:互聯網
上載者:User
 1   /// <summary>
2 /// 記憶體
3 /// </summary>
4 public class VAV_MDDFM_MEM
5 {
6 //定義記憶體的資訊結構
7 [StructLayout(LayoutKind.Sequential)]
8 public struct MEMORY_INFO
9 {
10 public uint dwLength;
11 public uint dwMemoryLoad;
12 public uint dwTotalPhys;
13 public uint dwAvailPhys;
14 public uint dwTotalPageFile;
15 public uint dwAvailPageFile;
16 public uint dwTotalVirtual;
17 public uint dwAvailVirtual;
18 }
19 [DllImport("kernel32")]
20 private static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
21 [DllImport("kernel32")]
22 private static extern void GetSystemDirectory(StringBuilder SysDir, int count);
23 [DllImport("kernel32")]
24 private static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
25
26 /// <summary>
27 /// 列印記憶體資訊
28 /// </summary>
29 public static void PrintMemInfo()
30 {
31 Console.WriteLine(GetMemInfo());
32 }
33
34 /// <summary>
35 /// 擷取記憶體資訊
36 /// </summary>
37 /// <returns></returns>
38 public static string GetMemInfo()
39 {
40 //調用GlobalMemoryStatus函數擷取記憶體的相關資訊
41 MEMORY_INFO MemInfo = new MEMORY_INFO();
42 GlobalMemoryStatus(ref MemInfo);
43
44 StringBuilder sb = new StringBuilder();
45
46 //*%的記憶體正在使用
47 sb.Append(MemInfo.dwMemoryLoad.ToString() + "% of the memory is being used " + "\r\n");
48 //總共的實體記憶體
49 sb.Append("Physical memory total :" + Utility.ConvertBytes(MemInfo.dwTotalPhys.ToString(), 3) + "GB" + "\r\n");
50 //可使用的實體記憶體
51 sb.Append("Use of physical memory :" + Utility.ConvertBytes(MemInfo.dwAvailPhys.ToString(), 3) + "GB" + "\r\n");
52 //分頁檔總大小
53 sb.Append("Total size of the swap file" + Utility.ConvertBytes(MemInfo.dwTotalPageFile.ToString(), 3) + "GB" + "\r\n");
54 //尚可分頁檔大小為
55 sb.Append(" Can still swap file size :" + Utility.ConvertBytes(MemInfo.dwAvailPageFile.ToString(), 3) + "GB" + "\r\n");
56 //總虛擬記憶體
57 sb.Append("The Total virtual memory :" + Utility.ConvertBytes(MemInfo.dwTotalVirtual.ToString(), 3) + "GB" + "\r\n");
58 //未用虛擬記憶體有
59 sb.Append("Unused virtual memory :" + Utility.ConvertBytes(MemInfo.dwAvailVirtual.ToString(), 3) + "GB" + "\r\n");
60 // ConvertBytes(totMem, 3) + " GB"
61 return sb.ToString();
62 }
63 }

 

 1  public class Utility
2 {
3 public static decimal ConvertBytes(string b, int iteration)
4 {
5 long iter = 1;
6 for (int i = 0; i < iteration; i++)
7 iter *= 1024;
8 return Math.Round((Convert.ToDecimal(b)) / Convert.ToDecimal(iter), 2, MidpointRounding.AwayFromZero);
9 }
10 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.