Trying to programmatically get the private working set of a process.
Currently I am able to get the working set without issue but having trouble getting the private working set.
Here's method:
private void GetProcessesForServer(string serverName)
{
var runningProcesses = new Process[0];
try
{
runningProcesses = Process.GetProcesses(serverName);
}
catch (Exception e)
{
ResultsPanel.Controls.Add(new Label { Text = string.Format("There was an error: {0}", e.GetBaseException().Message) });
}
IOrderedEnumerable<CustomProcess> processes = runningProcesses
.Select(process => new CustomProcess(process.Id, process.ProcessName, (process.WorkingSet64 / 1024)))
.ToList()
.OrderBy(process => process.ProcessName);
if (processes.Count() > 0)
ResultsLabel.Text = string.Format("Current running processes on {0}", ServerNamesDropDown.SelectedItem.Text);
ResultsGridView.DataSource = processes;
ResultsGridView.DataBind();
}
So I'm passing in a server name then trying to get all the running processes for that server then binding the list of processes to a grid view. Everything works without any issues however I need to get the private working set - similar to what you see in Windows Task manager - rather than the total working set.
Many thanks, Tim
On Windows Vista and beyond there is the "Working Set - Private" performance counter in the "Process" category (see msdn).
Given you are on such a platform you could use the System.Diagonstics.PerformanceCounter class to query this information.
To establish a link between a process ID and a given performance counter instance, use the "ID Process" counter of a category. In other words: lookup the instance where the "ID Process" counter is your desired process ID, the read the value of the "Working Set - Private" counter.
Hint: if you need to query all values for all processes use the System.Diagonstics.PerformanceCounterCategory.ReadCategory() call instead, as it is much faster the reading individual counters for all processes/instances.
Update: There is an article on codeproject that shows how to calculate that value on XP/2000, if you must. I have not tested it, so don't blame me ;-)
Update 2: You may also want to checkout this stackoverflow question/answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With