-2

I want to get the RAM (in MB) and CPU (%) using of my program in C# but I don't know how.

2

2 Answers 2

0

For ram it's pretty easy you can be use variables of the environment static class.

One such variable is Environment.WorkingSet, i'll let you look up MSDN for the others and their differences.

For cpu it's more complex because cpu usage of a program varies a lot over time unlike ram that only does when you expect it (programs get scheduled / priorized by Windows, could be 100% cpu usage during a few miliseconds, 0% next few miliseconds, if you checked it you'd see either 0 to 100 while you're looking for an average).

You'll have to query it (using WMI i assume) and average it every second or so to get something interesting.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try to use PerformanceCounter Class:-

private PerformanceCounter theCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName); 

You may need to add this to use the above:-

using System.Diagnostics; 

Also check How to get the CPU Usage in C#? and Process Properties and this MSDN forum for CPU usage

3 Comments

He asks about CPU & RAM and you answer with drive size?
@RonanThibaudau: Updated that!
Removed my downvote as a consequence

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.