Reputation: 501
I want to get my application pool's memory usage using Process class. This is my code so far:
ServerManager serverManager = new ServerManager();
ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools;
Process process = Process.GetProcessById(applicationPoolCollection.Where(p => p.Name == "mywebsite.com").First().WorkerProcesses[0].ProcessId);
I can get Process class but how do I know what is the amount of RAM it is using currently? I do see properties like PagedMemorySize64
and NonpagedSystemMemorySize64
. Which one would give me the correct RAM that it is occupying at the moment?
I cannot use PerformanceCounter
like so:
PerformanceCounter performanceCounter = new PerformanceCounter();
performanceCounter.CategoryName = "Process";
performanceCounter.CounterName = "Working Set";
performanceCounter.InstanceName = process.ProcessName;
Console.WriteLine(((uint)performanceCounter.NextValue() / 1024).ToString("N0"));
The reason being ProcessName
would return w3p and therefore PerformanceCounter would write the total IIS RAM usage in Console which is not what I want.
I just need the application pool's RAM usage. Therefore I think my answer lies in Process class and not PerformanceCounter
Any help is appreciated.
Upvotes: 0
Views: 1948
Reputation: 62256
If you need simply amount of ram visible to the process, which means amount of ram assigned to the process, but not necessary that every single bit of it is actually used by the process, you can use: WorkingSet. (Choose appropriate version for your .net)
Just invite your attention on the fact, that there is much more into process memory diagnostics, in case you might be concerned about allocated Virtual Memory, Commited Memory, Mapped Files and whatnot.
Upvotes: 1