I once had the task of determining the following performance parameters from inside a running application:
- Total virtual memory available
- Virtual memory currently used
- Virtual memory currently used by my process
- Total RAM available
- RAM currently used
- RAM currently used by my process
- % CPU currently used
- % CPU currently used by my process
The code had to run on Windows and Linux. Even though this seems to be a standard task, finding the necessary information in the manuals (WIN32 API, GNU docs) as well as on the Internet took me several days, because there's so much incomplete/incorrect/outdated information on this topic to be found out there.
In order to save others from going through the same trouble, I thought it would be a good idea to collect all the scattered information plus what I found by trial and error here in one place.
Best Answer
Windows
Some of the above values are easily available from the appropriate Win32 API, I just list them here for completeness. Others, however, need to be obtained from the Performance Data Helper library (PDH), which is a bit "unintuitive" and takes a lot of painful trial and error to get to work. (At least it took me quite a while, perhaps I've been only a bit stupid...)
Note: for clarity all error checking has been omitted from the following code. Do check the return codes...!
Total Virtual Memory:
Note: The name "TotalPageFile" is a bit misleading here. In reality this parameter gives the "Virtual Memory Size", which is size of swap file plus installed RAM.
Virtual Memory currently used:
Same code as in "Total Virtual Memory" and then
Virtual Memory currently used by current process:
Total Physical Memory (RAM):
Same code as in "Total Virtual Memory" and then
Physical Memory currently used:
Same code as in "Total Virtual Memory" and then
Physical Memory currently used by current process:
Same code as in "Virtual Memory currently used by current process" and then
CPU currently used:
CPU currently used by current process:
Linux
On Linux the choice that seemed obvious at first was to use the POSIX APIs like
getrusage()
etc. I spent some time trying to get this to work, but never got meaningful values. When I finally checked the kernel sources themselves, I found out that apparently these APIs are not yet completely implemented as of Linux kernel 2.6!?In the end I got all values via a combination of reading the pseudo-filesystem
/proc
and kernel calls.Total Virtual Memory:
Virtual Memory currently used:
Same code as in "Total Virtual Memory" and then
Virtual Memory currently used by current process:
Total Physical Memory (RAM):
Same code as in "Total Virtual Memory" and then
Physical Memory currently used:
Same code as in "Total Virtual Memory" and then
Physical Memory currently used by current process:
Change getValue() in "Virtual Memory currently used by current process" as follows:
CPU currently used:
CPU currently used by current process:
TODO: Other Platforms
I would assume, that some of the Linux code also works for the Unixes, except for the parts that read the /proc pseudo-filesystem. Perhaps on Unix these parts can be replaced by
getrusage()
and similar functions?