After suitable screwing around to keep gcc from optimizing away the writes, it goes a bit like this…
jim@gvdev:~$ ./fail
Before
Allocated 1 GB
Allocated 2 GB
...
Allocated 64 GB
After
Writing in 0 GB
Writing in 1 GB
...
Writing in 15 GB
... rather long delay here...
Write failed: Broken pipe
upstairs:~ jim$ <----- bad news here, notice the machine.
upstairs:~ jim$ slogin gvdev.XXXXX.net
ssh: connect to host gvdev.XXXXX.net port 22: Network is unreachable
… so now I have to get back in "going out" clothes, go downstairs, and drive over to gvdev and reboot it. Nov 2 21:29:33 gvdev kernel: [15115318.546376] fail[17150]:
segfault at 7fffe9cbc1e0 ip 00000000004006fe sp 00007fffe9cbb1d0
error 4 in fail[400000+1000]The problem is that there's no good way to differentiate between a process that merely wants more VM that it'll never touch and a process that wants actual RAM. In theory, every fork() should double your VM requirements - in practice, almost all are immediately followed by an exec() and the previous allocations are irrelevant. Do you fail fork() because you can't guarantee you can back every modified page in the new process? If not, why should you fail malloc() because you can't guarantee you can back it? There's no correct answer, and Linux lets you control which answer you want via /proc/sys/vm/overcommit_memory .
It's terrible about this with PostgreSQL. The OOM Killer tends to thump the postmaster, not the offending backend.
Memory is a global resource, even if you didn't overcommit the process getting the allocation failure is not guaranteed to be the misbehaving one, it's just the unlucky process that did the first allocation after all the memory was gone (this is also why handling allocation errors in your application is usually impossible, even without an overcommitting kernel).
The most 'fair' way to go about it is to just kill a random process. That skits the whole issue.
EDIT: this means if the kernel can't allocate more memory when you write then the kernel has a decision to make; kill you, or kill someone else to get more memory.