Peak memory usage of a linux/unix process

2019-01-03 04:02发布

Is there a tool that will run a command-line and report how much RAM was used total?

I'm imagining something analogous to /usr/bin/time

18条回答
甜甜的少女心
2楼-- · 2019-01-03 04:12

On macOS, you can use DTrace instead. The "Instruments" app is a nice GUI for that, it comes with XCode afaik.

查看更多
爷的心禁止访问
3楼-- · 2019-01-03 04:14

Valgrind one-liner:

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

Note use of --pages-as-heap to measure all memory in a process. More info here: http://valgrind.org/docs/manual/ms-manual.html

查看更多
疯言疯语
4楼-- · 2019-01-03 04:16

If the process runs for at least a couple seconds, then you can use the following bash script, which will run the given command line then print to stderr the peak RSS (substitute for rss any other attribute you're interested in). It's somewhat lightweight, and it works for me with the ps included in Ubuntu 9.04 (which I can't say for 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
查看更多
Explosion°爆炸
5楼-- · 2019-01-03 04:16

Heaptrack is KDE tool that has a GUI and text interface. I find it more suitable than valgrind to understand the memory usage of a process because it provides more details and flamegraphs. It's also faster because it does less checking that valgrind. And it gives you the peak memory usage.

Anyway, tracking rss and vss is misleading because pages could be shared, that's why that memusg. What you should really do is track the sum of Pss in /proc/[pid]/smaps or use pmap. GNOME system-monitor used to do so but it was too expensive.

查看更多
▲ chillily
6楼-- · 2019-01-03 04:18
time -f '%M' <run_program>
查看更多
乱世女痞
7楼-- · 2019-01-03 04:18

Re-inventing the wheel, with hand made bash script. Quick and clean.

My use case: I wanted to monitor a linux machine which has less RAM and wanted to take a snapshot of per container usage when it runs under heavy usage.

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

Sample output:

2017-10-12 13:29:33: Running free memory monitor with threshold 30%..

2017-10-12 13:29:33: Sufficient free memory available: 69.4567%

2017-10-12 13:30:03: Sufficient free memory available: 69.4567%

2017-10-12 16:47:02: Free memory 18.9387% is less than 30%

your custom command output

查看更多
登录 后发表回答