Adds a workaround to avoid the OOM killer in Linux.

This commit is contained in:
Andrea Mazzoleni 2012-10-03 19:49:38 +02:00
parent 026954e41e
commit 89339e24d3

11
util.c
View file

@ -669,10 +669,19 @@ void* malloc_nofail(size_t size)
void* ptr = malloc(size);
if (!ptr) {
fprintf(stderr, "Low memory\n");
/* don't use printf to avoid any possible extra allocation */
write(2, "Low Memory\n", 11);
exit(EXIT_FAILURE);
}
/* Here we preinitialize the memory to ensure that the OS is really allocating it */
/* and not only reserving the addressable space. */
/* Otherwise we are risking that the OOM (Out Of Memory) killer in Linux will kill the process. */
/* Filling the memory doesn't ensure to disable OOM, but it increase a lot the chances to */
/* get a real error from malloc() instead than a process killed. */
/* Note that calloc() doesn't have the same effect. */
memset(ptr, 0xA5, size);
mcounter += size;
return ptr;