How to get current CPU and RAM usage in C++?

后端 未结 9 965
后悔当初
后悔当初 2020-12-01 06:44

is it possible, in C++, to get the current RAM and CPU usage? Is there a platform-indepentent function call?

相关标签:
9条回答
  • 2020-12-01 07:08

    I notice that ACE is ported to vcpkg which would make it easy to compile & link a cross-platform C++ app.

    In C++ I'd like to monitor available system CPU and memory resources, so that my app can flex its consumption in response to resource availability.

    Is anyone please able to offer an ACE code snippet to get started on this?

    0 讨论(0)
  • 2020-12-01 07:09

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

    0 讨论(0)
  • 2020-12-01 07:14

    Sadly these things rely heavily on the underlying OS, so there are no platform-independent calls. (Maybe there are some wrapper frameworks, but I don't know of any.)

    On Linux you could have a look at the getrusage() function call, on Windows you can use GetProcessMemoryInfo() for RAM Usage. Have also a look at the other functions in the Process Status API of Windows.

    0 讨论(0)
提交回复
热议问题