首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

为什么malloc函数申请2GB空间失败,但是申请3-4GB空间却能成功?该怎么解决

2012-04-05 
为什么malloc函数申请2GB空间失败,但是申请3-4GB空间却能成功?为什么malloc函数申请2GB空间失败,但是申请3

为什么malloc函数申请2GB空间失败,但是申请3-4GB空间却能成功?
为什么malloc函数申请2GB空间失败,但是申请3-4GB空间却能成功?
申请大的成功,小的失败,什么道理啊?

[解决办法]
man malloc

BUGS
 By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. This is a really bad bug. In case it turns out that the system is out of memory, one or more processes will be killed by the infamous OOM killer. In case Linux is employed under circumstances where it would be less desirable to suddenly lose some randomly picked processes, and moreover the kernel version is sufficiently recent, one can switch off this overcommitting behavior using a command like:
 
# echo 2 > /proc/sys/vm/overcommit_memory
 
See also the kernel Documentation directory, files vm/overcommit-accounting and sysctl/vm.txt.

热点排行