-2
public static void getallmemoryusage(string processName)
{
    Process[] pprocessName = Process.GetProcessesByName(processName);//.GetCurrentProcess().ProcessName;
    var counter = new PerformanceCounter("Process", "Working Set - Private", processName);
    privateMemeory = string.Format("Private memory: {0}k", counter.NextValue() / 1024);
}

Some problems here:

  1. I have a variable Process[] pprocessName but i never use it.

  2. The it is now i'm getting a value that is not the same one in the task manager.

In the task manager i see 192.6MB in my program i see 197232. Now i started playing the game use the application(process) now in task manager it show: 219.0MB in my program 224304.

Why it's now the same values and what should i do with the Process[] pprocessName variable ? Also how do i show the percentage/s for the application(process) like it show it in task manager ?

I want that this method will return the same value of memory usage and same percentage value like in the task manager.

SAm
  • 2,154
  • 28
  • 28
user3681442
  • 297
  • 3
  • 15
  • 2
    One MB = 1024 KB, 1 KB = 1024 bytes http://en.wikipedia.org/wiki/JEDEC_memory_standards#Unit_prefixes_for_semiconductor_storage_capacity – Bas May 31 '14 at 11:15
  • possible duplicate of [c# calculate CPU usage for a specific application](http://stackoverflow.com/questions/1277556/c-sharp-calculate-cpu-usage-for-a-specific-application) – Jevgeni Geurtsen May 31 '14 at 12:13

1 Answers1

0

Yes. You can get the physical amount of memory occupied by each through the WorkingSet64 property of th Process class.

MSDN says:

The amount of physical memory, in bytes, allocated for the associated process.

In your case pprocessName is the array of processes with the name "processName" So you can do:

foreach(var process in pprocessName)
{
  var memoryBytes = process.WorkingSet64;
  //...
}
quantdev
  • 23,517
  • 5
  • 55
  • 88