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

    相关文章:

    • 2021-12-25
    • 2022-12-23
    • 2021-07-08
    • 2022-12-23
    • 2022-03-10
    • 2022-12-23
    • 2021-12-15
    • 2021-09-20
    猜你喜欢
    • 2022-12-23
    • 2021-08-28
    • 2021-06-24
    • 2021-12-24
    • 2022-03-07
    • 2021-12-20
    • 2021-09-01
    相关资源
    相似解决方案