12

I am trying to get the memory usage of my application but for some reason i am getting different size than in task manager

I am using:

enter image description here

Task manager shows that my application occupies 45mb , while when i pulling it in my code i get 85mb how can i get the same size as in task manager (without using wmi)

atikot
  • 4,761
  • 4
  • 26
  • 34

3 Answers3

7

None of the above is working for me. I have therefore found this solution

How to get application memory usage as shown in Task Manager?

by Hans Passant which is doing a great job.

string prcName = Process.GetCurrentProcess().ProcessName;
var counter_Exec = new PerformanceCounter("Process", "Working Set - Private", prcName);
double dWsp_Exec =  (double)counter_Exec.RawValue / 1024.0; <---that is the value in KB

Also, I sometimesfound a minor different (<10%) which could be related to a different update. enter image description here

The only minor drawback is that the first instruction take a long time (5") for accumulating data so it might be handled in a thread.

Community
  • 1
  • 1
Patrick
  • 3,073
  • 2
  • 22
  • 60
5

Presumably you're looking at the wrong column in "Task manager" or using the wrong property in Process class..

I guess you're looking for WorkingSet64 not PrivateMemorySize64. PrivateMemorySize64 is the amount of virtual memory allocated for the process, not the physical memory. For physical memory use WorkingSet64.

Also, you need to call process.Refresh() before accessing any of the dynamic properties in process class as it is heavily cached.

process.Refresh();
_data.MemoryUsed = (process.WorkingSet64).ConvertBytesToMegabytes().ToString(CultureInfo.InvariantCulture);
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • i added refresh and uploaded the code while debugging as you see the memory size is different, also there are not drastic changes in memory size it's pretty stable at 45-47mb~ – atikot Dec 31 '14 at 12:36
  • Don't check the process memory while debugging. Run the process out of the debugger and check. Because when debugging visual studio will run hosting process for you *YourProcess.vshost.exe*. That can cause confusion. I tested this with printing the output in console (running out of visual studio) and that worked perfectly. Also change that `Sleep(200)` to `Sleep(1000)` so that it will sync with task manager. – Sriram Sakthivel Dec 31 '14 at 12:43
  • I am trying without visual studio and debug and it is still not right, i don't see the vshost in task manager also – atikot Dec 31 '14 at 12:53
  • As I said, you're still looking at the wrong column in the task manager. You need to check the "Working set" column. If you need private working set check [this answer](http://stackoverflow.com/questions/2611141/calculate-private-working-set-memory-using-c-sharp) – Sriram Sakthivel Dec 31 '14 at 12:56
  • also WorkingSet64 shows 114mb, PrivateMemorySize64 shows 80-84mb – atikot Dec 31 '14 at 12:56
  • is there a way to get it from Process without using Performence counters or wmi? – atikot Dec 31 '14 at 12:59
  • @atikot Sorry, I don't know alternate way. What's wrong with performance counter anyway? – Sriram Sakthivel Dec 31 '14 at 13:16
  • @atikot You could also go directly to the WinAPI and [P/Invoke GetProcessMemoryInfo](http://www.pinvoke.net/default.aspx/psapi.getprocessmemoryinfo) – Chris O Jan 03 '15 at 00:31
  • Does this work across operating systems: Windows, Linux, MacOS? – DragonSpit Apr 06 '23 at 02:26
3

Try once with the following code,may be it will help

PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "ServerProcess";
PC.CounterName = "Working Set - Private";
PC.InstanceName = JSP[0].ProcessName; //Process
RAM_memorysize = PC.NextValue();            //float RAM_memorysize;
PC.Close();
PC.Dispose();