微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/linux/394309.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐