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:19

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:

#!/usr/bin/env bash

pid=$1

while ps $pid >/dev/null
do
    ps -o vsz= ${pid}
    sleep 1
done | sort -n | tail -n1

Example usage:

max_mem_usage.sh 23423
查看更多
爷的心禁止访问
3楼-- · 2019-01-03 04:21

(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:

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.

查看更多
可以哭但决不认输i
4楼-- · 2019-01-03 04:26

Perhaps (gnu) time(1) already does what you want. For instance:

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

But other profiling tools may give more accurate results depending on what you are looking for.

查看更多
倾城 Initia
5楼-- · 2019-01-03 04:26

'htop' is best command for see which process is using how much RAM.....

for more detail http://manpages.ubuntu.com/manpages/precise/man1/htop.1.html

查看更多
劫难
6楼-- · 2019-01-03 04:30

[Edit: Works on Ubuntu 14.04: /usr/bin/time -v command Make sure to use the full path.]

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 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
查看更多
倾城 Initia
7楼-- · 2019-01-03 04:31

Well, if you really want to show the memory peak and some more in-depth statistics i recommend using a profiler such as valgrind. A nice valgrind front-end is alleyoop.

查看更多
登录 后发表回答