How to measure actual memory usage of an applicati

2018-12-31 06:51发布

This question is covered here in great detail.

How do you measure the memory usage of an application or process in Linux?

From the blog article of Understanding memory usage on Linux, ps is not an accurate tool to use for this intent.

Why ps is "wrong"

Depending on how you look at it, ps is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running. Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported by ps are almost definitely wrong.

30条回答
冷夜・残月
2楼-- · 2018-12-31 07:04

If the process is not using up too much memory (either because you expect this to be the case, or some other command has given this initial indication), and the process can withstand being stopped for a short period of time, you can try to use the gcore command.

gcore <pid>

Check the size of the generated core file to get a good idea how much memory a particular process is using.

This won't work too well if process is using hundreds of megs, or gigs, as the core generation could take several seconds or minutes to be created depending on I/O performance. During the core creation the process is stopped (or "frozen") to prevent memory changes. So be careful.

Also make sure the mount point where the core is generated has plenty of disk space and that the system will not react negatively to the core file being created in that particular directory.

查看更多
皆成旧梦
3楼-- · 2018-12-31 07:05

Three more methods to try:

  1. ps aux --sort pmem
    It sorts the output by %MEM.
  2. ps aux | awk '{print $2, $4, $11}' | sort -k2r | head -n 15
    It sorts using pipes.
  3. top -a
    It starts top sorting by %MEM

(Extracted from here)

查看更多
君临天下
4楼-- · 2018-12-31 07:05

Another vote for here, but I would like to add that you can use a tool like Alleyoop to help you interpret the results generated by valgrind.

I use the two tools all the time and always have lean, non-leaky code to proudly show for it ;)

查看更多
情到深处是孤独
5楼-- · 2018-12-31 07:07

What about time ?

Not the Bash builtin time but the one you can find with which time, for example /usr/bin/time

Here's what it covers, on a simple ls :

$ /usr/bin/time --verbose ls
(...)
Command being timed: "ls"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 0%
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): 2372
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 1
Minor (reclaiming a frame) page faults: 121
Voluntary context switches: 2
Involuntary context switches: 9
Swaps: 0
File system inputs: 256
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
查看更多
梦醉为红颜
6楼-- · 2018-12-31 07:09

Hard to tell for sure, but here are two "close" things that can help.

$ ps aux 

will give you Virtual Size (VSZ)

You can also get detailed stats from /proc file-system by going to /proc/$pid/status

The most important is the VmSize, which should be close to what ps aux gives.

/proc/19420$ cat status
Name:   firefox
State:  S (sleeping)
Tgid:   19420
Pid:    19420
PPid:   1
TracerPid:  0
Uid:    1000    1000    1000    1000
Gid:    1000    1000    1000    1000
FDSize: 256
Groups: 4 6 20 24 25 29 30 44 46 107 109 115 124 1000 
VmPeak:   222956 kB
VmSize:   212520 kB
VmLck:         0 kB
VmHWM:    127912 kB
VmRSS:    118768 kB
VmData:   170180 kB
VmStk:       228 kB
VmExe:        28 kB
VmLib:     35424 kB
VmPTE:       184 kB
Threads:    8
SigQ:   0/16382
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000020001000
SigCgt: 000000018000442f
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
Cpus_allowed:   03
Mems_allowed:   1
voluntary_ctxt_switches:    63422
nonvoluntary_ctxt_switches: 7171

查看更多
无色无味的生活
7楼-- · 2018-12-31 07:10

Valgrind is amazing if you have the time to run it. valgrind --tool=massif is The Right Solution.

However, I'm starting to run larger examples, and using valgrind is no longer practical. Is there a way to tell the maximum memory usage (modulo page size and shared pages) of a program?

On a real unix system, /usr/bin/time -v works. On Linux, however, this does not work.

查看更多
登录 后发表回答