如何计算命令使用的内存?

How to Calculate Memory Used By A Command?

考虑我是 运行 一个命令 :

cprogram.c using gcc command

以下命令使用的内存:

gcc cprogram.c

注意: 不应使用 top 和 ps 命令。

我需要其他方法来计算命令使用的内存,并且此内存应该仅特定于此命令。

如果您在 Linux,您可以直接访问源 ps 用途:有关内存使用情况的信息(以及其他信息)公开在 /proc 文件系统上。例如,以 root 身份尝试 cat /proc/1234/status,其中 1234 是进程的 pid。

请注意,这可能不会计算 gcc 子进程使用的内存。

您可以使用/usr/bin/time获取命令使用的峰值内存:

/usr/bin/time -l ps

输出

  PID TTY           TIME CMD
97160 ttys000    0:00.01 -bash
97167 ttys000    0:00.00 /usr/bin/time -l ps
        0.00 real         0.00 user         0.00 sys
    946176  maximum resident set size                     <-- peak memory usage
         0  average shared memory size
         0  average unshared data size
         0  average unshared stack size
       244  page reclaims
         0  page faults
         0  swaps
         3  block input operations
         0  block output operations
         0  messages sent
         0  messages received
         0  signals received
         5  voluntary context switches
         0  involuntary context switches