对/proc/buddyinfo的理解(转载)

本文介绍Linux系统的内存管理机制,特别是通过/proc/buddyinfo文件来诊断内存碎片问题的方法。通过理解buddy算法的工作原理,我们可以了解到如何评估内存碎片的程度,并采取相应措施减少内存碎片带来的负面影响。

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

最近总是遇到out of memory,查看了一些文档,提到了这个内核参数,特此解释一下。

3.2.2. /proc/buddyinfo
This file is used primarily for diagnosing memory fragmentation issues. Using the buddy algorithm, each column represents the number of pages of a certain order (a certain size) that are available at any given time. For example, for zone DMA (direct memory access), there are 90 of 2^(0*PAGE_SIZE) chunks of memory. Similarly, there are 6 of 2^(1*PAGE_SIZE) chunks, and 2 of 2^(2*PAGE_SIZE) chunks of memory available.

The DMA row references the first 16 MB on a system, the HighMem row references all memory greater than 4 GB on a system, and the Normal row references all memory in between.

The following is an example of the output typical of /proc/buddyinfo:

 

内存碎片问题

/proc/buddyinfo
Submitted by admin on Sat, 2006-07-22 19:07
This file exports some information about the buddy allocator.

Each column of numbers represents the number of pages of that order which are available. In the example below, there are 7 chunks of 2 ^ 0 * PAGE_SIZE available in ZONE_DMA, and 12 chunks of 2 ^ 3 * PAGE_SIZE available in ZONE_NORMAL, etc...

This information can give you a good idea about how fragmented memory is and give you a clue as to how big of an area you can safely allocate.

When a Linux system has been running for a while memory fragmentation can increase which depends heavily on the nature of the applications that are running on it. The more processes allocate and free memory, the quicker memory becomes fragmented. And the kernel may not always be able to defragment enough memory for a requested size on time. If that happens, applications may not be able to allocate larger contiguous chunks of memory even though there is enough free memory available. Starting with the 2.6 kernel, i.e. RHEL4 and SLES9, memory management has improved tremendously and memory fragmentation has become less of an issue.

To see memory fragmentation you can use the magic SysRq key. Simply execute the following command:
# echo m > /proc/sysrq-triggerThis command will dump current memory information to /var/log/messages. Here is an example of a RHEL3 32-bit system:

Jul 23 20:19:30 localhost kernel: 0*4kB 0*8kB 0*16kB 1*32kB 0*64kB 1*128kB 1*256kB 1*512kB 1*1024kB 0*2048kB 0*4096kB = 1952kB)
Jul 23 20:19:30 localhost kernel: 1395*4kB 355*8kB 209*16kB 15*32kB 0*64kB 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 12244kB)
Jul 23 20:19:31 localhost kernel: 1479*4kB 673*8kB 205*16kB 73*32kB 21*64kB 847*128kB 473*256kB 92*512kB 164*1024kB 64*2048kB 28*4096kB = 708564kB)The first line shows DMA memory fragmentation. The second line shows Low Memory fragmentation and the third line shows High Memory fragmentation. The output shows memory fragmentation in the Low Memory area. But there are many large memory chunks available in the High Memory area, e.g. 28 4MB.

If memory information was not dumped to /var/log/messages, then SysRq was not enabled. You can enable SysRq by setting sysrq to 1:
# echo 1 > /proc/sys/kernel/sysrqStarting with the 2.6 kernel, i.e. RHEL4 and SLES9, you don’t need SysRq to dump memory information. You can simply check /proc/buddyinfo for memory fragmentation.

Here is the output of a 64-bit server running the 2.6 kernel:
# cat /proc/buddyinfo
Node 0, zone DMA 5 4 3 4 3 2 1 0 1 1 2
Node 0, zone Normal 1046 527 128 36 17 5 26 40 13 16 94
# echo m > /proc/sysrq-trigger
# grep Normal /var/log/messages | tail -1
Jul 23 21:42:26 localhost kernel: Normal: 1046*4kB 529*8kB 129*16kB 36*32kB 17*64kB 5*128kB 26*256kB 40*512kB 13*1024kB 16*2048kB 94*4096kB = 471600kB
#In this example I used SysRq again to show what each number in /proc/buddyinfo is referring

根据以上文章理解内存碎片是怎么回事了。
471600kb就是可用的内存(可以连续分配的内存地址空间),以4kb,8,16,32,64,128,256,512,1024,2048,4096kb为单位的所有可分配内存之和。

<think>我们被要求详细介绍“zone”和“/proc/buddyinfo”。首先,我们需要解释Linux内存管理中的zone概念,然后详细介绍/proc/buddyinfo文件的作用、内容和如何解读。 注意:回答后必须提出3-5个相关问题,以“§§相关问题§§”开头,然后列出问题。 1. 关于zone: 在Linux内核中,物理内存被划分为多个区域(zones),主要是为了处理硬件限制。例如,某些硬件设备只能访问低端内存(DMA区域),而内核需要分配不同区域的内存以满足不同需求。 常见的zone包括: - DMA: 通常是最开始的16MB内存,用于直接内存访问(DMA)设备。 - DMA32: 在64位系统中,用于32位DMA设备,通常是可以访问4GB以下的内存区域。 - Normal: 在32位系统中,这是16MB到896MB之间的内存;在64位系统中,这个区域通常包含所有内存(因为64位系统没有高端内存问题)。 - HighMem: 仅在32位系统中存在,用于访问超过896MB的内存(通过动态映射)。 2. 关于/proc/buddyinfo: 这个文件显示了内核的伙伴系统(buddy system)的当前状态。伙伴系统是内核中用于管理物理内存页(通常每页4KB)的分配和回收的算法,目的是减少内存碎片。 文件内容按每个节点(NUMA)和每个zone列出,显示每个order(从0到最大,通常是10或11)的空闲连续页块的数量。order表示连续的页块大小,例如order=0表示1页(4KB),order=1表示2页(8KB),order=n表示2^n页。 示例格式: Node 0, zone DMA 1 0 1 0 2 1 1 0 1 1 3 Node 0, zone Normal 101 52 33 23 5 2 2 1 1 0 0 Node 1, zone Normal ... 每一行中的数字表示对应order(从0开始)的空闲块数。例如,第一行表示DMA zone中,order0有1个空闲块(4KB),order1有0个,order2有1个(16KB),等等。 通过观察/proc/buddyinfo,我们可以判断内存碎片情况:如果小order的块很多而大order的块很少,说明内存碎片化严重。 回答结构: - 先解释zone的概念和常见类型。 - 再解释/proc/buddyinfo的作用和如何解读。 最后,根据以上内容提出3-5个相关问题。</think>### 回答 在Linux内存管理中,**zone(内存区域)** 和 **/proc/buddyinfo** 是诊断内存分配和碎片问题的核心概念: #### 一、内存区域(Zone) Linux内核将物理内存划分为多个**zone**,主要目的是处理硬件架构限制(如DMA设备只能访问低地址内存)。常见zone类型包括: 1. **DMA Zone** - 范围:物理内存最开始的16MB - 用途:供旧式DMA设备直接访问,避免地址映射问题 2. **DMA32 Zone**(仅64位系统) - 范围:4GB以下内存 - 用途:支持32位DMA设备 3. **Normal Zone** - 范围:16MB~896MB(32位系统)或大部分内存(64位系统) - 用途:内核和进程的通用内存分配 4. **HighMem Zone**(仅32位系统) - 范围:896MB以上 - 用途:管理超过内核线性地址空间的内存 **关键特性**: - 每个NUMA节点包含独立的zone集合 - 水线(watermarks)按zone单独设置(min/low/high) - 内存分配优先从本地zone尝试,不足时触发回收或跨zone分配 #### 二、/proc/buddyinfo 文件解析 此文件实时显示**伙伴系统(Buddy System)** 的空闲内存块状态,用于检测内存碎片: ```bash $ cat /proc/buddyinfo Node 0, zone DMA 1 0 1 0 2 1 1 0 1 1 3 Node 0, zone DMA32 102 48 30 15 3 2 1 0 0 0 0 Node 0, zone Normal 201 85 40 20 5 1 0 0 0 0 0 ``` ##### 结构解读: 1. **列含义**(从左到右): - `Node 0, zone DMA`:NUMA节点0的DMA区域 - 后续数字:每个order(0~10)的空闲连续页块数量 2. **Order与内存块大小**: | Order | 页数 | 内存大小 (4KB页) | |-------|------|------------------| | 0 | 1 | 4 KB | | 1 | 2 | 8 KB | | 2 | 4 | 16 KB | | ... | ... | ... | | 10 | 1024 | 4 MB | 3. **碎片诊断**: - **健康状态**:大order(如8~10)有较多空闲块(如`201,85,40`) - **碎片严重**:小order块多(左侧数字大),大order块少(右侧接近0) *示例:若Normal zone的order10值为0,表明无法分配4MB连续内存* ##### 应用场景: - 定位分配失败:当`mmap`请求大内存失败时,检查对应zone的大order值 - 监控碎片趋势:定期采样观察大order块是否持续减少 - 验证回收效果:kswapd回收后观察大order块是否增加
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值