如何查看进程的内存占用量
ps命令
严格来说,ps命令只能看到进程内存占用的百分比。
比如下面命令可以列出最耗内存的10个进程,并将所有进程按照内存占用量从高到底排序:
ps -o pid,user,%mem,command ax |sort -k3 -brn |head
2005 lujun99+ 17.8 /usr/lib/firefox/firefox 2066 lujun99+ 17.4 /usr/lib/firefox/firefox -contentproc -childID 1 -isForBrowser -prefsLen 24973 -schedulerPrefs 0001,2 -parentBuildID 20180905135451 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 2005 true tab 2118 lujun99+ 11.6 /usr/lib/firefox/firefox -contentproc -childID 2 -isForBrowser -prefsLen 24973 -schedulerPrefs 0001,2 -parentBuildID 20180905135451 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 2005 true tab 2425 lujun99+ 9.7 /usr/lib/firefox/firefox -contentproc -childID 3 -isForBrowser -prefsLen 28794 -schedulerPrefs 0001,2 -parentBuildID 20180905135451 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 2005 true tab 2435 lujun99+ 9.0 /usr/lib/firefox/firefox -contentproc -childID 4 -isForBrowser -prefsLen 28794 -schedulerPrefs 0001,2 -parentBuildID 20180905135451 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 2005 true tab 1743 lujun99+ 5.9 awesome 1218 lujun99+ 5.4 /usr/bin/emacs --fg-daemon 1792 lujun99+ 2.3 java -ea -client -Dfile.encoding=UTF-8 -Xmx320M -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=40 -Dlog4j.defaultInitOverride=true -Djava.util.logging.config.file=/opt/nutstore/conf/java.logging.properties -Dnutstore.config.dir=/opt/nutstore/conf -Dnutstore.x64=True -Djava.library.path=/opt/nutstore/lib/native -cp /opt/nutstore/lib/guava-r07.jar:/opt/nutstore/lib/rdiff-java-0.1.0.jar:/opt/nutstore/lib/log4j-1.2.15.jar:/opt/nutstore/lib/jackson-mapper-asl-1.9.13.jar:/opt/nutstore/lib/juds-0.95-osx.jar:/opt/nutstore/lib/nutstore_client-3.4.4.jar:/opt/nutstore/lib/annotations.jar:/opt/nutstore/lib/commons-codec-1.4.jar:/opt/nutstore/lib/commons-cli-1.2.jar:/opt/nutstore/lib/sqlite4java.jar:/opt/nutstore/lib/inotify-java-2.0.2.jar:/opt/nutstore/lib/jackson-core-asl-1.9.13.jar:/opt/nutstore/lib/commons-collections4-4.1.jar:/opt/nutstore/lib/jsr305-3.0.1.jar:/opt/nutstore/lib/swt.jar nutstore.client.gui.NutstoreGUI --restart 1 1467 lujun99+ 1.3 /usr/lib/Xorg -nolisten tcp :0 vt1 -keeptty -auth /tmp/serverauth.4mFBlelDHO 1747 lujun99+ 0.9 fcitx
pmap命令
使用命令 pmap $pid
可以查看 $pid
这个进程及其依赖库的内存占用量(单位为K)
pmap $$
14931: /bin/bash 000056317f317000 820K r-x-- bash 000056317f5e4000 12K r---- bash 000056317f5e7000 16K rw--- bash 000056317f5eb000 52K rw--- [ anon ] 0000563181464000 264K rw--- [ anon ] 00007f3ea01b5000 6036K r---- locale-archive 00007f3ea079a000 8K rw--- [ anon ] 00007f3ea079c000 416K r-x-- libncursesw.so.6.1 00007f3ea0804000 2044K ----- libncursesw.so.6.1 00007f3ea0a03000 20K r---- libncursesw.so.6.1 00007f3ea0a08000 4K rw--- libncursesw.so.6.1 00007f3ea0a09000 136K r---- libc-2.28.so 00007f3ea0a2b000 1324K r-x-- libc-2.28.so 00007f3ea0b76000 304K r---- libc-2.28.so 00007f3ea0bc2000 4K ----- libc-2.28.so 00007f3ea0bc3000 16K r---- libc-2.28.so 00007f3ea0bc7000 8K rw--- libc-2.28.so 00007f3ea0bc9000 16K rw--- [ anon ] 00007f3ea0bcd000 4K r---- libdl-2.28.so 00007f3ea0bce000 4K r-x-- libdl-2.28.so 00007f3ea0bcf000 4K r---- libdl-2.28.so 00007f3ea0bd0000 4K r---- libdl-2.28.so 00007f3ea0bd1000 4K rw--- libdl-2.28.so 00007f3ea0bd2000 268K r-x-- libreadline.so.7.0 00007f3ea0c15000 2048K ----- libreadline.so.7.0 00007f3ea0e15000 12K r---- libreadline.so.7.0 00007f3ea0e18000 24K rw--- libreadline.so.7.0 00007f3ea0e1e000 12K rw--- [ anon ] 00007f3ea0e67000 8K r---- ld-2.28.so 00007f3ea0e69000 124K r-x-- ld-2.28.so 00007f3ea0e88000 32K r---- ld-2.28.so 00007f3ea0e90000 4K r---- ld-2.28.so 00007f3ea0e91000 4K rw--- ld-2.28.so 00007f3ea0e92000 4K rw--- [ anon ] 00007ffe6c74a000 132K rw--- [ stack ] 00007ffe6c78f000 12K r---- [ anon ] 00007ffe6c792000 8K r-x-- [ anon ] total 14212K
如果觉得pmap使用单位为K不好看,也可以让它跟 numfmt 命令结合
pmap $$ |numfmt --header=1 --field 2-2 --to=iec --from iec
15514: /bin/bash 00005599578b7000 820K r-x-- bash 0000559957b84000 12K r---- bash 0000559957b87000 16K rw--- bash 0000559957b8b000 52K rw--- [ anon ] 000055995938e000 264K rw--- [ anon ] 00007fcb2194e000 5.9M r---- locale-archive 00007fcb21f33000 8.0K rw--- [ anon ] 00007fcb21f35000 416K r-x-- libncursesw.so.6.1 00007fcb21f9d000 2.0M ----- libncursesw.so.6.1 00007fcb2219c000 20K r---- libncursesw.so.6.1 00007fcb221a1000 4.0K rw--- libncursesw.so.6.1 00007fcb221a2000 136K r---- libc-2.28.so 00007fcb221c4000 1.3M r-x-- libc-2.28.so 00007fcb2230f000 304K r---- libc-2.28.so 00007fcb2235b000 4.0K ----- libc-2.28.so 00007fcb2235c000 16K r---- libc-2.28.so 00007fcb22360000 8.0K rw--- libc-2.28.so 00007fcb22362000 16K rw--- [ anon ] 00007fcb22366000 4.0K r---- libdl-2.28.so 00007fcb22367000 4.0K r-x-- libdl-2.28.so 00007fcb22368000 4.0K r---- libdl-2.28.so 00007fcb22369000 4.0K r---- libdl-2.28.so 00007fcb2236a000 4.0K rw--- libdl-2.28.so 00007fcb2236b000 268K r-x-- libreadline.so.7.0 00007fcb223ae000 2.0M ----- libreadline.so.7.0 00007fcb225ae000 12K r---- libreadline.so.7.0 00007fcb225b1000 24K rw--- libreadline.so.7.0 00007fcb225b7000 12K rw--- [ anon ] 00007fcb22600000 8.0K r---- ld-2.28.so 00007fcb22602000 124K r-x-- ld-2.28.so 00007fcb22621000 32K r---- ld-2.28.so 00007fcb22629000 4.0K r---- ld-2.28.so 00007fcb2262a000 4.0K rw--- ld-2.28.so 00007fcb2262b000 4.0K rw--- [ anon ] 00007ffd184e9000 132K rw--- [ stack ] 00007ffd1859b000 12K r---- [ anon ] 00007ffd1859e000 8.0K r-x-- [ anon ] total 14M