linux申请内存失败,Linux 内存分配失败(关于overcommit_memory)

1、问题现象和分析:

测试时发现当系统中空闲内存还有很多时,就报内存分配失败了,所有进程都报内存分配失败:

sshd@localhost:/var/log>free

total       used       free     shared    buffers     cached

Mem:      12183700    8627972    3555728          0     289252     584444

-/+ buffers/cache:    7754276    4429424

Swap:            0          0          0

sshd@localhost:/var/log>free

-bash: fork: Cannot allocate memory

sshd@localhost:/var/log>cat /proc/meminfo

-bash: fork: Cannot allocate memory

而messages日志中,也没有OOM相关的记录。最后确认原因为:/proc/sys/vm/overcommit_memory参数导致。

该环境中该参数设置为2,表示“No overcommit”,即系统中所有进程占用的虚拟内存空间不能超过上限:

cat /proc/meminfo

CommitLimit:    12061860 kB  //虚拟地址空间的上限

Committed_AS:    8625360 kB  //当前的使用量

而该参数应该默认是0,这种情况下,只有还有空闲的物理内存,就可以继续分配,不受虚拟地址空间的限制。

echo 0 > /proc/sys/vm/overcommit_memory

如此修正后解决。

2、关于overcommit_memory说明:

取值为0,系统在为应用进程分配虚拟地址空间时,会判断当前申请的虚拟地址空间大小是否超过剩余内存大小,如果超过,则虚拟地址空间分配失败。因此,也就是如果进程本身占用的虚拟地址空间比较大或者剩余内存比较小时,fork、malloc等调用可能会失败。

取值为1,系统在为应用进程分配虚拟地址空间时,完全不进行限制,这种情况下,避免了fork可能产生的失败,但由于malloc是先分配虚拟地址空间,而后通过异常陷入内核分配真正的物理内存,在内存不足的情况下,这相当于完全屏蔽了应用进程对系统内存状态的感知,即malloc总是能成功,一旦内存不足,会引起系统OOM杀进程,应用程序对于这种后果是无法预测的

取值为2,则是根据系统内存状态确定了虚拟地址空间的上限,由于很多情况下,进程的虚拟地址空间占用远大小其实际占用的物理内存,这样一旦内存使用量上去以后,对于一些动态产生的进程(需要复制父进程地址空间)则很容易创建失败,如果业务过程没有过多的这种动态申请内存或者创建子进程,则影响不大,否则会产生比较大的影响

3、相应代码分析:

点击(此处)折叠或打开

int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)

{

unsigned long free, allowed;

vm_acct_memory(pages);

/*

* Sometimes we want to use more memory than we have

*/

if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) //overcommit_memory=1,直接返回成功,不做任何限制。

return 0;

if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { //overcommit_memory=0,启发式方式,根据当前系统中空闲内存状况来决定是否可以分配内存。

unsigned long n;

free = global_page_state(NR_FILE_PAGES);

free += nr_swap_pages;

/*

* Any slabs which are created with the

* SLAB_RECLAIM_ACCOUNT flag claim to have contents

* which are reclaimable, under pressure. The dentry

* cache and most inode caches should fall into this

*/

free += global_page_state(NR_SLAB_RECLAIMABLE);

/*

* Leave the last 3% for root

*/

if (!cap_sys_admin)

free -= free / 32; //root用户可以在free更少(3%)的时候,分配内存。

if (free > pages) // pages为需要分配的内存大小,free为根据一定规则算出来的“空闲内存大小”,第一次free仅为NR_FILE_PAGES+NR_SLAB_RECLAIMABLE,由于直接或者系统中“实际空闲”内存代价比较大,所以进行分阶判断,提高效率。

return 0;

/*

* nr_free_pages() is very expensive on large systems,

* only call if we're about to fail.

*/

n = nr_free_pages(); //当第一次判断不满足内存分配条件时,再进行“实际空闲”内存的获取操作。

/*

* Leave reserved pages. The pages are not for anonymous pages.

*/

if (n <= totalreserve_pages)

goto error;

else

n -= totalreserve_pages;

/*

* Leave the last 3% for root

*/

if (!cap_sys_admin)

n -= n / 32;

free += n;

if (free > pages)

return 0;

goto error;

}

allowed = (totalram_pages - hugetlb_total_pages()) //当overcommit_memory=2时,根据系统中虚拟地址空间的总量来进行限制。

* sysctl_overcommit_ratio / 100;

/*

* Leave the last 3% for root

*/

if (!cap_sys_admin)

allowed -= allowed / 32;

allowed += total_swap_pages;

/* Don't let a single process grow too big:

leave 3% of the size of this process for other processes */

if (mm)

allowed -= mm->total_vm / 32;

if (percpu_counter_read_positive(&vm_committed_as) < allowed)

return 0;

error:

vm_unacct_memory(pages);

return -ENOMEM;

}

转载自:

http://blog.chinaunix.net/uid-20671208-id-4440244.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值