Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to profile memory and CPU usage from C#

I'm running a test that launches two processes, using C#. I need to get the top memory and CPU used by my process. Please, someone could give me a guideline about how to do it using managed code? (I also run it on linux using mono).

The architecture is the following:

The process test.exe launches two processes: A.exe and B.exe. I need to measure meausre max memory and CPU for processes A and B, from test.exe

Is it possible to do? Thanks in advance

like image 234
Daniel Peñalba Avatar asked Dec 17 '22 01:12

Daniel Peñalba


2 Answers

You can use the System.Diagnostics.Process class to start the process. You can then check the UserProcessorTime, TotalProcessorTime, PeakWorkingSet64 and other properties to check processor usage and memory usage. Check this MSDN Article for System.Diagnostics.Process.

like image 116
Espen Burud Avatar answered Dec 24 '22 11:12

Espen Burud


Try use this Get CPU Info

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";
} 

EDIT: So you can check difference before you start your process and after.

like image 44
Likurg Avatar answered Dec 24 '22 11:12

Likurg