Linux 查看進(jìn)程消耗內(nèi)存情況總結(jié)
本文轉(zhuǎn)載自微信公眾號(hào)「DBA閑思雜想錄」,作者瀟湘隱者 。轉(zhuǎn)載本文請(qǐng)聯(lián)系DBA閑思雜想錄公眾號(hào)。
在Linux中,有很多命令或工具用來(lái)查看內(nèi)存使用情況,今天我們來(lái)簡(jiǎn)單討論一下如何查看進(jìn)程消耗、占用的內(nèi)存情況,Linux的內(nèi)存管理和相關(guān)概念要比Windows復(fù)雜一些。在此之前,我們需要了解一下Linux系統(tǒng)下面有關(guān)內(nèi)存的專(zhuān)用名詞和專(zhuān)業(yè)術(shù)語(yǔ)概念:
物理內(nèi)存和虛擬內(nèi)存
物理內(nèi)存:就是系統(tǒng)硬件提供的內(nèi)存大小,是真正的內(nèi)存,一般叫做內(nèi)存條。也叫隨機(jī)存取存儲(chǔ)器(random access memory,RAM)又稱(chēng)作“隨機(jī)存儲(chǔ)器”,是與CPU直接交換數(shù)據(jù)的內(nèi)部存儲(chǔ)器,也叫主存(內(nèi)存)。
虛擬內(nèi)存:相對(duì)于物理內(nèi)存,在Linux下還有一個(gè)虛擬內(nèi)存的概念,虛擬內(nèi)存就是為了滿(mǎn)足物理內(nèi)存的不足而提出的策略,它是利用磁盤(pán)空間虛擬出的一塊邏輯內(nèi)存,用作虛擬內(nèi)存的磁盤(pán)空間被稱(chēng)為交換空間(Swap Space)。Linux會(huì)在物理內(nèi)存不足時(shí),使用虛擬內(nèi)存,內(nèi)核會(huì)把暫時(shí)不用的內(nèi)存塊信息寫(xiě)到虛擬內(nèi)存,這樣物理內(nèi)存就得到了釋放,這塊兒內(nèi)存就可以用于其他目的,而需要用到這些內(nèi)容的時(shí)候,這些信息就會(huì)被重新從虛擬內(nèi)存讀入物理內(nèi)存。
Linux的buffers與cached
在Linux系統(tǒng)中經(jīng)常發(fā)現(xiàn)空閑的內(nèi)存很少,似乎所有的內(nèi)存都被消耗殆盡了,表面上看是內(nèi)存不夠用了,很多新手看到內(nèi)存被“消耗殆盡”非常緊張,其實(shí)這個(gè)是因?yàn)長(zhǎng)inux系統(tǒng)將空閑的內(nèi)存用來(lái)做磁盤(pán)文件數(shù)據(jù)的緩存。這個(gè)導(dǎo)致你的系統(tǒng)看起來(lái)處于內(nèi)存非常緊急的狀況。但是實(shí)際上不是這樣。這個(gè)區(qū)別于Windows的內(nèi)存管理。Linux會(huì)利用空閑的內(nèi)存來(lái)做cached & buffers。
buffers是指用來(lái)給塊設(shè)備做的緩沖大小(塊設(shè)備的讀寫(xiě)緩沖區(qū)),它只記錄文件系統(tǒng)的metadata以及 tracking in-flight pages.
Buffers are associated with a specific block device, and cover caching of filesystem metadata as well as tracking in-flight pages. The cache only contains parked file data. That is, the buffers remember what's in directories, what file permissions are, and keep track of what memory is being written from or read to for a particular block device. The cache only contains the contents of the files themselves.
cached是作為page cache的內(nèi)存, 文件系統(tǒng)的cache。你讀寫(xiě)文件的時(shí)候,Linux內(nèi)核為了提高讀寫(xiě)性能與速度,會(huì)將文件在內(nèi)存中進(jìn)行緩存,這部分內(nèi)存就是Cache Memory(緩存內(nèi)存)。即使你的程序運(yùn)行結(jié)束后,Cache Memory也不會(huì)自動(dòng)釋放。這就會(huì)導(dǎo)致你在Linux系統(tǒng)中程序頻繁讀寫(xiě)文件后,你會(huì)發(fā)現(xiàn)可用物理內(nèi)存會(huì)很少。其實(shí)緩存內(nèi)存(Cache Memory)在你需要使用內(nèi)存的時(shí)候會(huì)自動(dòng)釋放,所以你不必?fù)?dān)心沒(méi)有內(nèi)存可用
Cached is the size of the page cache. Buffers is the size of in-memory block I/O buffers. Cached matters; Buffers is largely irrelevant.
Cached is the size of the Linux page cache, minus the memory in the swap cache, which is represented by SwapCached (thus the total page cache size is Cached + SwapCached). Linux performs all file I/O through the page cache. Writes are implemented as simply marking as dirty the corresponding pages in the page cache; the flusher threads then periodically write back to disk any dirty pages. Reads are implemented by returning the data from the page cache; if the data is not yet in the cache, it is first populated. On a modern Linux system, Cached can easily be several gigabytes. It will shrink only in response to memory pressure. The system will purge the page cache along with swapping data out to disk to make available more memory as needed.
Buffers are in-memory block I/O buffers. They are relatively short-lived. Prior to Linux kernel version 2.4, Linux had separate page and buffer caches. Since 2.4, the page and buffer cache are unified and Buffers is raw disk blocks not represented in the page cache—i.e., not file data. The Buffers metric is thus of minimal importance. On most systems, Buffers is often only tens of megabytes.
Linux共享內(nèi)存
共享內(nèi)存是進(jìn)程間通信中最簡(jiǎn)單的方式之一。共享內(nèi)存允許兩個(gè)或更多進(jìn)程訪(fǎng)問(wèn)同一塊內(nèi)存,就如同 malloc() 函數(shù)向不同進(jìn)程返回了指向同一個(gè)物理內(nèi)存區(qū)域的指針。當(dāng)一個(gè)進(jìn)程改變了這塊地址中的內(nèi)容的時(shí)候,其它進(jìn)程都會(huì)察覺(jué)到這個(gè)。其實(shí)所謂共享內(nèi)存,就是多個(gè)進(jìn)程間共同地使用同一段物理內(nèi)存空間,它是通過(guò)將同一段物理內(nèi)存映射到不同進(jìn)程的虛擬空間來(lái)實(shí)現(xiàn)的。由于映射到不同進(jìn)程的虛擬空間中,不同進(jìn)程可以直接使用,不需要像消息隊(duì)列那樣進(jìn)行復(fù)制,所以共享內(nèi)存的效率很高。共享內(nèi)存可以通過(guò)mmap()映射普通文件機(jī)制來(lái)實(shí)現(xiàn),也可以System V共享內(nèi)存機(jī)制來(lái)實(shí)現(xiàn),System V是通過(guò)映射特殊文件系統(tǒng)shm中的文件實(shí)現(xiàn)進(jìn)程間的共享內(nèi)存通信,也就是說(shuō)每個(gè)共享內(nèi)存區(qū)域?qū)?yīng)特殊文件系統(tǒng)shm中的一個(gè)文件。
另外,我們還必須了解RSS、PSS、USS等相關(guān)概念:
- VSS – Virtual Set Size 虛擬耗用內(nèi)存(包含共享庫(kù)占用的內(nèi)存)
- RSS – Resident Set Size 實(shí)際使用物理內(nèi)存(包含共享庫(kù)占用的內(nèi)存)
- PSS – Proportional Set Size 實(shí)際使用的物理內(nèi)存(比例分配共享庫(kù)占用的內(nèi)存)
- USS – Unique Set Size 進(jìn)程獨(dú)自占用的物理內(nèi)存(不包含共享庫(kù)占用的內(nèi)存)
RSS(Resident set size),使用top命令可以查詢(xún)到,是最常用的內(nèi)存指標(biāo),表示進(jìn)程占用的物理內(nèi)存大小。但是,將各進(jìn)程的RSS值相加,通常會(huì)超出整個(gè)系統(tǒng)的內(nèi)存消耗,這是因?yàn)镽SS中包含了各進(jìn)程間共享的內(nèi)存。
PSS(Proportional set size)所有使用某共享庫(kù)的程序均分該共享庫(kù)占用的內(nèi)存時(shí),每個(gè)進(jìn)程占用的內(nèi)存。顯然所有進(jìn)程的PSS之和就是系統(tǒng)的內(nèi)存使用量。它會(huì)更準(zhǔn)確一些,它將共享內(nèi)存的大小進(jìn)行平均后,再分?jǐn)偟礁鬟M(jìn)程上去。
USS(Unique set size )進(jìn)程獨(dú)自占用的內(nèi)存,它是PSS中自己的部分,它只計(jì)算了進(jìn)程獨(dú)自占用的內(nèi)存大小,不包含任何共享的部分。
所以下面介紹的命令,有些查看進(jìn)程的虛擬內(nèi)存使用,有些是查看進(jìn)程的RSS或?qū)嶋H物理內(nèi)存。在講述的時(shí)候,我們會(huì)標(biāo)注這些信息。
top命令查看
執(zhí)行top命令后,執(zhí)行SHIFT +F ,可以選擇按某列排序,例如選擇n后,就會(huì)按字段%MEM排序
當(dāng)然也可以使用shift+m 或大寫(xiě)鍵M 讓top命令按字段%MEM來(lái)排序,當(dāng)然你也可以按VIRT(虛擬內(nèi)存)、SWAP(進(jìn)程使用的SWAP空間)、RES(實(shí)際使用物理內(nèi)存,當(dāng)然這里由于涉及共享內(nèi)存緣故,你看到的實(shí)際內(nèi)存非常大)
- %MEM -- Memory usage (RES)
- A task's currently used share of available physical memory
- VIRT -- virtual memory
- The total amount of virtual memory used by the task. It includes all code, data and shared libraries plus pages that have been swapped out. (Note: you can define the STATSIZE=1 environment variable and the VIRT will be calculated from the /proc/#/state VmSize field.)
- VIRT = SWAP + RES
- SWAP -- Swapped size (kb)
- The swapped out portion of a task’s total virtual memory image.
- RES -- Resident size (kb)
- RES = CODE + DATA.
是否有人會(huì)覺(jué)得奇怪,為什么%MEM這一列的值加起來(lái)會(huì)大于100呢?這個(gè)是因?yàn)檫@里計(jì)算的時(shí)候包含了共享內(nèi)存的緣故,另外由于共享內(nèi)存的緣故,你看到進(jìn)程使用VIRT或RES都非常高。由于大部分的物理內(nèi)存通常在多個(gè)應(yīng)用程序之間共享,名為實(shí)際使用物理內(nèi)存(RSS,對(duì)應(yīng)top命令里面的RES)的這個(gè)標(biāo)準(zhǔn)的內(nèi)存耗用衡量指標(biāo)會(huì)大大高估內(nèi)存耗用情況。
ps命令查看
使用ps命令找出占用內(nèi)存資源最多的20個(gè)進(jìn)程(數(shù)量可以任意設(shè)置)
- # ps aux | head -1;ps aux |grep -v PID |sort -rn -k +4 | head -20
- USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
- oracle 32147 11.0 51.2 13252080 12666320 ? Rs Aug24 163:16 ora_s000_SCM2
- oracle 32149 14.2 50.9 13250344 12594264 ? Ss Aug24 210:41 ora_s001_SCM2
- oracle 32153 4.2 49.6 13250820 12279432 ? Ss Aug24 62:27 ora_s003_SCM2
- oracle 32155 2.5 48.6 13250268 12040732 ? Ss Aug24 38:21 ora_s004_SCM2
- oracle 32157 1.2 44.5 13250296 11011708 ? Ss Aug24 18:31 ora_s005_SCM2
- oracle 32151 2.7 39.7 13350436 9829944 ? Ss Aug24 41:18 ora_s002_SCM2
- oracle 32159 0.5 38.9 13250704 9625764 ? Ss Aug24 8:18 ora_s006_SCM2
- oracle 32161 0.2 26.3 13250668 6507244 ? Ss Aug24 3:38 ora_s007_SCM2
- oracle 32129 0.0 25.5 13299084 6324644 ? Ss Aug24 1:25 ora_dbw0_SCM2
- oracle 32181 0.0 15.8 13250152 3913260 ? Ss Aug24 0:56 ora_s017_SCM2
- oracle 32145 2.7 15.3 13255256 3786456 ? Ss Aug24 40:11 ora_d000_SCM2
- oracle 32127 0.0 15.2 13248996 3762860 ? Ss Aug24 0:05 ora_mman_SCM2
- oracle 32163 0.0 14.2 13250108 3525160 ? Ss Aug24 1:04 ora_s008_SCM2
- oracle 32165 0.0 8.1 13250172 2007704 ? Ss Aug24 0:37 ora_s009_SCM2
- oracle 32169 0.0 6.6 13250060 1656864 ? Ss Aug24 0:08 ora_s011_SCM2
- oracle 32177 0.0 6.0 13250148 1498760 ? Ss Aug24 0:12 ora_s015_SCM2
- oracle 32187 0.0 5.1 13250084 1267384 ? Ss Aug24 0:06 ora_s020_SCM2
- oracle 32179 0.0 5.1 13250584 1280156 ? Ss Aug24 0:05 ora_s016_SCM2
- oracle 32167 0.0 5.0 13250060 1248668 ? Ss Aug24 0:08 ora_s010_SCM2
- oracle 32175 0.0 3.4 13250596 857380 ? Ss Aug24 0:03 ora_s014_SCM2
- #ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -n -r | less
查看進(jìn)程占用的實(shí)際物理內(nèi)存(與smem看到實(shí)際物理內(nèi)存大小有出入,這里解釋一下:SIZE: 進(jìn)程使用的地址空間, 如果進(jìn)程映射了100M的內(nèi)存, 進(jìn)程的地址空間將報(bào)告為100M內(nèi)存. 事實(shí)上, 這個(gè)大小不是一個(gè)程序?qū)嶋H使用的內(nèi)存數(shù). 所以這里看到的內(nèi)存跟smem看到的大小有出入)
- 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 "" }' |cut -d "" -f2 | cut -d "-" -f1
- ps aux | awk '{print $6/1024 " MB\t\t" $11}' | sort -n
smem命令查看
關(guān)于smem命令,這里不做介紹,直接參考鏈接Linux監(jiān)控工具介紹系列——smem
- #smem -rs pss
pmap命令查看
- # ps -ef | grep tomcat
- # pmap 32341
- # pmap -x 32341
The -x option can be used to provide information about the memory allocation and mapping types per mapping. The amount of resident, non-shared anonymous, and locked memory is shown for each mapping。
python腳本查看
網(wǎng)上有個(gè)python腳本計(jì)算程序或進(jìn)程的內(nèi)存使用情況,地址位于https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py
python ps_mem.py
- [root@mylnx03 ~]# python ps_mem.py -h
- Usage: ps_mem [OPTION]...
- Show program core memory usage
- -h, -help Show this help
- -p <pid>[,pid2,...pidN] Only show memory usage PIDs in the specified list
- -s, --split-args Show and separate by, all command line arguments
- -t, --total Show only the total value
- -d, --discriminate-by-pid Show by process rather than by program
- -S, --swap Show swap information
- -w <N> Measure and show process memory every N seconds
- [root@mylnx03 ~]# python ps_mem.py -p 32341
- Private + Shared = RAM used Program
- 411.2 MiB + 184.0 KiB = 411.4 MiB java
- ---------------------------------
- 411.4 MiB
- =================================
參考資料:
https://stackoverflow.com/questions/131303/how-to-measure-actual-memory-usage-of-an-application-or-process
http://www.cnblogs.com/kerrycode/p/5079319.html
https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py