Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a Windows Performance Counter for the current process

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?

like image 539
Dave Avatar asked Mar 25 '26 07:03

Dave


2 Answers

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.

like image 50
Remus Rusanu Avatar answered Mar 27 '26 20:03

Remus Rusanu


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)?.

like image 21
Gabe Avatar answered Mar 27 '26 19:03

Gabe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!