使用 Proc 文件系统计算进程的内存
我正在 Perl 中通过读取 Proc 文件系统中的值来编写小型进程监视器脚本。现在我可以使用 /proc/[pid]/status 和 /proc/[pid]/io 文件。现在我想计算一个进程的内存使用情况。经过搜索,我发现内存使用情况会出现在 /proc/[pid]/statm 中。但我仍然无法弄清楚该文件需要哪些必要字段来计算内存使用情况。有人能帮我解决这个问题吗?提前致谢。
I am writing small process monitor script in Perl by reading values from Proc file system. Right now I am able to fetch number of threads, process state, number of bytes read and write using /proc/[pid]/status and /proc/[pid]/io files. Now I want to calculate the memory usage of a process. After searching, I came to know memory usage will be present /proc/[pid]/statm. But I still can't figure out what are necessary fields needed from that file to calculate the memory usage. Can anyone help me on this? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要
resident
或size
。来自 kernel.org。You likely want
resident
orsize
. From kernel.org.了解进程的“内存使用情况”是极其困难的。 VM 大小和 RSS 是已知的、可测量的值。
但你可能想要的是别的东西。在实践中,“VM 大小”似乎太高,而 RSS 通常看起来太低。
主要问题是:
因此,您确实需要考虑什么才算是“内存使用量”。
在我看来,从逻辑上讲:
我不知道有任何实用程序可以做到这一点。不过,这看起来并不简单,并且涉及(至少)读取 /proc/pid/pagemap 以及可能的其他一些 /proc 接口,其中一些是仅限 root 的。
It is extremely difficult to know what the "memory usage" of a process is. VM size and RSS are known, measurable values.
But what you probably want is something else. In practice, "VM size" seems too high and RSS often seems too low.
The main problems are:
So you really need to think about what counts as "memory usage".
It seems to me that logically:
I don't know of any utility which does this. It seems nontrivial though, and involves (at least) reading /proc/pid/pagemap and possibly some other /proc interfaces, some of which are root-only.
另一种(不太简单,但更精确)的可能性是解析 /proc/123/maps 文件,也许可以使用 pmap 实用程序。它为您提供有关“虚拟内存”(即进程的地址空间)的信息。
Another (less simple, but more precise) possibility would be to parse the
/proc/123/maps
file, perhaps by using thepmap
utility. It gives you information about the "virtual memory" (i.e. the address space of the process).