/proc/meminfo Explainedlinux
"Free," "buffer," "swap," "dirty." What does it all mean? If you said, "something to do with the Summer of '68", you may need a primer on 'meminfo'.web
The entries in the /proc/meminfo can help explain what's going on with your memory usage, if you know how to read it.緩存
Example of "cat /proc/meminfo":oracle
root: total: used: free: shared: buffers: cached:
Mem: 1055760384 1041887232 13873152 0 100417536 711233536
Swap: 1077501952 8540160 1068961792
MemTotal: 1031016 kB
MemFree: 13548 kB
MemShared: 0 kB
Buffers: 98064 kB
Cached: 692320 kB
SwapCached: 2244 kB
Active: 563112 kB
Inact_dirty: 309584 kB
Inact_clean: 79508 kB
Inact_target: 190440 kB
HighTotal: 130992 kB
HighFree: 1876 kB
LowTotal: 900024 kB
LowFree: 11672 kB
SwapTotal: 1052248 kB
SwapFree: 1043908 kB
Committed_AS: 332340 kB
The information comes in the form of both high-level and low-level statistics. At the top you see a quick summary of the most common values people would like to look at. Below you find the individual values we will discuss. First we will discuss the high-level statistics.app
MemTotal: Total usable ram (i.e. physical ram minus a few reserved bits and the kernel binary code)less
MemFree: Is sum of LowFree+HighFree (overall stat)ide
MemShared: 0; is here for compat reasons but always zero.工具
Buffers: Memory in buffer cache. mostly useless as metric nowadays性能
Cached: Memory in the pagecache (diskcache) minus SwapCacheui
SwapCache: Memory that once was swapped out, is swapped back in but still also is in the swapfile (if memory is needed it doesn't need to be swapped out AGAIN because it is already in the swapfile. This saves I/O)
VM splits the cache pages into "active" and "inactive" memory. The idea is that if you need memory and some cache needs to be sacrificed for that, you take it from inactive since that's expected to be not used. The vm checks what is used on a regular basis and moves stuff around.
When you use memory, the CPU sets a bit in the pagetable and the VM checks that bit occasionally, and based on that, it can move pages back to active. And within active there's an order of "longest ago not used" (roughly, it's a little more complex in reality). The longest-ago used ones can get moved to inactive. Inactive is split into two in the above kernel (2.4.18-24.8.0). Some have it three.
Active: Memory that has been used more recently and usually not reclaimed unless absolutely necessary.
Inact_dirty: Dirty means "might need writing to disk or swap." Takes more work to free. Examples might be files that have not been written to yet. They aren't written to memory too soon in order to keep the I/O down. For instance, if you're writing logs, it might be better to wait until you have a complete log ready before sending it to disk.
Inact_clean: Assumed to be easily freeable. The kernel will try to keep some clean stuff around always to have a bit of breathing room.
Inact_target: Just a goal metric the kernel uses for making sure there are enough inactive pages around. When exceeded, the kernel will not do work to move pages from active to inactive. A page can also get inactive in a few other ways, e.g. if you do a long sequential I/O, the kernel assumes you're not going to use that memory and makes it inactive preventively. So you can get more inactive pages than the target because the kernel marks some cache as "more likely to be never used" and lets it cheat in the "last used" order.
HighTotal: is the total amount of memory in the high region. Highmem is all memory above (approx) 860MB of physical RAM. Kernel uses indirect tricks to access the high memory region. Data cache can go in this memory region.
LowTotal: The total amount of non-highmem memory.
LowFree: The amount of free memory of the low memory region. This is the memory the kernel can address directly. All kernel datastructures need to go into low memory.
SwapTotal: Total amount of physical swap memory.
SwapFree: Total amount of swap memory free.
Committed_AS: An estimate of how much RAM you would need to make a 99.99% guarantee that there never is OOM (out of memory) for this workload. Normally the kernel will overcommit memory. That means, say you do a 1GB malloc, nothing happens, really. Only when you start USING that malloc memory you will get real memory on demand, and just as much as you use. So you sort of take a mortgage and hope the bank doesn't go bust. Other cases might include when you mmap a file that's shared only when you write to it and you get a private copy of that data. While it normally is shared between processes. The Committed_AS is a guesstimate of how much RAM/swap you would need worst-case.
在Linux下查看內存咱們通常用free命令:
[root@scs-2 tmp]# free
total used free shared buffers cached
Mem: 3266180 3250004 16176 0 110652 2668236
-/+ buffers/cache: 471116 2795064
Swap: 2048276 80160 1968116
下面是對這些數值的解釋:
total:總計物理內存的大小。
used:已使用多大。
free:可用有多少。
Shared:多個進程共享的內存總額。
Buffers/cached:磁盤緩存的大小。
第三行(-/+ buffers/cached):
used:已使用多大。
free:可用有多少。
第四行就很少解釋了。
區 別:第二行(mem)的used/free與第三行(-/+ buffers/cache) used/free的區別。這兩個的區別在於使用的角度來看,第一行是從OS的角度來看,由於對於OS,buffers/cached 都是屬於被使用,因此他的可用內存是16176KB,已用內存是3250004KB,其中包括,內核(OS)使用+Application(X, oracle,etc)使用的+buffers+cached.
第三行所指的是從應用程序角度來看,對於應用程序來講,buffers/cached 是等於可用的,由於buffer/cached是爲了提升文件讀取的性能,當應用程序需在用到內存的時候,buffer/cached會很快地被回收。
因此從應用程序的角度來講,可用內存=系統free memory+buffers+cached。
如上例:
2795064=16176+110652+2668236
接下來解釋何時內存會被交換,以及按什麼方交換。 當可用內存少於額定值的時候,就會開會進行交換。
如何看額定值:
cat /proc/meminfo
[root@scs-2 tmp]# cat /proc/meminfo
MemTotal: 3266180 kB
MemFree: 17456 kB
Buffers: 111328 kB
Cached: 2664024 kB
SwapCached: 0 kB
Active: 467236 kB
Inactive: 2644928 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 3266180 kB
LowFree: 17456 kB
SwapTotal: 2048276 kB
SwapFree: 1968116 kB
Dirty: 8 kB
Writeback: 0 kB
Mapped: 345360 kB
Slab: 112344 kB
Committed_AS: 535292 kB
PageTables: 2340 kB
VmallocTotal: 536870911 kB
VmallocUsed: 272696 kB
VmallocChunk: 536598175 kB
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 2048 kB
用free -m查看的結果:
[root@scs-2 tmp]# free -m
total used free shared buffers cached
Mem: 3189 3173 16 0 107 2605
-/+ buffers/cache: 460 2729
Swap: 2000 78 1921
查看/proc/kcore文件的大小(內存鏡像):
[root@scs-2 tmp]# ll -h /proc/kcore
-r-------- 1 root root 4.1G Jun 12 12:04 /proc/kcore
備註:
佔用內存的測量
測量一個進程佔用了多少內存,linux爲咱們提供了一個很方便的方法,/proc目錄爲咱們提供了全部的信息,實際上top等工具也經過這裏來獲取相應的信息。
/proc/meminfo 機器的內存使用信息
/proc/pid/maps pid爲進程號,顯示當前進程所佔用的虛擬地址。
/proc/pid/statm 進程所佔用的內存
[root@localhost ~]# cat /proc/self/statm654 57 44 0 0 334 0輸出解釋CPU 以及CPU0。。。的每行的每一個參數意思(以第一行爲例)爲:參數 解釋 /proc//statusSize (pages) 任務虛擬地址空間的大小 VmSize/4Resident(pages) 應用程序正在使用的物理內存的大小 VmRSS/4Shared(pages) 共享頁數 0Trs(pages) 程序所擁有的可執行虛擬內存的大小 VmExe/4Lrs(pages) 被映像到任務的虛擬內存空間的庫的大小 VmLib/4Drs(pages) 程序數據段和用戶態的棧的大小 (VmData+ VmStk )4dt(pages) 04查看機器可用內存/proc/28248/>freetotal used free shared buffers cachedMem: 1023788 926400 97388 0 134668 503688-/+ buffers/cache: 288044 735744Swap: 1959920 89608 1870312咱們經過free命令查看機器空閒內存時,會發現free的值很小。這主要是由於,在linux中有這麼一種思想,內存不用白不用,所以它儘量的cache和buffer一些數據,以方便下次使用。但實際上這些內存也是能夠馬上拿來使用的。因此 空閒內存=free+buffers+cached=total-used