Hacker News new | ask | show | jobs
by rntz 3477 days ago
The specification allows for this, yes. However, on some platforms (including linux glibc by default, I believe), malloc() never fails, but allocates virtual memory optimistically; the first you hear of an out of memory condition is when the system slows down due to paging, and the next thing you notice is when the OOM killer nixes a process.

Of course, other platforms, especially embedded ones, behave differently.

1 comments

Actually there is one reason for malloc to return NULL even with virtual memory, your process can run out of address space.
Or run out of room in the paging file. Your addressable memory cannot be larger than physical memory without a backing store.
Depending on VM_OVERCOMMIT_MEMORY, Linux might give out address space well beyond the size of the pagefile, hoping many of those pages are never written to (e.g., most threads never get anywhere near the bottom of their default stacks).