C中当前进程的内存使用情况 [英] Memory usage of current process in C
问题描述
我需要在 C 中获取当前进程的内存使用情况.有人可以提供如何在 Linux 平台上执行此操作的代码示例吗?
I need to get the memory usage of the current process in C. Can someone offer a code sample of how to do this on a Linux platform?
我知道获取内存使用情况的 cat/proc/
方法,但我不知道如何在 C 中捕获它.
I'm aware of the cat /proc/<your pid>/status
method of getting memory usage, but I have no idea how to capture that in C.
顺便说一句,这是我正在修改的 PHP 扩展(当然,我是 C 新手).如果 PHP 扩展 API 中有可用的快捷方式,那就更有用了.
BTW, it's for a PHP extension I'm modifying (granted, I'm a C newbie). If there are shortcuts available within the PHP extension API, that would be even more helpful.
推荐答案
您始终可以像打开常规文件一样打开 /proc
系统中的文件"(使用self"符号链接,因此您不必查找自己的 pid):
You can always just open the 'files' in the /proc
system as you would a regular file (using the 'self' symlink so you don't have to look up your own pid):
FILE* status = fopen( "/proc/self/status", "r" );
当然,您现在必须解析文件以挑选出您需要的信息.
Of course, you now have to parse the file to pick out the information you need.
这篇关于C中当前进程的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!