C#获取特定进程CPU和内存使用率

首先是获取特定进程对象,可以使用Process.GetProcesses()方法来获取系统中运行的所有进程,或者使用Process.GetCurrentProcess()方法来获取当前程序所对应的进程对象。当有了进程对象后,可以通过进程对象名称来创建PerformanceCounter类型对象,通过设定PerformanceCounter构造函数的参数实现获取特定进程的CPU和内存使用情况。

具体实例代码如下:

首先是获取本机中所有进程对象,分别输出某一时刻各个进程的内存使用情况:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Diagnostics;
 6 using System.Threading;
 7 
 8 namespace CSharpPerformance
 9 {//该程序可以实时监控所有进程或者指定进程的工作集、私有工作集
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //新建一个Stopwatch变量用来统计程序运行时间
15             Stopwatch watch = Stopwatch.StartNew();
16             //获取本机运行的所有进程ID和进程名,并输出哥进程所使用的工作集和私有工作集
17             foreach (Process ps in Process.GetProcesses())
18             {
19                 PerformanceCounter pf1 = new PerformanceCounter("Process", "Working Set - Private", ps.ProcessName);
20                 PerformanceCounter pf2 = new PerformanceCounter("Process", "Working Set", ps.ProcessName);
21                 Console.WriteLine("{0}:{1}  {2:N}KB", ps.ProcessName, "工作集(进程类)", ps.WorkingSet64 / 1024);
22                 Console.WriteLine("{0}:{1}  {2:N}KB", ps.ProcessName, "工作集        ", pf2.NextValue() / 1024);
23                 //私有工作集
24                 Console.WriteLine("{0}:{1}  {2:N}KB", ps.ProcessName, "私有工作集    ", pf1.NextValue() / 1024);
25 
26             }
27 
28             watch.Stop();
29             Console.WriteLine(watch.Elapsed);
30             Console.ReadLine();
31         }
32     }
33 }

其中,工作集ps.WorkingSet64是静态的,pf2.NextValue()是动态变化的,工作集包含进程运行时其独占的内存和与其他进程共享的内存的和,而私有工作集是只包含进程独占的内存。

下面一组代码可以动态显示本程序所对应的进程的CPU和内存使用率的变化:

首先是SystemInfo.cs类:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Diagnostics;
  4 using System.Threading;
  5 using System.IO;
  6 using System.Text;
  7 using System.Management;
  8 using System.Runtime.InteropServices;
  9 
 10 namespace CSharpPerformance
 11 {
 12     public class SystemInfo
 13     {
 14         private int m_ProcessorCount = 0;   //CPU个数
 15         private PerformanceCounter pcCpuLoad;   //CPU计数器
 16         private long m_PhysicalMemory = 0;   //物理内存
 17 
 18         private const int GW_HWNDFIRST = 0;
 19         private const int GW_HWNDNEXT = 2;
 20         private const int GWL_STYLE = (-16);
 21         private const int WS_VISIBLE = 268435456;
 22         private const int WS_BORDER = 8388608;
 23 
 24         #region AIP声明
 25         [DllImport("IpHlpApi.dll")]
 26         extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
 27 
 28         [DllImport("User32")]
 29         private extern static int GetWindow(int hWnd, int wCmd);
 30 
 31         [DllImport("User32")]
 32         private extern static int GetWindowLongA(int hWnd, int wIndx);
 33 
 34         [DllImport("user32.dll")]
 35         private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
 36 
 37         [DllImport("user32", CharSet = CharSet.Auto)]
 38         private extern static int GetWindowTextLength(IntPtr hWnd);
 39         #endregion
 40 
 41         #region 构造函数
 42         /// <summary>
 43         /// 构造函数,初始化计数器等
 44         /// </summary>
 45         public SystemInfo()
 46         {
 47             //初始化CPU计数器
 48             pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
 49             pcCpuLoad.MachineName = ".";
 50             pcCpuLoad.NextValue();
 51 
 52             //CPU个数
 53             m_ProcessorCount = Environment.ProcessorCount;
 54 
 55             //获得物理内存
 56             ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
 57             ManagementObjectCollection moc = mc.GetInstances();
 58             foreach (ManagementObject mo in moc)
 59             {
 60                 if (mo["TotalPhysicalMemory"] != null)
 61                 {
 62                     m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
 63                 }
 64             }
 65         }
 66         #endregion
 67 
 68         #region CPU个数
 69         /// <summary>
 70         /// 获取CPU个数
 71         /// </summary>
 72         public int ProcessorCount
 73         {
 74             get
 75             {
 76                 return m_ProcessorCount;
 77             }
 78         }
 79         #endregion
 80 
 81         #region CPU占用率
 82         /// <summary>
 83         /// 获取CPU占用率
 84         /// </summary>
 85         public float CpuLoad
 86         {
 87             get
 88             {
 89                 return pcCpuLoad.NextValue();
 90             }
 91         }
 92         #endregion
 93 
 94         #region 可用内存
 95         /// <summary>
 96         /// 获取可用内存
 97         /// </summary>
 98         public long MemoryAvailable
 99         {
100             get
101             {
102                 long availablebytes = 0;
103                 //ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Memory");
104                 //foreach (ManagementObject mo in mos.Get())
105                 //{
106                 //    availablebytes = long.Parse(mo["Availablebytes"].ToString());
107                 //}
108                 ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
109                 foreach (ManagementObject mo in mos.GetInstances())
110                 {
111                     if (mo["FreePhysicalMemory"] != null)
112                     {
113                         availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
114                     }
115                 }
116                 return availablebytes;
117             }
118         }
119         #endregion
120 
121         #region 物理内存
122         /// <summary>
123         /// 获取物理内存
124         /// </summary>
125         public long PhysicalMemory
126         {
127             get
128             {
129                 return m_PhysicalMemory;
130             }
131         }
132         #endregion
133 
134         #region 结束指定进程
135         /// <summary>
136         /// 结束指定进程
137         /// </summary>
138         /// <param name="pid">进程的 Process ID</param>
139         public static void EndProcess(int pid)
140         {
141             try
142             {
143                 Process process = Process.GetProcessById(pid);
144                 process.Kill();
145             }
146             catch { }
147         }
148         #endregion
149 
150 
151         #region 查找所有应用程序标题
152         /// <summary>
153         /// 查找所有应用程序标题
154         /// </summary>
155         /// <returns>应用程序标题范型</returns>
156         public static List<string> FindAllApps(int Handle)
157         {
158             List<string> Apps = new List<string>();
159 
160             int hwCurr;
161             hwCurr = GetWindow(Handle, GW_HWNDFIRST);
162 
163             while (hwCurr > 0)
164             {
165                 int IsTask = (WS_VISIBLE | WS_BORDER);
166                 int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE);
167                 bool TaskWindow = ((lngStyle & IsTask) == IsTask);
168                 if (TaskWindow)
169                 {
170                     int length = GetWindowTextLength(new IntPtr(hwCurr));
171                     StringBuilder sb = new StringBuilder(2 * length + 1);
172                     GetWindowText(hwCurr, sb, sb.Capacity);
173                     string strTitle = sb.ToString();
174                     if (!string.IsNullOrEmpty(strTitle))
175                     {
176                         Apps.Add(strTitle);
177                     }
178                 }
179                 hwCurr = GetWindow(hwCurr, GW_HWNDNEXT);
180             }
181 
182             return Apps;
183         }
184         #endregion     
185     }
186 }

然后是执行代码:

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Diagnostics;
 6 using System.Threading;
 7 
 8 namespace CSharpPerformance
 9 {//该程序可以实时监控程序本身对应进程的工作集、私有工作集和CPU使用率
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //获取当前进程对象
15             Process cur = Process.GetCurrentProcess();
16 
17             PerformanceCounter curpcp = new PerformanceCounter("Process", "Working Set - Private", cur.ProcessName);
18             PerformanceCounter curpc = new PerformanceCounter("Process", "Working Set", cur.ProcessName);
19             PerformanceCounter curtime = new PerformanceCounter("Process", "% Processor Time", cur.ProcessName);
20 
21             //上次记录CPU的时间
22             TimeSpan prevCpuTime = TimeSpan.Zero;
23             //Sleep的时间间隔
24             int interval = 1000;
25 
26             PerformanceCounter totalcpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
27 
28             SystemInfo sys = new SystemInfo();
29             const int KB_DIV = 1024;
30             const int MB_DIV = 1024 * 1024;
31             const int GB_DIV = 1024 * 1024 * 1024;
32             while (true)
33             {
34                 //第一种方法计算CPU使用率
35                 //当前时间
36                 TimeSpan curCpuTime = cur.TotalProcessorTime;
37                 //计算
38                 double value = (curCpuTime - prevCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100;
39                 prevCpuTime = curCpuTime;
40 
41                 Console.WriteLine("{0}:{1}  {2:N}KB CPU使用率:{3}", cur.ProcessName, "工作集(进程类)", cur.WorkingSet64 / 1024,value);//这个工作集只是在一开始初始化,后期不变
42                 Console.WriteLine("{0}:{1}  {2:N}KB CPU使用率:{3}", cur.ProcessName, "工作集        ", curpc.NextValue() / 1024,value);//这个工作集是动态更新的
43                 //第二种计算CPU使用率的方法
44                 Console.WriteLine("{0}:{1}  {2:N}KB CPU使用率:{3}%", cur.ProcessName, "私有工作集    ", curpcp.NextValue() / 1024,curtime.NextValue()/Environment.ProcessorCount);
45                 //Thread.Sleep(interval);
46 
47                 //第一种方法获取系统CPU使用情况
48                 Console.Write("\r系统CPU使用率:{0}%", totalcpu.NextValue());
49                 //Thread.Sleep(interval);
50 
51                 //第二章方法获取系统CPU和内存使用情况
52                 Console.Write("\r系统CPU使用率:{0}%,系统内存使用大小:{1}MB({2}GB)", sys.CpuLoad, (sys.PhysicalMemory - sys.MemoryAvailable) / MB_DIV, (sys.PhysicalMemory - sys.MemoryAvailable) / (double)GB_DIV);
53                 Thread.Sleep(interval);
54             }
55 
56             Console.ReadLine();
57         }
58     }
59 }

 

以上程序可以正常运行,没隔1S刷新一次,实现动态显示本程序对应进程的CPU和内存使用情况。

 

 

 

posted @ 2013-08-27 21:01  maowang  阅读(47470)  评论(5编辑  收藏  举报