首页 文章

linux / unix进程的峰值内存使用情况

提问于
浏览
316

是否有一个工具可以运行命令行并报告总共使用了多少RAM?

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

18 回答

  • 0

    实际上/ usr / bin / time可能会做你想要的 . 就像是 .

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

    详情请见时间(1)

  • 59

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

    我受到了杨的剧本的启发,并提出了这个名为memusg的小工具 . 我只是将采样率提高到0.1来处理很短的生活过程 . 我没有监控单个进程,而是测量进程组的rss总和 . (是的,我写了很多单独的程序一起工作)它目前适用于Mac OS X和Linux . 用法必须与 time 类似:

    memusg ls -alR / >/dev/null
    

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

    在我们开始任何严肃的分析之前,有这么简单的工具来看看它是很好的 .

  • 15

    [ Edit :适用于Ubuntu 14.04: /usr/bin/time -v command 确保使用完整路径 . ]

    看起来像 /usr/bin/time 确实给你这个信息,如果你通过 -v (这是在Ubuntu 8.10) . 参见例如下面的 Maximum resident set size

    $ /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
    
  • 90

    Valgrind单线:

    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

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

  • 12

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

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

    但是其他分析工具可能会根据您的需求提供更准确的结果 .

  • 0

    如果进程运行至少几秒钟,那么你可以使用以下bash脚本,它将运行给定的命令行,然后打印到stderr峰值RSS(替代 rss 任何其他属性,你有点轻量级,它的工作原理对于我来说,包含在Ubuntu 9.04中的 ps (我不能说 time ) .

    #!/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
    
  • 0

    在MacOS Sierra上使用:

    /usr/bin/time -l commandToMeasure
    

    您可以使用 grep 来获取您想要的内容 .

  • 9

    On Linux:

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

    (不要与Bash time 内置命令混淆!所以使用 full path/usr/bin/time

    例如:

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

    On 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
          . . .
    
  • 0

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

  • 1

    您可以使用Valgrind之类的工具来执行此操作 .

  • 3

    这是(基于其他答案)一个非常简单的脚本,它监视已经运行的进程 . 您只需使用要作为参数观察的进程的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
    
  • -2
    time -f '%M' <run_program>
    
  • 7
  • 4

    'htop'是最好的命令,看看哪个进程正在使用多少内存.....

    了解更多详情http://manpages.ubuntu.com/manpages/precise/man1/htop.1.html

  • 327

    Heaptrack是具有GUI和文本界面的KDE工具 . 我发现它比valgrind更适合理解进程的内存使用情况,因为它提供了更多细节和火焰图 . 它也更快,因为它更少检查valgrind . 它为您提供峰值内存使用率 .

    无论如何,跟踪rss和vss是误导性的,因为页面可以被共享,这就是为什么 memusg . 你应该做的是跟踪 /proc/[pid]/smapsPss 的总和或使用 pmap . GNOME system-monitor以前这样做但是太贵了 .

  • 16

    用手工制作的bash脚本重新发明轮子 . 快速而干净 .

    My use case: 我想监控一台内存较少的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%你的自定义命令输出

  • 23

    在macOS上,您可以使用DTrace . “仪器”应用程序是一个很好的GUI,它带有XCode afaik .

  • 4

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

    对不起,我是第一次来这里,只能提问...

    使用建议:

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

    然后grep mem_heap_B massif.out ... mem_heap_B = 1150976 mem_heap_B = 1150976 ...

    这与“top”命令在类似时刻显示的内容非常不同:

    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
    

相关问题