linux/unix 进程的峰值内存使用量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/774556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Peak memory usage of a linux/unix process
提问by jes5199
Is there a tool that will run a command-line and report the peak RAM usage total?
是否有工具可以运行命令行并报告峰值 RAM 使用总量?
I'm imagining something analogous to /usr/bin/time
我正在想象类似于 /usr/bin/time 的东西
采纳答案by erobertc
Here's a one-liner that doesn't require any external scripts or utilities and doesn't require you to start the process via another program like Valgrind or time, so you can use it for any process that's already running:
这是一个单行程序,不需要任何外部脚本或实用程序,也不需要您通过另一个程序(如 Valgrind 或 time)启动进程,因此您可以将它用于任何已经在运行的进程:
grep VmPeak /proc/$PID/status
(replace $PID
with the PID of the process you're interested in)
(替换$PID
为您感兴趣的进程的 PID)
回答by alexn
回答by Dana the Sane
You can use a tool like Valgrindto do this.
您可以使用Valgrind 之类的工具来执行此操作。
回答by Jacob Gabrielson
[Edit: Works on Ubuntu 14.04: /usr/bin/time -v command
Make sure to use the full path.]
[编辑:适用于 Ubuntu 14.04:/usr/bin/time -v command
确保使用完整路径。]
Looks like /usr/bin/time
does give you that info, if you pass -v
(this is on Ubuntu 8.10). See, e.g., Maximum resident set size
below:
/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
回答by simon
/usr/bin/time maybe does what you want, actually. Something like.
/usr/bin/time 实际上可能会做你想要的。就像是。
/usr/bin/time --format='(%Xtext+%Ddata %Mmax)'
See time(1) for details...
详情见时间(1)...
回答by Jon Ericson
回答by Yang
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
).
如果该过程运行至少几秒钟,那么您可以使用以下 bash 脚本,该脚本将运行给定的命令行,然后将峰值 RSS 打印到 stderr(替换rss
您感兴趣的任何其他属性)。它有点轻量级,它适用于ps
包含在 Ubuntu 9.04 中的我(我不能说它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
回答by netj
(This is an already answered, old question.. but just for the record :)
(这是一个已经回答的老问题..但只是为了记录:)
I was inspired by Yang's script, and came up with this small tool, named memusg. I simply increased the sampling rate to 0.1 to handle much short living processes. Instead of monitoring a single process, I made it measure rss sum of the process group. (Yeah, I write lots of separate programs that work together) It currently works on Mac OS X and Linux. The usage had to be similar to that of time
:
我受到了 Yang 脚本的启发,想出了这个名为memusg 的小工具。我只是将采样率增加到 0.1 来处理很短的生命过程。我没有监控单个进程,而是让它测量进程组的 rss 总和。(是的,我编写了许多可以协同工作的独立程序)它目前可以在 Mac OS X 和 Linux 上运行。用法必须类似于time
:
memusg ls -alR / >/dev/null
It only shows the peak for the moment, but I'm interested in slight extensions for recording other (rough) statistics.
它只显示目前的峰值,但我对记录其他(粗略)统计数据的轻微扩展感兴趣。
It's good to have such simple tool for just taking a look before we start any serious profiling.
在我们开始任何认真的分析之前,有这样一个简单的工具来看看是件好事。
回答by Hans W
回答by static_rtti
Here is (based on the other answers) a very simple script that watches an already running process. You just run it with the pid of the process you want to watch as the argument:
这是(基于其他答案)一个非常简单的脚本,用于监视已经运行的进程。您只需使用要查看的进程的 pid 作为参数运行它:
#!/usr/bin/env bash
pid=
while ps $pid >/dev/null
do
ps -o vsz= ${pid}
sleep 1
done | sort -n | tail -n1
Example usage:
用法示例:
max_mem_usage.sh 23423
回答by jbeard4
Valgrind one-liner:
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
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
注意使用 --pages-as-heap 来测量进程中的所有内存。更多信息在这里:http: //valgrind.org/docs/manual/ms-manual.html