问题描述
当它尝试派生一个子进程时,父进程以errno = 12(内存不足)失败.父进程在Linux 3.0内核-SLES 11上运行.在分叉子进程时,父进程已经用完了大约70%的RAM(180GB/256GB).有没有解决此问题的方法?
The parent process fails with errno=12(Out of memory) when it tries to fork a child. The parent process runs on Linux 3.0 kernel - SLES 11. At the point of forking the child, the parent process has already used up around 70% of the RAM(180GB/256GB). Is there any workaround for this problem?
该应用程序是用C ++编写的,并使用g ++ 4.6.3进行了编译.
The application is written in C++, compiled with g++ 4.6.3.
推荐答案
也许您的系统中阻止了虚拟内存过度提交.
Maybe virtual memory over commit is prevented in your system.
如果防止,则虚拟内存不能大于物理RAM +交换的大小.如果允许,则虚拟内存可以大于RAM + swap.
If it is prevented, then the virtual memory can not be bigger than sizeof physical RAM + swap. If it is allowed, then virtual memory can be bigger than RAM+swap.
当您的进程派生时,您的进程(父级和子级)将具有2 * 180GB的虚拟内存(如果没有交换,这将太多了).
When your process forks, your processes (parent and child) would have 2*180GB of virtual memory (that is too much if you don't have swap).
因此,通过这种方式允许过量提交:
So, allow over commit by this way:
echo 1 > /proc/sys/vm/overcommit_memory
如果子进程立即执行,或者在父进程写太多自己的内存之前释放已分配的内存,它应该会有所帮助.因此,请注意,如果两个进程都继续使用所有内存,则内存不足杀手可能会采取行动.
It should help, if child process execves immediately, or frees allocated memory before the parent writes too much to own memory. So, be careful, out of memory killer may act if both processes keep using all the memory.
proc(5)的手册页说:
man page of proc(5) says:
此文件包含虚拟内核 内存计费模式.值是:0:启发式过量使用(这是 默认值)1:始终过量使用,从不检查2:始终检查,从不 过量使用
This file contains the kernel virtual memory accounting mode. Values are: 0: heuristic overcommit (this is the default) 1: always overcommit, never check 2: always check, never overcommit
在模式0下,不检查带有MAP_NORESERVE的mmap(2)的调用,并且 默认检查非常弱,导致存在获得 处理"OOM杀死".在Linux 2.4下,任何非零值表示模式 1.在模式2(自Linux 2.6起可用)中,系统上的总虚拟地址空间被限制为(SS + RAM *(r/100)),其中SS是 交换空间的大小,RAM是物理内存的大小, r是文件/proc/sys/vm/overcommit_ratio的内容.
In mode 0, calls of mmap(2) with MAP_NORESERVE are not checked, and the default check is very weak, leading to the risk of getting a process "OOM-killed". Under Linux 2.4 any nonzero value implies mode 1. In mode 2 (available since Linux 2.6), the total virtual address space on the system is limited to (SS + RAM*(r/100)), where SS is the size of the swap space, and RAM is the size of the physical memory, and r is the contents of the file /proc/sys/vm/overcommit_ratio.
此处有更多信息:在SLES中过量使用内存
这篇关于fork()失败,出现内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!