overcommit_memory

overcommit_memory是Linux内核中的一个参数,控制malloc、mmap等内存分配行为。当设置为0时,内核尝试估算剩余内存;设为1,则总是允许过度承诺;设为2,则采取不过度承诺策略,防止内存过量分配。默认值为0。overcommit_kbytes和overcommit_ratio用于限制在overcommit_memory设为2时,允许的内存承诺量。这两个选项互斥,用于设置进程虚拟地址空间的最大承诺值不超过交换分区加上一定的物理内存百分比或固定数量。
摘要由CSDN通过智能技术生成

目录

  • overcommit_memory
  • Overcommit Accounting
  • CommitLimit and Committed_AS
  • overcommit_kbytes or overcommit_ratio


overcommit_memory: Documentation for /proc/sys/vm/ — The Linux Kernel documentation

overcommit_memory这个vm参数用于控制malloc、mmap这些用于在进程虚拟地址空间申请虚拟内存的系统调用,例如一次性申请大量的虚拟地址是否允许成功,就取决于这个参数的配置。

参数位置:/proc/sys/vm/overcommit_memory(运行时配置内核参数,那你绝对要关注sysctl指令和/proc/sys目录)

overcommit_memory

This value contains a flag that enables memory overcommitment.

When this flag is 0, the kernel attempts to estimate the amount of free memory left when userspace requests more memory.

When this flag is 1, the kernel pretends there is always enough memory until it actually runs out.

When this flag is 2, the kernel uses a “never overcommit” policy that attempts to prevent any overcommit of memory. Note that user_reserve_kbytes affects this policy.

This feature can be very useful because there are a lot of programs that malloc() huge amounts of memory “just-in-case” and don’t use much of it.

The default value is 0.

See Overcommit Accounting and mm/util.c::__vm_enough_memory() for more information.

更多详情,需要去转看Overcommit Accounting。

补充:默认情况下,当malloc一次申请的虚拟内存大于128K则会转成mmap,此时不会在堆内存中通过brk指针申请虚拟内存,而是通过mmap在内存映射区划分内存。

1.先看malloc API文档中的MMAP_THRESHOLD:

Normally, malloc() allocates memory from the heap, and adjusts
       the size of the heap as required, using sbrk(2).  When allocating
       blocks of memory larger than MMAP_THRESHOLD bytes, the glibc
       malloc() implementation allocates the memory as a private
       anonymous mapping using mmap(2).  MMAP_THRESHOLD is 128 kB by
       default, but is adjustable using 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值