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

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


当前回答

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

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

其他回答

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

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

重新发明轮子,用手工制作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
      . . .

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

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