3

I am writing code for a C++ application foo.exe. Inside the application, I want to log its address space usage. So the performance counter I want to look at is "\Process(foo)\Virtual Bytes". The problem I am encountering is that there may be multiple instances of foo.exe running on the system. In perfmon, I can see that these are referenced as "\Process(foo#2)\Virtual Bytes" where the #2 could be any number depending on the number of processes.

How can I construct a counter path to get the virtual bytes for the current process that handles the case where there are multiples instances of the process?

Dave
  • 885
  • 2
  • 10
  • 20

2 Answers2

4

Your instance is the one that has the ID Process counter value that matches your current process ID. Unfortunately there is no other way than to get all instances and enumerate through them untill you find the instance that is yours, but that is fairly trivial to do.

Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • 1
    Be careful to always be sure that the `ID Process` value matches your PID. Just because you're `foo#2` now doesn't mean `foo` won't go away and turn you into `foo#1`! – Gabe Dec 08 '10 at 05:23
  • 1
    @Gabe: I think that the instances are named at 'birth'. Once created as `foo#2`, it cannot change its name untill the process terminates. – Remus Rusanu Dec 08 '10 at 05:25
  • 2
    Remus: That's not my experience from using Perfmon. – Gabe Dec 08 '10 at 05:27
  • Thanks for the information. I wouldn't say it's trivial to get this information, but I made it work. It's disappointing that the library has such a bad interface. – Dave Dec 08 '10 at 19:43
  • I was surprised! Gabe is correct -- the instance # changes. Only the `ID Process` counter can be relied upon. – Alan McBee Jan 21 '12 at 06:44
1

If all you want is the Virtual Bytes (or other memory statistics) for your process, it's much easier to use GetProcessMemoryInfo, as described in How to determine a process "virtual size" (WinXP)?.

Community
  • 1
  • 1
Gabe
  • 84,912
  • 12
  • 139
  • 238