/proc/meminfo详细

本文详细解读了/proc/meminfo文件中的各项指标,包括内存总量、空闲内存、缓冲区大小等内容,并介绍了如何利用这些信息判断系统内存使用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/proc/meminfo详细

(2010-10-13 04:10:22)

$cat /proc/meminfo
MemTotal:        2052440 kB //总内存
MemFree:           50004 kB //空闲内存
Buffers:           19976 kB //给文件的缓冲大小
Cached:           436412 kB //高速缓冲存储器(http://baike.baidu.com/view/496990.htm)使用的大小
SwapCached:        19864 kB //被高速缓冲存储用的交换空间大小
Active:          1144512 kB //活跃使用中的高速缓冲存储器页面文件大小
Inactive:         732788 kB //不经常使用的高速缓冲存储器页面文件大小
Active(anon):     987640 kB //anon:不久
Inactive(anon):   572512 kB
Active(file):     156872 kB
Inactive(file):   160276 kB
Unevictable:           8 kB
Mlocked:               8 kB
HighTotal:       1177160 kB //The total and free amount of memory, in kilobytes, that is not directly mapped into kernel space.
HighFree:           7396 kB // The HighTotal value can vary based on the type of kernel used.
LowTotal:         875280 kB // The total and free amount of memory, in kilobytes, that is directly mapped into kernel space.  used. 
LowFree:           42608 kB //The LowTotal value can vary based on the type of kernel
SwapTotal:        489940 kB //交换空间总大小
SwapFree:         450328 kB //空闲交换空间
Dirty:               104 kB //等待被写回到磁盘的大小
Writeback:             0 kB //正在被写回的大小
AnonPages:       1408256 kB //未映射的页的大小
Mapped:           131964 kB //设备和文件映射的大小
Slab:              37368 kB //内核数据结构缓存的大小,可减少申请和释放内存带来的消耗
SReclaimable:      14164 kB //可收回slab的大小
SUnreclaim:        23204 kB //不可收回的slab的大小23204+14164=37368
PageTables:        13308 kB //管理内存分页的索引表的大小
NFS_Unstable:          0 kB //不稳定页表的大小
Bounce:                0 kB //bounce:退回
WritebackTmp:          0 kB //
CommitLimit:     1516160 kB
Committed_AS:    2511900 kB
VmallocTotal:     122880 kB //虚拟内存大小
VmallocUsed:       28688 kB //已经被使用的虚拟内存大小
VmallocChunk:      92204 kB
HugePages_Total:       0 //大页面的分配
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       10232 kB
DirectMap2M:      899072 kB

/proc/meminfo详解

/proc/meminfo Explained



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

 

在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/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://blog.chinaunix.net/u2/78225/showart_1727609.html

<think>首先,用户的问题是关于Linux系统的:用户想在Linux系统中修改或查看/proc/meminfo中MemTotal的值。用户提供了四个引用:[^1]、[^2]、[^3]、[^4],这些是关于/proc/meminfo的解释,包括MemTotal的含义。关键点:-/proc/meminfo是一个虚拟文件系统,由Linux内核提供,用于显示系统内存信息。-根据引用[^2],MemTotal表示内核管理的总物理内存大小,但它可能不等于实际的物理内存大小,因为firmware/BIOS和内核本身会占用一些内存。这个值在系统运行期间是固定的。-引用[^4]提到MemTotal不是直接可修改的,因为它是一个内核状态。用户的需求:-**查看MemTotal的值**:这是直接和简单的。用户可以通过命令查看/proc/meminfo的内容。-**修改MemTotal的值**:从引用中看,MemTotal是基于实际内存的统计值,不能随意修改,因为它是内核计算的。修改可能涉及内核启动参数或系统配置。系统级指令:1.所有行内数学表达式使用$...$格式。2.独立公式使用$$...$$格式并单独成段。3.LaTeX语法正确。4.使用中文回答。5.生成相关问题。6.回答中引用的段落末尾添加引用标识如[^1]。用户可见层指令:-回答结构清晰,帮助用户逐步解决问题。-保证回答真实可靠。我的回答结构:1.**解答问题**:分步骤解释如何查看和试图修改MemTotal。-首先,解释查看方法:使用`cat/proc/meminfo`命令。-其次,解释修改部分:说明MemTotal通常是只读的,为什么不能修改,以及可能的间接方式(如调整内核启动参数)。2.**引用相关部分**:在适当位置添加引用标识。3.**生成相关问题**:在回答末尾添加相关问题。回答语言:中文。可能的步骤:-**查看MemTotal**:-在终端运行:`cat/proc/meminfo|grepMemTotal`-这会显示MemTotal的值。-**修改MemTotal**:-MemTotal不是直接可修改的文件;/proc文件系统是只读的,表示内核状态。-修改实际内存配置可能涉及BIOS/UEFI设置或内核启动参数(如mem=size启动选项),但这不是修改MemTotal本身,而是影响初始内存分配。-警告用户修改可能不稳定或破坏系统。从引用[^2]:"MemTotal表示内存管理系统管理的总物理内存大小...这个值在系统运行期间一般是固定不变的。如果设备发生重启,这个值可能会发生变化。"所以重启系统或改变内存配置才可能影响它。核心观点:不能直接修改/proc/meminfo的内容,因为它不是真实文件;它是内核虚拟的。试图修改它会出错。添加生成的相关问题。确保回答清晰:1.介绍:简述/proc/meminfo的作用。2.查看MemTotal:详细步骤。3.修改MemTotal:解释为什么难、可能的替代。4.总结建议。5.相关问题。使用引用:-[^1]是/proc/meminfo详解。-[^2]解释MemTotal的来源。-[^3]关于buffers/cache。-[^4]解释MemAvailable。在回答中:-当解释MemTotal不可修改时,引用[^2]:"...这个值在系统运行期间一般是固定不变..."-当提到内存使用情况时,引用[^4]。最终回答结构:-标题(如果需要)-步骤1:如何查看-步骤2:是否能修改和为什么-步骤3:如果需要改变内存分配的选项-结尾:建议-§§相关问题§§</think> ### 如何在Linux系统中查看或修改/proc/meminfo中的MemTotal值 在Linux系统中,`/proc/meminfo` 是一个虚拟文件系统(procfs),由内核动态生成,用于实时展示系统内存使用情况。`MemTotal` 表示内核管理的总物理内存大小,但它可能不等于实际安装的物理内存总量(如BIOS保留的内存会减少可用值)。`/proc/meminfo` 文件是内核统计数据的只读视图,其内容不能直接修改[^2]。下面我为您逐步解释如何安全查看和处理MemTotal的值。 #### 1. **查看 MemTotal 的值** 查看过程非常简单,您只需要在Linux终端中使用命令行工具: - **步骤**: 1. 打开终端。 2. 运行命令查看整个 `/proc/meminfo` 文件,并过滤出 MemTotal: ```bash cat /proc/meminfo | grep MemTotal ``` 或者只输出MemTotal的值: ```bash grep MemTotal /proc/meminfo ``` 3. 输出示例(单位kB): ``` MemTotal: 7570916 kB ``` 这里,`7570916 kB` 是内核管理的总物理内存量,可能比实际物理内存小(如8GB物理内存可能显示约7.5GB),因为内核启动时会保留部分内存给固件或自身使用[^2]。 - **解释**: - `/proc/meminfo` 是虚拟文件,每次读取时都从内核获取最新状态。 - MemTotal是固定值,除非系统重启或硬件配置变更,否则不会变化。这是系统级别的内存统计,不能由用户应用程序直接修改[^2][^4]。 #### 2. **修改 MemTotal 的值?不可能的原因和替代方案** **注意:MemTotal的值不能直接修改**。原因包括: - `/proc` 文件系统是内核提供的只读接口,任何写入尝试都会失败(例如 `echo 'value' > /proc/meminfo` 会报错"Permission denied")。 - MemTotal基于物理内存分配(在启动时由BIOS/UEFI和内核决定)。改变它需要调整系统底层配置: - **唯一可行的间接方式**:修改内核启动参数或硬件设置,但这不是直接编辑 `/proc/meminfo`。 - **方法(重启后生效)**: 1. 编辑GRUB配置文件(例如 `/etc/default/grub`),添加内存限制参数 `mem=size`(例如 `mem=8G`)。 2. 运行 `sudo update-grub` 更新配置。 3. 重启系统:`sudo reboot`。 4. 重启后,再次查看 `/proc/meminfo`,MemTotal可能增加或减少(取决于参数设置)。 - **警告**:此操作风险高: - `mem=size` 参数强制内核使用特定内存大小,可能导致系统不稳定或无法启动(如果设置过大或过小)。 - 物理内存实际可用值不会改变;这只是限制内核管理的内存范围,MemFree和MemAvailable可能受影响[^2][^4]。 - **示例命令(修改GRUB)**: ```bash sudo nano /etc/default/grub # 在 GRUB_CMDLINE_LINUX 行添加 mem=8192M (表示8GB) sudo update-grub sudo reboot ``` - 正常使用中,**不建议人为修改MemTotal**。如果需要更多应用可用内存,应关注 MemFree 和 MemAvailable(后者估算应用可用内存:近似 MemFree + Buffers + Cached[^4])。优化内存可通过清除缓存: ```bash sync; echo 3 > /proc/sys/vm/drop_caches # 释放缓存但不重置MemTotal ``` #### 建议总结 - **查看**:用 `grep MemTotal /proc/meminfo` 快速获取值。 - **修改**:MemTotal 不能直接编辑,因为它是内核只读状态。强制修改需通过系统重启和内核参数调整(有风险)。 - **可靠性说明**:真实环境保持MemTotal不变是最稳定的。如果您误操作后系统异常,请回复具体错误,我会帮助诊断。 基于Linux内存管理机制,`/proc/meminfo` 提供了关键指标,但 MemTotal 的不可修改特性确保了系统稳定性[^2][^4]。如果您是开发或调试用途,请优先检查其他可调参数如 `/proc/sys/vm/`。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值