There are couple of posts talking about this subject: when you have multiple process’s with the same name, it’s not straightforward to figure out which performance counter instance represents which process. The suggestion would be to use the process ID and the “ID Process” performance counter (This is not the essence of this post, if you know this already scroll down).
To explain this further let’s say we have two web applications with two different application pools that we wish to monitor their consumption of memory (though probably there are better ways to do it). The worker process for a web application on IIS is a w3wp.exe process, so for our two web applications we will have two w3wp.exe process’s running like the following:
When you want to create a performance counter for these processes, the Performance Monitor will use the name of the process as the name of the performance counter instance for first process only, as for the rest of the processes who got the same name, for each of these processes, the Performance Monitor will assign a suffix "#N" to the name of the instance, like the following:
But how to figure out which one is which? the answer as we stated in the introduction is to use the “ID Process” performance counter:
And here is a Stackoverflow answer that describes how to do it programmatically.
Now is my problem! and this is the essence of this post; what if the process “4468”, which is monitored by the performance counter instance “w3wp”, died? you’d think that the performance counter “w3wp” dies with it and the performance counter “w3wp#1” will still be there monitoring process “3744”….well….WRONG!
What will happen is that counters will shift by one counter up; the counter “w3wp#1” will disappear, and w3wp will pickup up process “3744”:
Which will mess all your readings up!
So the conclusion is that if you have multiple processes with the same name, and you want to monitor them in Performance Monitor, pay attention to the “ID Process” performance counter; if it changed (which means one of the processes died) then consider that all your subsequent readings are wrong. And if if you are doing it programmatically, I suggest to create the performance counter just before you want to read it; don’t hold on too long for a performance counter instance after creation.