如何在 C++ 中获取当前 CPU 和 RAM 使用情况?
在 C++ 中是否可以获取当前 RAM 和 CPU 使用情况? 是否存在平台无关的函数调用?
is it possible, in C++, to get the current RAM and CPU usage? Is there a platform-indepentent function call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不,没有,标准中没有。
如果您确实需要此信息,则必须编写特定于平台的#ifdefs 或链接到提供该信息的库。
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.
在 Linux 上,这将使用 /proc/self/status 。 需要做更多的工作才能将其转化为数字。 我发现这很有用,只是将内存使用情况作为字符串直接打印到屏幕上。
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.
没有独立于平台的方法可以做到这一点。 尽管对于Windows,您可以通过在代码中使用PDH.dll(Performance Data Helper)及其相关API来获取CPU使用率和性能指标。
以下是有关如何使用它的更多信息。
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.
不直接。
但您可以使用抽象操作系统的库(例如 ACE)。
不过,如果您只需要 CPU 和内存,这可能会有点重。
Not directly.
But you can use a library that abstracts the OS (such as ACE).
Though this might by a bit heavy if you just want CPU and Memory.
如果情况仍然如此,请检查:
http://sourceforge.net/projects/cpp-cpu -monitor/
它为您提供了如何获取 Linux 的 CPU 和 RAM 使用情况的示例(在 Debian 和 CentOS 上测试)以及如何安装的非常简单的说明。
如果您对这个小项目有任何疑问,请随时询问。
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.
我注意到 ACE 已移植到 vcpkg 这将使编译变得容易 链接跨平台 C++ 应用程序。
在 C++ 中,我想监视可用的系统 CPU 和内存资源,以便我的应用程序可以根据资源可用性来调整其消耗。
有人可以提供 ACE 代码片段来开始这方面的工作吗?
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?
遗憾的是,这些东西严重依赖于底层操作系统,因此不存在独立于平台的调用。 (也许有一些包装框架,但我不知道。)
在 Linux 上,您可以查看 getrusage() 函数调用,在 Windows 上您可以使用 GetProcessMemoryInfo() 了解 RAM 使用情况。 还可以查看 进程状态 API< 中的其他函数Windows 的 /a>。
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.
有一个开源库可以跨多个平台提供这些(以及更多系统信息):SIGAR API
我已经在相当大的项目中使用过它并且运行良好(除了 OS X 等上的某些极端情况)
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.)
据我所知,没有一个独立于平台的功能可以实现这一点。 如果您计划针对多个版本的 Windows,请注意某些版本的实现会有所不同。 例如,我在 NT 3.51 下测试应用程序时遇到了这个问题......(过时,我知道)。
这是我用于内存方面的一些代码。 这不适用于 Windows 以外的平台,并且在没有 WIN32 定义的情况下编译时只会返回 0:
编辑:我忘了提及,此代码除以并向下舍入到最近的 MB,因此 >> 到处都是20个。
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.