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

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


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


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


[编辑:适用于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

/usr/bin/time可能是你想要的。喜欢的东西。

 /usr/bin/time --format='(%Xtext+%Ddata %Mmax)'

详见第(1)段。


也许(gnu) time(1)已经做了您想要的。例如:

$ /usr/bin/time -f "%P %M" command
43% 821248

但其他分析工具可能会根据您正在寻找的内容提供更准确的结果。


如果进程运行了至少几秒钟,那么您可以使用以下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

(这是一个已经回答了的老问题。但只是为了记录:)

我受到杨的剧本的启发,想出了这个小工具,名叫memusg。我只是将采样率提高到0.1,以处理更短的生存过程。我没有监视单个进程,而是让它测量进程组的rss和。(是的,我写了很多独立的程序一起工作)它目前在Mac OS X和Linux上工作。用法必须和time的用法相似:

memusg ls -alR / >/dev/null

它只显示了目前的峰值,但我对记录其他(粗略)统计数据的轻微扩展感兴趣。

在我们开始任何严肃的分析之前,有这样一个简单的工具来查看是很好的。


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


下面是(基于其他答案)一个非常简单的脚本,用于监视已经运行的进程。你只需要用你想要观察的进程的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

Valgrind一行程序:

Valgrind——tool=massif——pages-as-heap=yes——massif-out-file=massif。出去。/ test.sh;grep mem_heap_B massif。出| sed - e ' s / mem_heap_B = \(. * \) / \ 1 /“| - g | tail - n 1

注意使用——pages-as-heap来测量进程中的所有内存。更多信息请访问:http://valgrind.org/docs/manual/ms-manual.html

这将大大降低您的命令速度。


在MacOS Sierra上使用:

/usr/bin/time -l commandToMeasure

你可以用grep获取你想要的。


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

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


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

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


重新发明轮子,用手工制作bash脚本。快速干净。

我的用例:我想监控一台RAM较少的linux机器,并想在它运行在大量使用时获取每个容器使用情况的快照。

#!/usr/bin/env bash

threshold=$1

echo "$(date '+%Y-%m-%d %H:%M:%S'): Running free memory monitor with threshold $threshold%.."

while(true)
    freePercent=`free -m | grep Mem: | awk '{print ($7/$2)*100}'`    
  do

  if (( $(awk 'BEGIN {print ("'$freePercent'" < "'$threshold'")}') ))
  then
       echo "$(date '+%Y-%m-%d %H:%M:%S'): Free memory $freePercent% is less than $threshold%"
       free -m
       docker stats --no-stream
       sleep 60  
       echo ""  
  else
       echo "$(date '+%Y-%m-%d %H:%M:%S'): Sufficient free memory available: $freePercent%"
  fi
  sleep 30

done

样例输出:

2017-10-12 13:29:33:使用阈值30%运行空闲内存监视器。 2017-10-12 13:29:33:可用空闲内存充足:69.4567% 2017-10-12 13:30:03:可用空闲内存充足:69.4567% 2017-10-12 16:47:02:空闲内存18.9387%小于30% 您的定制命令输出


在Linux上:

使用/usr/bin/time -v <program> <args>查找“最大常驻集大小”。

(不要与Bash time内置命令混淆!所以使用完整路径,/usr/bin/time)

例如:

> /usr/bin/time -v ./myapp
        User time (seconds): 0.00
        . . .
        Maximum resident set size (kbytes): 2792
        . . .

在BSD、MacOS上:

使用/usr/bin/time -l <program> <args>,查找“最大常驻集大小”:

>/usr/bin/time -l ./myapp
        0.01 real         0.00 user         0.00 sys
      1440  maximum resident set size
      . . .

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


time -f '%M' <run_program>

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

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

使用建议:

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或time等其他程序启动进程,所以你可以将它用于任何已经运行的进程:

grep ^VmPeak /proc/$PID/status

(将$PID替换为您感兴趣的进程的PID)


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

busybox time -v uname -r

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

sudo pacman -S busybox