is it possible, in C++, to get the current RAM and CPU usage? Is there a platform-indepentent function call?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
On Linux, this will use /proc/self/status . More work is required to turn this into a number. I find this useful as it is though, just to print the memory usage directly to the screen as a string.
No, there isn't, not in the standard.
If you truly need this information, you will have to write platform-specific #ifdefs or link against a library that provides it.
There is not a platform independent function for this that I know of. IF you plan to target multiple versions of Windows be aware that the implementation differs across some versions. I hit this problem when testing an app under NT 3.51 for instance... (archaic, I know).
Here is some code I used for the memory side of things. This doesn't work across platforms other than windows, and will just return 0 when compiled without the WIN32 define:
EDIT: I forgot to mention, this code divides and rounds down to the nearest MB, hence the >> 20 all over the place.
There is an open source library that gives these (and more system info stuff) across many platforms: SIGAR API
I've used it in fairly large projects and it works fine (except for certain corner cases on OS X etc.)
If that is still the case please check:
http://sourceforge.net/projects/cpp-cpu-monitor/
It gives you an example how to get CPU and RAM usage of a Linux (tested on Debian and CentOS) and a quite simple instruction of how to install.
Please feel free to ask if you have any questions regarding this small project.
There is no platform independent way to do this. Although for windows, you can get the CPU usage and performance metrics by using PDH.dll(Performance Data Helper) and its related APIs in your code.
Here's more on how to use it.