From 89339e24d3ccea4f5196e65015f9e45f2dea3f7d Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Wed, 3 Oct 2012 19:49:38 +0200 Subject: [PATCH] Adds a workaround to avoid the OOM killer in Linux. --- util.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/util.c b/util.c index bcb85fdd..aabb3cbf 100644 --- a/util.c +++ b/util.c @@ -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;