4
I need to get the memory and CPU usage of an application in c#, with the code below (using PerformanceCounter
) did not get the same Task Manager value from Windows.
PerformanceCounter cpu;
PerformanceCounter ram;
cpu = new PerformanceCounter("Process", "% Processor Time", "Servidor - estoque", true);
ram = new PerformanceCounter("Process", "Private Bytes", "Servidor - estoque", true);
int Cpu = Convert.ToInt32(cpu.NextValue());
int Ram = Convert.ToInt32(ram.NextValue());
CPU.Value = Cpu;
RAM.Value = (Ram/1024/1024);
How do I make the spent value of memory and CPU of a given application to be the same demonstrated in Task Manager?
Does this code demonstrate the application’s use of actual memory? I have tested and not shown the demonstrated use in Task Manager
– Marlon Pereira
This code shows the amount of memory used (and not shared), at the moment, by the process. Note that the value is displayed in bytes and that the value in the task manager is shown in kB, so to get the value in kB you have to divide the value by 1024.
– Omni
I advise you to read this article (in English) on the different types of memory of a process.
– Omni