Linux系统下/proc/meminfo详解
热3已有 8987 次阅读 2010-03-24 12:42 标签: Linux meminfo proc 详解 系统
/proc/meminfo Explained
March 2003
"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'.
The entries in the /proc/meminfo can helpexplain what's going on with your memory usage, if you know how to read it.
Example of "cat /proc/meminfo":
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 bothhigh-level and low-level statistics. At the top you see a quick summary of themost common values people would like to look at. Below you find the individualvalues we will discuss. First we will discuss the high-level
statistics.
High-Level Statistics
MemTotal:Total usable ram (i.e.
physical ram minus a few reserved bits and the kernelbinary code)
MemFree:Is sum of LowFree+HighFree (overall stat)
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 SwapCache
SwapCache:Memory that once was swapped out, is swapped back in but still also is in theswapfile (if memory is needed it doesn't need to be swapped out AGAIN becauseit is already in the swapfile. This saves I/O)
Detailed Level Statistics
VM Statistics
VM splits the cache pages into"active" and "inactive" memory. The idea is that if youneed memory and some cache needs to be sacrificed for that, you take it frominactive since that's expected to be not used. The vm checks what is used on aregular basis and
moves stuff around.
When you use memory, the CPU sets a bit inthe pagetable and the VM checks that bit occasionally, and based on that, itcan move pages back to active. And within active there's an order of"longest ago not used" (roughly, it's a little more complex inreality).
The longest-ago used ones can get moved to inactive. Inactive issplit 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 unlessabsolutely necessary.
Inact_dirty:
Dirty means "might need writing to disk or swap." Takes morework to free. Examples might be files that have not been written to yet. Theyaren'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 completelog ready before sending it to disk.
Inact_clean:Assumed to be easily freeable. The kernel will try to keep some clean stuffaround always to have a bit of breathing room.
Inact_target:Just a goal metric the kernel uses for making sure there are enough inactivepages around. When exceeded, the kernel will not do work to move pages fromactive to inactive. A page can also get inactive in a few other ways,
e.g. ifyou do a long sequential I/O, the kernel assumes you're not going to use thatmemory and makes it inactive preventively. So you can get more inactive pagesthan the target because the kernel marks some cache as "more likely to benever used" and lets it
cheat in the "last used" order.
Memory Statistics
HighTotal:is the total amount of memory in the high region.
Highmem is all memoryabove (approx) 860MB of physical RAM.
Kernel uses indirect tricks to accessthe 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 thememory the kernel can address directly. All kernel datastructures need to gointo 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 thatthere never is OOM (out of memory) for this workload. Normally the kernel willovercommit memory. That means, say you do a 1GB malloc, nothing happens,really.
Only when you start USING that malloc memory you will get real memoryon demand, and just as much as you use. So you sort of take a mortgage and hopethe bank doesn't go bust. Other cases might include when you mmap a file that'sshared only when you write to
it and you get a private copy of that data. Whileit normally is shared between processes. The Committed_AS is a guesstimate ofhow 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会很快地被回收。
所以从应用程序的角度来说,可用内存=系统freememory+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/statm
654 57 44 0 0 334 0
输出解释
CPU 以及CPU0。。。的每行的每个参数意思(以第一行为例)为:
参数 解释 /proc//status
Size (pages) 任务虚拟地址空间的大小 VmSize/4
Resident(pages) 应用程序正在使用的物理内存的大小 VmRSS/4
Shared(pages) 共享页数 0
Trs(pages) 程序所拥有的可执行虚拟内存的大小 VmExe/4
Lrs(pages) 被映像到任务的虚拟内存空间的库的大小 VmLib/4
Drs(pages) 程序数据段和用户态的栈的大小(VmData+ VmStk )4
dt(pages) 04
查看机器可用内存
/proc/28248/>free
total used free shared buffers cached
Mem: 1023788 926400 97388 0 134668 503688
-/+ buffers/cache: 288044 735744
Swap: 1959920 89608 1870312
我们通过free命令查看机器空闲内存时,会发现free的值很小。这主要是因为,在linux中有这么一种思想,内存不用白不用,因此它尽可能的cache和buffer一些数据,以方便下次使用。但实际上这些内存也是可以立刻拿来使用的。
所以 空闲内存=free+buffers+cached=total-used
以下转自:
http://www.ha97.com/4337.html
PS:前天有童鞋问我,为啥我的Linux系统没运行多少程序,显示的可用内存这么少?其实Linux与Win的内存管理不同,会尽量缓存内存以提高读写性能,通常叫做Cache Memory。
有时候你会发现没有什么程序在运行,但是使用top或free命令看到可用内存free项会很少,此时查看系统的 /proc/meminfo 文件,会发现有一项 Cached Memory:
输入cat /proc/meminfo查看:
MemTotal: 16425996 kB
MemFree: 5698808 kB
Buffers: 380904 kB
Cached: 9389356 kB
SwapCached: 212 kB
Active: 6569200 kB
Inactive: 3725364 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 16425996 kB
LowFree: 5698808 kB
SwapTotal: 8273464 kB
SwapFree: 8273252 kB
Dirty: 980 kB
Writeback: 0 kB
AnonPages: 524108 kB
Mapped: 24568 kB
Slab: 381776 kB
PageTables: 7496 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 16486460 kB
Committed_AS: 2143856 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 267656 kB
VmallocChunk: 34359469303 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
Hugepagesize: 2048 kB
free命令里各项内存指标说明:
total used free shared buffers cached
Mem: 16425996 10727220 5698776 0 380904 9389832
-/+ buffers/cache: 956484 15469512
Swap: 8273464 212 8273252
其中第一行用全局角度描述系统使用的内存状况:
total——总物理内存
used——已使用内存,一般情况这个值会比较大,因为这个值包括了cache+应用程序使用的内存
free——完全未被使用的内存
shared——应用程序共享内存
buffers——缓存,主要用于目录方面,inode值等(ls大目录可看到这个值增加)
cached——缓存,用于已打开的文件
总结:
total=used+free
used=buffers+cached (maybe add shared also)
第二行描述应用程序的内存使用:
前个值表示-buffers/cache——应用程序使用的内存大小,used减去缓存值
后个值表示+buffers/cache——所有可供应用程序使用的内存大小,free加上缓存值
总结:
-buffers/cache=used-buffers-cached
+buffers/cache=free+buffers+cached
第三行表示swap的使用:
used——已使用
free——未使用
什么是Cache Memory(缓存内存):
当你读写文件的时候,Linux内核为了提高读写性能与速度,会将文件在内存中进行缓存,这部分内存就是Cache Memory(缓存内存)。即使你的程序运行结束后,Cache Memory也不会自动释放。这就会导致你在Linux系统中程序频繁读写文件后,你会发现可用物理内存会很少。
其实这缓存内存(Cache Memory)在你需要使用内存的时候会自动释放,所以你不必担心没有内存可用。如果你希望手动去释放Cache Memory也是有办法的。
如何释放Cache Memory(缓存内存):
用下面的命令可以释放Cache Memory:
To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes:
echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches
注意,释放前最好sync一下,防止丢失数据。
总结:个人经验认为没必要手动释放,这种内存管理方式也是比win优胜的地方之一!因为Linux的内核内存管理机制,一般情况下不需要特意去释放已经使用的cache。这些cache起来的内容可以提高文件以及磁盘的读写速度。