Under the ".NET CLR Memory" performance counter category, there is a counter named "Process ID". Any given instance of this counter is supposed to return the Process Id of the Windows process associated with the instance. For my own processes, this counter always has a zero value. Why is that?
If I look at the
devenv instance of the counter, it returns the expected value - at least, the value matches the process id reported by Task Manager.
If I run my own application in Debug mode and examine the
PerfCounterTest.vshost instance of the counter, it only ever has the value zero.
If I run my own application without the debugger, the
PerfCounterTest instance is always zero.
If I run several instances of my own application, the sequence-numbered instances
PerfCounterTest#2 ... all have the value zero.
Why is this counter always zero and, seeing as it is always zero, how do I find the instance-name associated with my process, bearing in mind that it may not have a unique name?
Best How To :
As it happens, an instance of this counter will have the value zero until the first garbage collection occurs in the process associated with that instance.
This fact is documented in the counter help text associated with the "Process ID" counter:
This counter displays the process ID of the CLR process instance being monitored. The value displayed will be 0 until after the first garbage collection.
The obscurity stems from the fact that, while you can easily read this text using stuff in the
System.Diagnostics namespace, there is no other documentation on the matter. I searched the MSDN, TechNet and the web and found nothing. Even after discovering the answer, I opened up PerfMon and tried to find this string in the U.I. - to the best of my knowledge, it is not displayed anywhere.
For future reference, here's how you interrogate the counter help text:
using (Process process = Process.GetCurrentProcess())
using (PerformanceCounter performanceCounter = new PerformanceCounter(".NET CLR Memory", "Process ID", process.ProcessName, true))