You can [effectively] disable overcommiting from the kernel> = 2.5.30.
Following Linux Kernel Memory :
// Save your work here and pay attention to the current value of overcommit_ratio
# echo 2 > overcommit_memory
# echo 1 > overcommit_ratio
VM_OVERCOMMIT_MEMORY 2, , overcommit_ratio, 1 (.. )
- Null malloc
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
void *page = 0; int index;
void *pages[256];
index = 0;
while(1)
{
page = malloc(1073741824);
if(!page)break;
pages[index] = page;
++index;
if(index >= 256)break;
}
if(index >= 256)
{
printf("allocated 256 pages\n");
}
else
{
printf("memory failed at %d\n",index);
}
while(index > 0)
{
--index;
free(pages[index]);
}
return 0;
}
$ cat /proc/sys/vm/overcommit_memory
0
$ cat /proc/sys/vm/overcommit_ratio
50
$ ./code/stackoverflow/test-memory
allocated 256 pages
$ su
# echo 2 > /proc/sys/vm/overcommit_memory
# echo 1 > /proc/sys/vm/overcommit_ratio
# exit
exit
$ cat /proc/sys/vm/overcommit_memory
2
$ cat /proc/sys/vm/overcommit_ratio
1
$ ./code/stackoverflow/test-memory
memory failed at 0
overcommit_memory 0 overcommit_ratio