linux – fork()失败,出现内存不足错误

父进程在尝试分叉子进程时失败,并且errno = 12(内存不足).父进程在 Linux 3.0内核上运行 – SLES 11.在分叉子进程时,父进程已经占用了大约70%的RAM(180GB / 256GB).这个问题有解决方法吗?

该应用程序用C语言编写,用g 4.6.3编译.

解决方法

可能在您的系统中阻止了提交虚拟内存.

如果被阻止,则虚拟内存不能大于物理RAM交换的大小.如果允许,则虚拟内存可以大于RAM交换.

当您的进程分叉时,您的进程(父进程和子进程)将具有2 * 180GB的虚拟内存(如果您没有交换则太多).

所以,通过这种方式允许过度提交:

echo 1 > /proc/sys/vm/overcommit_memory

如果子进程立即执行,它应该有所帮助,或者在父进程写入太多内存之前释放已分配的内存.所以,要小心,如果两个进程都继续使用所有内存,则内存杀手可能会起作用.

proc(5)的手册页说:

/proc/sys/vm/overcommit_memory

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

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.

更多信息:Overcommit Memory in SLES

相关文章

Linux中的ARP防火墙主要用于防御ARP欺骗攻击,其效果取决于多...
insmod和modprobe加-f参数导致Invalid module format错误 这...
将ArchLinux安装到U盘 几个月前入门Arch的时候上网搜了不少安...
1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...