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

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


当前回答

请务必回答这个问题。提供详细信息并分享您的研究!

对不起,我是第一次来这里,只能问问题。

使用建议:

valgrind --tool=massif --pages-as-heap=yes --massif-out-file=massif.out ./test.sh; grep mem_heap_B massif.out | sed -e 's/mem_heap_B=\(.*\)/\1/' | sort -g | tail -n 1

然后:

grep mem_heap_B massif.out
...
mem_heap_B=1150976
mem_heap_B=1150976
...

这与顶部命令在相同时刻所显示的非常不同:

14673 gu27mox   20   0 3280404 468380  19176 R 100.0  2.9   6:08.84 pwanew_3pic_com

Valgrind的测量单位是什么??

/usr/bin/time -v ./test.sh从来没有回答-你必须直接将可执行文件提供给/usr/bin/time:

/usr/bin/time -v  pwanew_3pic_compass_2008florian3_dfunc.static  card_0.100-0.141_31212_resubmit1.dat_1.140_1.180   1.140 1.180 31212


    Command being timed: "pwanew_3pic_compass_2008florian3_dfunc.static card_0.100-0.141_31212_resubmit1.dat_1.140_1.180 1.140 1.180 31212"

    User time (seconds): 1468.44
    System time (seconds): 7.37
    Percent of CPU this job got: 99%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 24:37.14
    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): 574844
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 74
    Minor (reclaiming a frame) page faults: 468880
    Voluntary context switches: 1190
    Involuntary context switches: 20534
    Swaps: 0
    File system inputs: 81128
    File system outputs: 1264
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0

其他回答

好吧,如果你真的想显示内存峰值和一些更深入的统计数据,我建议使用一个分析器,如valgrind。一个很好的valgrind前端是alleyoop。

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

因为/usr/bin/time在许多现代发行版中都不存在(取而代之的是Bash内置时间),你可以使用Busybox time实现带-v参数:

busybox time -v uname -r

它的输出类似于GNU时间输出。 Busybox在大多数Linux发行版(Debian, Ubuntu等)中都是预安装的。如果你使用Arch Linux,你可以安装它:

sudo pacman -S busybox

下面是(基于其他答案)一个非常简单的脚本,用于监视已经运行的进程。你只需要用你想要观察的进程的pid作为参数来运行它:

#!/usr/bin/env bash

pid=$1

while ps $pid >/dev/null
do
    ps -o vsz= ${pid}
    sleep 1
done | sort -n | tail -n1

使用示例:

max_mem_usage.sh 23423

Heaptrack是一个具有GUI和文本界面的KDE工具。我发现它比valgrind更适合了解进程的内存使用情况,因为它提供了更多细节和火焰图。它还更快,因为它检查valgrind的次数更少。它会给你最大的内存使用量。

不管怎样,跟踪rss和vss是误导的,因为页面可以共享,这就是为什么memusg。你真正应该做的是跟踪/proc/[pid]/smaps中Pss的和或使用pmap。GNOME系统监视器曾经这样做,但它太昂贵了。