This is true for small allocations, but once you start trying to do allocations in the several GB range you can easily hit allocation failures. Fortunately, these are also the ones that tend to be predictable and relatively easy to handle.
You won't be OOM killed until you actually touch those pages (I assume). That's independent of calling malloc, which only cares about virtual memory space in your process.
Some mallocs also have a maximum sensible size and will fail anything above that because it's probably a bug.
Okay, this was one of those "why don't you just write the code before posting" situations. You were right.
#include <stdio.h>
#include <stdlib.h>
int main() {
char *str;
size_t chunk = 1<<31;
for (int i=0; i<64; i++) {
str = (char *) malloc(chunk);
if (str == NULL) {
printf("Failed to allocate at %d\n", i);
return 1;
} else {
printf("Allocated the %d chunk\n", i);
}
}
return 0;
}
That does test true for str == NULL on my machine. I didn't get to put chars in the str because that does == NULL there since the chunk size is too large.
You wouldn't run into allocation failures even if you allocate many gigs at once. With a 64 kb address space, you will succeed in allocating the first 18 billion gigabytes. That's not a particularly common scenario.
16
u/DarkLordAzrael Apr 15 '21
This is true for small allocations, but once you start trying to do allocations in the several GB range you can easily hit allocation failures. Fortunately, these are also the ones that tend to be predictable and relatively easy to handle.