/proc/meminfo文件分析

本文详细解析了/proc/meminfo中各项内存指标的含义,包括MemTotal、MemFree、Buffers、Cached等,并介绍了这些指标如何帮助理解系统的内存使用情况。

from : http://blog.youkuaiyun.com/liujishen/article/details/4979503


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":

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.

High-Level Statistics

  • MemTotal: Total usable ram (i.e. physical ram minus a few reserved bits and the kernel binary 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 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)

Detailed Level Statistics
VM Statistics

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.

Memory Statistics

  • 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.

> cat /proc/meminfo 读出的内核信息进行解释,

下篇文章会简单对读出该信息的代码进行简单的分析。

MemTotal: 507480 kB
MemFree: 10800 kB
Buffers: 34728 kB
Cached: 98852 kB
SwapCached: 128 kB
Active: 304248 kB
Inactive: 46192 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 507480 kB
LowFree: 10800 kB
SwapTotal: 979956 kB
SwapFree: 941296 kB
Dirty: 32 kB
Writeback: 0 kB
AnonPages: 216756 kB
Mapped: 77560 kB
Slab: 22952 kB
SReclaimable: 15512 kB
SUnreclaim: 7440 kB
PageTables: 2640 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 1233696 kB
Committed_AS: 828508 kB
VmallocTotal: 516088 kB
VmallocUsed: 5032 kB
VmallocChunk: 510580 kB



相应选项中文意思想各位高手已经知道,如何翻译有什么错误,请务必指出:

MemTotal: 所有可用RAM大小 (即物理内存减去一些预留位和内核的二进制代码大小)

MemFree: LowFree与HighFree的总和,被系统留着未使用的内存

Buffers: 用来给文件做缓冲大小

Cached: 被高速缓冲存储器(cache memory)用的内存的大小(等于 diskcacheminusSwapCache ).

SwapCached:被高速缓冲存储器(cache memory)用的交换空间的大小
已经被交换出来的内存,但仍然被存放在swapfile中。用来在需要的时候很快的被替换而不需要再次打开I/O端口。

Active: 在活跃使用中的缓冲或高速缓冲存储器页面文件的大小,除非非常必要否则不会被移作他用.

Inactive: 在不经常使用中的缓冲或高速缓冲存储器页面文件的大小,可能被用于其他途径.

HighTotal:
HighFree: 该区域不是直接映射到内核空间。内核必须使用不同的手法使用该段内存。

LowTotal:
LowFree: 低位可以达到高位内存一样的作用,而且它还能够被内核用来记录一些自己的数据结构。Among many
other things, it is where everything from the Slab is
allocated. Bad things happen when you're out of lowmem.

SwapTotal: 交换空间的总大小

SwapFree: 未被使用交换空间的大小

Dirty: 等待被写回到磁盘的内存大小。

Writeback: 正在被写回到磁盘的内存大小。

AnonPages:未映射页的内存大小

Mapped: 设备和文件等映射的大小。

Slab: 内核数据结构缓存的大小,可以减少申请和释放内存带来的消耗。

SReclaimable:可收回Slab的大小

SUnreclaim:不可收回Slab的大小(SUnreclaim+SReclaimable=Slab)

PageTables:管理内存分页页面的索引表的大小。

NFS_Unstable:不稳定页表的大小

Bounce:

CommitLimit: Based on the overcommit ratio ('vm.overcommit_ratio'),
this is the total amount of memory currently available to
be allocated on the system. This limit is only adhered to
if strict overcommit accounting is enabled (mode 2 in
'vm.overcommit_memory').
The CommitLimit is calculated with the following formula:
CommitLimit = ('vm.overcommit_ratio' * Physical RAM) + Swap
For example, on a system with 1G of physical RAM and 7G
of swap with a `vm.overcommit_ratio` of 30 it would
yield a CommitLimit of 7.3G.
For more details, see the memory overcommit documentation
in vm/overcommit-accounting.

Committed_AS: The amount of memory presently allocated on the system.
The committed memory is a sum of all of the memory which
has been allocated by processes, even if it has not been
"used" by them as of yet. A process which malloc()'s 1G
of memory, but only touches 300M of it will only show up
as using 300M of memory even if it has the address space
allocated for the entire 1G. This 1G is memory which has
been "committed" to by the VM and can be used at any time
by the allocating application. With strict overcommit
enabled on the system (mode 2 in 'vm.overcommit_memory'),
allocations which would exceed the CommitLimit (detailed
above) will not be permitted. This is useful if one needs
to guarantee that processes will not fail due to lack of
memory once that memory has been successfully allocated.

VmallocTotal: 可以vmalloc虚拟内存大小

VmallocUsed: 已经被使用的虚拟内存大小。

VmallocChunk: largest contigious block of vmalloc area which is free



下面简单来个例子,看看已用内存和物理内存大小..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int MemInfo(char* Info, int len);
int main()
{
char buf[128];

memset(buf, 0, 128);
MemInfo(buf, 100);
printf("%s", buf);
return 0;
}
int MemInfo(char* Info, int len)
{
char sStatBuf[256];
FILE* fp;
int flag;
int TotalMem;
int UsedMem;
char* line;
if(system("free -m | awk '{print $2,$3}' > mem"));
memset(sStatBuf, 0, 256);
fp = fopen("mem", "rb");
if(fp < 0)
{
return -1;
}
fread(sStatBuf,1, sizeof(sStatBuf) , fp);

line = strstr(sStatBuf, "/n");
TotalMem = atoi(line);
line = strstr(line, " ");
UsedMem = atoi(line);
memset(sStatBuf, 0, 256);
sprintf(sStatBuf, "Used %dM/Total %dM/n", UsedMem, TotalMem);
if(strlen(sStatBuf) > len)
{
return -1;
}
memcpy(Info, sStatBuf, strlen(sStatBuf));
return 0;
}


结果:Used 488M/Total 495M



### 三级标题:Linux `/proc/meminfo` 内存信息分析解读 Linux 系统中的 `/proc/meminfo` 文件是用于提供系统内存使用情况的主要接口。通过 `cat /proc/meminfo` 可以查看详细的内存统计信息,这些信息被诸如 `free` 和 `vmstat` 等工具所使用,其内容比这些命令更为丰富[^2]。该文件中的数据由内核维护,反映的是内核视角下的内存使用状态,而不是直接等同于物理内存的实际使用情况[^3]。 #### MemTotal 和 MemFree `MemTotal` 表示系统中总的可用物理内存大小,单位为 KB。这个值并不等于设备中实际安装的内存大小,因为在系统启动过程中,BIOS 或固件会保留一部分内存,同时内核本身也会占用一部分内存,剩余的部分才可供操作系统管理使用。一旦系统启动完成,`MemTotal` 的值通常是固定的,除非系统重启,否则不会发生变化[^3]。 ```plaintext MemTotal: 7570916 kB ``` `MemFree` 则表示当前系统中尚未被使用的物理内存总量,单位同样是 KB。这个值反映了系统当前的空闲内存状况。需要注意的是,`MemFree` 的值并不是越大越好,因为 Linux 系统倾向于尽可能多地使用内存来缓存文件和数据,以提高性能。当应用程序需要更多内存时,系统会自动释放这些缓存以满足需求。 ```plaintext MemFree: 1048576 kB ``` #### Buffers Cached `Buffers` 和 `Cached` 是两个经常被误解的字段。`Buffers` 通常表示用于文件系统元数据的缓存,而 `Cached` 则表示用于文件数据的缓存。这两个值加起来大致等于系统中用于文件缓存的总内存。然而,这个总和并不总是等于 `Active(file) + Inactive(file)`,因为 `Cached` 的统计方式 `Active(file)` 和 `Inactive(file)` 之间存在一定的差异[^4]。 ```plaintext Buffers: 262144 kB Cached: 2097152 kB ``` #### AnonPages AnonHugePages `AnonPages` 表示系统中匿名页的总量,匿名页指的是那些没有关联到任何文件的内存页,通常由应用程序的堆栈和匿名映射产生。`AnonHugePages` 则表示使用了大页(Huge Pages)的匿名页数量。大页机制可以减少页表项的数量,从而提升内存访问效率。`AnonHugePages` 的值通常小于或等于 `AnonPages`,具体取决于系统是否启用了大页支持[^4]。 ```plaintext AnonPages: 3145728 kB AnonHugePages: 1048576 kB ``` #### Slab `Slab` 字段表示内核中用于缓存对象的内存总量。SLAB 分配器是 Linux 内核中的一种高效内存分配机制,用于管理频繁分配和释放的小对象。`Slab` 的值包括了 `SReclaimable`(可回收的 SLAB 缓存)和 `SUnreclaim`(不可回收的 SLAB 缓存)两部分。这部分内存主要用于内核对象的缓存,如 inode、dentry 等。 ```plaintext Slab: 524288 kB SReclaimable: 262144 kB SUnreclaim: 262144 kB ``` #### SwapTotal SwapFree `SwapTotal` 表示系统中总的交换分区大小,单位为 KB。`SwapFree` 则表示当前未被使用的交换分区大小。交换分区是磁盘上的一块区域,用于在物理内存不足时将部分内存内容交换到磁盘上。虽然使用交换分区可以扩展系统的可用内存,但其性能远低于物理内存,因此应尽量避免过度依赖交换分区。 ```plaintext SwapTotal: 2097152 kB SwapFree: 2097152 kB ``` #### HugePages_Total、HugePages_Free HugePages_Surp `HugePages_Total` 表示系统中配置的大页总数,`HugePages_Free` 表示当前未被使用的巨大页数量。`HugePages_Surp` 表示超出预分配的大页数量。大页机制通过减少页表项的数量来提高内存访问效率,适用于需要大量连续内存的应用场景。 ```plaintext HugePages_Total: 512 HugePages_Free: 256 HugePages_Surp: 0 ``` ### 三级标题:相关字段的解释联系 `AnonHugePages` `AnonPages` 之间的关系在于,`AnonHugePages` 是 `AnonPages` 的一个子集,表示使用了大页机制的匿名页数量。`HugePages_Total` 则是系统中配置的大页总数,它决定了系统可以使用的最大大页数量。`HugePages_Free` 和 `HugePages_Surp` 分别表示当前未被使用的巨大页数量和超出预分配的大页数量。这些字段共同反映了系统中大页的使用情况及其对性能的影响。 ### 三级标题:总结 通过 `/proc/meminfo` 提供的内存信息,可以深入了解系统的内存使用状况。每个字段都有其特定的含义和用途,理解这些字段可以帮助系统管理员更好地监控和优化系统的内存使用。特别是对于 `MemTotal`、`MemFree`、`Buffers`、`Cached`、`AnonPages`、`AnonHugePages`、`Slab`、`SwapTotal` 和 `HugePages` 等字段的理解,有助于更准确地评估系统的内存使用情况和性能瓶颈。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值