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

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


当前回答

这里有一个单行程序,不需要任何外部脚本或实用程序,也不需要你通过Valgrind或time等其他程序启动进程,所以你可以将它用于任何已经运行的进程:

grep ^VmPeak /proc/$PID/status

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

其他回答

重新发明轮子,用手工制作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% 您的定制命令输出

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

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

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

因为/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