怎么测量当前应用程序的内存占用?Process类?PerformanceCounter类?
网上很多答案说用Process或PerformanceCounter,于是我用了一下的代码测试,看看它们测出的数据是不是和Process Explorer测到的一样。
class Program { static Process process = Process.GetCurrentProcess(); static PerformanceCounter privateBytesCounter = new PerformanceCounter("Process", "Private Bytes", process.ProcessName); static PerformanceCounter workingSetCounter = new PerformanceCounter("Process", "Working Set", process.ProcessName); static void Main(string[] args) { GetMeasure(); Console.WriteLine("按下回车申请大内存"); Console.ReadLine(); int[] arr = new int[10000000]; for (int i = 0; i < arr.Length; i++) { arr[i] = i; } GetMeasure(); privateBytesCounter.Dispose(); workingSetCounter.Dispose(); Console.ReadKey(); } private static void GetMeasure() { Console.WriteLine("{0,30} {1,20}", "Private bytes", "working set"); Console.WriteLine("process数据{0,15} {1,20}", process.PrivateMemorySize64 / 1024, process.WorkingSet64 / 1024); Console.WriteLine("性能计数器数据{0,12} {1,20}", privateBytesCounter.NextValue() / 1024, workingSetCounter.NextValue() / 1024); }