linux - ubuntu查看进程内存 - 统计进程内存

如何测量应用程序或进程的实际内存使用情况? (20)



理解Linux上内存使用的博客文章中, ps不是一个准确的工具来用于此目的。


根据你如何看待它, ps不报告进程的实际内存使用情况。 它真正在做的是显示每个进程如果是唯一进程运行时会占用多少实际内存。 当然,典型的Linux机器在任何时候都有数十个进程在运行,这意味着由ps报告的VSZ和RSS数字几乎肯定是错误的

/prox/xxx/numa_maps gives some info there: N0=??? N1=???. But this result might be lower than the actual result, as it only count those which have been touched.

Based on answer to a related question .

You may use SNMP to get the memory and cpu usage of a process in a particular device in network :)


  • the device running the process should have snmp installed and running
  • snmp should be configured to accept requests from where you will run the script below(it may be configured in snmpd.conf)
  • you should know the process id(pid) of the process you want to monitor


  • HOST-RESOURCES-MIB::hrSWRunPerfCPU is the number of centi-seconds of the total system's CPU resources consumed by this process. Note that on a multi-processor system, this value may increment by more than one centi-second in one centi-second of real (wall clock) time.

  • HOST-RESOURCES-MIB::hrSWRunPerfMem is the total amount of real system memory allocated to this process.


Process monitoring script:


echo "IP: "
read ip
echo "specfiy pid: "
read pid
echo "interval in seconds:"
read interval

while [ 1 ]
    snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfCPU.$pid
    snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfMem.$pid
    sleep $interval;

Another vote for valgrind 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 ;)

Get valgrind. give it your program to run, and it'll tell you plenty about its memory usage.

This would apply only for the case of a program that runs for some time and stops. I don't know if valgrind can get its hands on an already-running process or shouldn't-stop processes such as daemons.

I would suggest that you use atop. You can find everything about it on this page . It is capable of providing all the necessary KPI for your processes and it can also capture to a file.

If you want something quicker than profiling with Valgrind and your kernel is older and you can't use smaps, a ps with the options to show the resident set of the process (with ps -o rss,command ) can give you a quick and reasonable _aproximation_ of the real amount of non-swapped memory being used.

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 )

While this question seems to be about examining currently running processes, I wanted to see the peak memory used by an application from start to finish. Besides valgrind, you can use tstime , which is much simpler. It measures the "highwater" memory usage (RSS and virtual). From this answer .


不是Bash内建time而是您可以在which time找到的which time ,例如/usr/bin/time


$ /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

使用ps或类似的工具,您只能获得该进程分配的内存页面数量。 这个数字是正确的,但是:

  • 并不反映应用程序使用的实际内存量,而只是为其保留的内存量

  • 如果页面被共享,例如通过多个线程或通过使用动态链接的库,可能会引起误解

如果您真的想知道应用程序实际使用的内存量,则需要在分析器中运行它。 例如, valgrind可以为您提供有关所用内存量的更多信息,更重要的是,可以了解程序中可能发生的内存泄漏。 valgrind的堆分析器工具被称为'massif':

Massif是一个堆分析器。 它通过定期获取程序堆的快照来执行详细的堆分析。 它会生成一个图表,显示堆的使用情况,包括有关程序的哪些部分负责大部分内存分配的信息。 该图由一个文本或HTML文件进行补充,其中包含更多信息以确定分配最多内存的位置。 地块运行程序比正常慢20倍左右。


valgrind --tool=massif <executable> <arguments>

Massif写入内存使用快照转储(例如massif.out.12345 )。 这些提供(1)内存使用的时间线,(2)为每个快照记录程序内存分配的位置。 分析这些文件的一个很好的图形工具是massif-visualizer 。 但是我发现ms_print是valgrind附带的一个简单的基于文本的工具,已经有了很大的帮助。

要查找内存泄漏,请使用valgrind的(默认) memcheck工具。


ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | awk '{total=total + $1} END {print total}'

如果你有足够的时间来运行它,Valgrind真是太棒了。 valgrind --tool=massif是正确的解决方案。

但是,我开始运行更大的示例,并且使用valgrind不再实用。 有没有一种方法可以告诉程序的最大内存使用量(模数页面大小和共享页面)?

在真正的unix系统上, /usr/bin/time -v可以工作。 但是,在Linux上,这不起作用。

如果你的代码是C或C ++,你可以使用getrusage() ,它会返回关于你的进程的内存和时间使用情况的各种统计信息。



这个文件看起来像一个有7个整数的文本文件。 您可能最感兴趣的是该文件中的第一个(所有内存使用)和第六个(数据内存使用)数字。


$ ps aux 


您还可以通过转到/proc/$pid/status来从/ proc文件系统获取详细的统计信息

最重要的是VmSize,它应该接近ps aux给出的值。

/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

我使用htop ; 这是一个类似于Windows任务管理器的非常好的控制台程序。


sudo pmap -x <process pid>

除了答案中列出的解决方案之外,您可以使用Linux命令“top”; 它提供了正在运行的系统的动态实时视图,它为整个系统提供了CPU和内存的使用情况,并为每个程序提供了百分比:



top -p <PID>


top | grep <PROCESS NAME>


VIRT - 虚拟映像(kb):该任务使用的虚拟内存总量

RES - 常驻大小(kb):任务已使用的未交换物理内存; RES = CODE + DATA。

DATA - 数据+堆栈大小(kb):专用于非可执行代码的物理内存量,也称为“数据驻留集”大小或DRS。

SHR - 共享内存大小(kb):任务使用的共享内存量。 它只是反映了可能与其他进程共享的内存。


# Returns total memory used by process $1 in kb.
# See /proc/NNNN/smaps if you want to do something
# more interesting.


for line in $(</proc/$1/smaps)
   [[ $line =~ ^Size:\s+(\S+) ]] && ((kb += ${.sh.match[1]}))

print $kb