我在使用 ps aux
命令时想到了这个问题。
在这里我可以看到一些进程处于 0% CPU 0% MEM 0 VSZ 0 RSS。
如果守护进程没有使用任何内存,为什么以及如何首先显示它?我有点理解 0% CPU 使用率意味着该进程当前未在使用,但 0% MEM 不意味着根本没有进程吗?
我想检查这是否是特定于系统守护进程的,所以我制作了一个简单的 C 程序,它带有一个无限循环,没有任何变量。
void main()
{
while (1){}
}
这次 VSZ 和 RSS 有实际值,而 MEM 保持在 0%。
这里发生了什么?
最佳答案
%MEM
可能未在您的系统上完整记录。关于 ps
命令的 AIX 手册说:
%MEM
Calculated as the sum of the number of working segment and code segment pages in memory times 4 (that is, the RSS value), divided by the size of the real memory in use, in the machine in KB, times 100, rounded to the nearest full percentage point. This value attempts to convey the percentage of real memory being used by the process. Unfortunately, like RSS, it tends the exaggerate the cost of a process that is sharing program text with other processes. Further, the rounding to the nearest percentage point causes all of the processes in the system that have RSS values under 0.005 times real memory size to have a %MEM of 0.0.
正如您通过检查输出所怀疑的那样,应用了一些舍入。因此,如果值太低,则会打印 %0.0。
而且,这个测量的是实际内存使用的百分比,这意味着它不反射(reflect)进程的大小,而只反射(reflect)进程的哪一部分实际映射到实际内存。
在您的第一种情况下,CPU 的 %0.0 仅表示该进程存在但实际上什么都不做,并且它可能处于等待状态(或消耗非常小的处理能力百分比),而不是“当前不是”正在使用”。在你的第二种情况下,你的进程是事件的,它实际上很忙(这是 %97.7
反射(reflect)的),但它所做的是愚蠢的(无限循环什么都不做)。
要了解所有这些,您可以阅读有关进程状态、进程调度和虚拟内存的内容。
关于linux - 守护进程如何在不使用内存的情况下保持事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54248101/