是否有一种工具可以运行命令行并报告最高 RAM 使用总量?
我想象着类似于 /usr/bin/time 的东西。
是否有一种工具可以运行命令行并报告最高 RAM 使用总量?
我想象着类似于 /usr/bin/time 的东西。
由于许多现代发行版中没有 /usr/bin/time
(使用 Bash 内置的 time 代替),因此您可以使用 Busybox 的 time 实现并带上 -v
参数:
因为 /usr/bin/time
在许多现代的 Linux 发行版中已经不存在了(取而代之的是 Bash 内置的 time 命令),所以你可以通过加上 -v
参数来使用 Busybox 的 time 实现。
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
memusg
。你真正应该做的是跟踪/proc/[pid]/smaps
中Pss
的总和,或者使用pmap
。GNOME system-monitor曾经这样做,但代价太高了。#!/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 --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
...
这与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