如何从C#中分析内存和CPU使用情况

4

我正在运行一个使用C#启动两个进程的测试。我需要获取我的进程使用的内存和CPU的峰值。如何使用托管代码来实现?(我还在Linux上使用Mono运行它)。

架构如下:进程test.exe启动两个进程:A.exeB.exe。我需要从test.exe测量进程A和B的最大内存和CPU使用率。

这样做可能吗?


最高使用的CPU?你是指百分比吗?当它正在运行时,它占用了100%的核心。当它被阻塞时,它是0%。进程的CPU使用百分比是这两个值的平均值。这真的是你想知道的吗? - Mike Dunlavey
3个回答

8
你可以使用 System.Diagnostics.Process 类来启动进程。然后,你可以检查 UserProcessorTimeTotalProcessorTimePeakWorkingSet64 等属性来检查处理器使用情况和内存使用情况。请参阅此MSDN文章 System.Diagnostics.Process

这并不告诉您处理器使用情况,只有 CPU 时间。 - Joey

3

尝试使用这个获取CPU信息

PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;

cpuCounter = new PerformanceCounter();

cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";

ramCounter = new PerformanceCounter("Memory", "Available MBytes");


public string getCurrentCpuUsage(){
        return cpuCounter.NextValue()+"%";
}

public string getAvailableRAM(){
        return ramCounter.NextValue()+"MB";
} 

编辑: 因此,在开始您的过程之前和之后,您可以检查差异。


1

或者你可以试试这个

private ulong GetCPU(ThreadEntry[] thList) 
{ 
  ulong result = 0; 
  for (int i = 0; i < thList.Length; i++) 
  { 
    ulong NewThC = 0; 
    ulong NewThE = 0; 
    ulong NewThK = 0; 
    ulong NewThU = 0; 
    if(GetThreadTimes(thList[i].ThreadID, ref NewThC, ref NewThE, ref NewThK, ref NewThU)) 
      result = result + NewThK + NewThU; 
    int error = Marshal.GetLastWin32Error(); 
//often ERROR == 6 or ERROR == 18
  } 
  return result; 
} 

//GET ALL PROCESS CPU USAGE 
private void timer1_Tick(object sender, EventArgs e) 
{ 
//reset results 
  this.textBox1.Text = string.Empty; 
//turn of timer 
  this.Enabled = false; 
  uint perm = GetCurrentPermissions();


  //SET PERMISION SO I CAN RECEIVE INFO ABOUT THREADS
  SetProcPermissions(0xFFFFFFFF);


//get all running process (OPENNETCF)       
  List<ProcessEntry> processList = new List<ProcessEntry>(ProcessEntry.GetProcesses()); 

//OLD Variables stored in list 
  if (OldResList == null || (OldResList != null && OldResList.Length !=   processList.Count)) 
    OldResList = new ulong[processList.Count]; 

//SORT by ID only for testing       
  processList.Sort(CompareProcessEntry); 
//GET ALL CPU USAGE       
  for(int i=0;i<processList.Count;i++) 
  { 
  //new value 
    ulong newRes = GetCPU( processList[i].GetThreads() ); 
  //result 
    ulong result = (newRes - OldResList[i]); 
  //valid result 
    result = result / (ulong)this.timer1.Interval; 
  //set result to the thexbox 
    this.textBox1.Text += processList[i].ExeFile + " " +  result.ToString() + " ;"; 
  //change old to new 
    OldResList[i] = newRes; 
  } 
  //sleep 
  Thread.Sleep(1000); 
  SetProcPermissions(0xFFFFFFFF);
  //start again 

  timer1.Enabled = true; 
  } 
  public static int CompareProcessEntry(ProcessEntry p1, ProcessEntry p2) 
  { 
   return p1.ProcessID.CompareTo(p2.ProcessID); 
  } 

  [DllImport("coredll")] 
  private static extern bool GetThreadTimes(uint p, ref ulong NewThC, ref ulong NewThE, ref ulong NewThK, ref ulong NewThU); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接