linux 用户/内核空间分配原理,修改方法

32位Linux系统为例,4GB的地址空间通常被分为:

  • 3GB 用户空间
  • 1GB 内核空间

这种分配方式被称为3/1分割(3G/1G split)。

修改分配大小

Linux内核提供了一个启动参数 mem 来调整这个分割。例如:

  • 使用 mem=3G 会将用户空间限制在3GB
  • 使用 mem=2G 会将用户空间限制在2GB,给内核空间更多内存

修改这个参数,你需要编辑bootloader的配置文件(如GRUB的配置),并在内核启动参数中添加相应的 mem 选项。

3/1分割的原因

The 3GB/1GB split in 32-bit Linux systems is based on several considerations:

1. User Space Needs: Most applications run in user space and benefit from having a larger address space available.

2. Kernel Space Efficiency: 1GB is usually sufficient for kernel operations in most scenarios.

3. Balance: This split provides a balance between user and kernel needs for a wide range of use cases.

4. Historical Compatibility: Many applications were designed with the assumption of having close to 3GB of addressable space.

5. Performance: This split allows for efficient kernel entry/exit without requiring a complete TLB (Translation Lookaside Buffer) flush.

6. Direct Memory Access (DMA): The 1GB kernel space allows for easier management of DMA operations, especially for devices that can only address 32-bit memory spaces.

However, this split isn't always optimal:

- For systems with large amounts of RAM and running memory-intensive kernel operations (e.g., database servers), a larger kernel space might be beneficial.
- For systems primarily running large user-space applications, a larger user space could be advantageous.

This is why Linux allows for adjusting this split through boot parameters, enabling system administrators to optimize memory allocation based on specific use cases.

malloc最大可以申请的堆内存大小

shell中输入 ulimit -v可以查询malloc可以申请的最大内存大小。
大多数系统会将单个进程的最大内存限制设置得比3GB小,通常在2GB左右,以保留一些地址空间用于其他目的。原因如下:

  • 3GB是整个用户空间的大小,但并非所有这些空间都可用于单个进程。
  • 一些地址空间被用于加载共享库、栈、环境变量等。
  • 实际可用的内存还受到系统总内存和其他运行进程的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值