是否有一种工具可以运行命令行并报告峰值RAM使用总量?

我正在想象类似于/usr/bin/time的东西


当前回答

你可以使用Valgrind这样的工具来做到这一点。

其他回答

在macOS上,可以改用DTrace。“Instruments”应用是一个很好的GUI,它带有XCode afaik。

[编辑:适用于Ubuntu 14.04: /usr/bin/time -v命令确保使用完整路径。]

看起来/usr/bin/time确实给了你这个信息,如果你输入-v(这是在Ubuntu 8.10上)。例如,参见下面的最大常驻集大小:

$ /usr/bin/time -v ls /
....
        Command being timed: "ls /"
        User time (seconds): 0.00
        System time (seconds): 0.01
        Percent of CPU this job got: 250%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 0
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 315
        Voluntary context switches: 2
        Involuntary context switches: 0
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0

使用Massif: http://valgrind.org/docs/manual/ms-manual.html

'htop'是查看哪个进程使用了多少RAM.....的最佳命令

欲知详情 http://manpages.ubuntu.com/manpages/precise/man1/htop.1.html

如果进程运行了至少几秒钟,那么您可以使用以下bash脚本,它将运行给定的命令行,然后将峰值RSS打印到stderr(替代RSS您感兴趣的任何其他属性)。它有点轻量,它适用于Ubuntu 9.04中包含的ps(我不能说时间)。

#!/usr/bin/env bash
"$@" & # Run the given command line in the background.
pid=$! peak=0
while true; do
  sleep 1
  sample="$(ps -o rss= $pid 2> /dev/null)" || break
  let peak='sample > peak ? sample : peak'
done
echo "Peak: $peak" 1>&2