Live data from Hacker News

Malloc Never Fails (2012)

scvalex.net

31–40 of 165 posts

Re: Malloc Never Fails (2012)

#32

malloc fails with ulimit

Very relevant, because containers are often under what basically is ulimit. Not that you can do much when malloc fails...

You still have to gracefully shut down the application by closing file handles, aborting database transactions, etc.

Re: Malloc Never Fails (2012)

#33
Malloc is just mmap so how about:

ENOMEM The process's maximum number of mappings would have been exceeded. This error can also occur for munmap(), when unmapping a region in the middle of an existing mapping, since this results in two smaller mappings on either side of the region being unmapped.

Re: Malloc Never Fails (2012)

#36

Earlier quoted context omitted.

Very relevant, because containers are often under what basically is ulimit. Not that you can do much when malloc fails...

> Not that you can do much when malloc fails... Tell the user they can’t do that operation? Use on-disk storage instead? Re-use memory you already have (such as evicting a cache and taking the memory it already had allocated)? Abandon what you were doing if it was only an optimisation and wasn’t essential (such as allocating memory as part of a speculative execution)? Run a garbage collector and try again? Lots of op…

In practice i haven't seen any application to gracefully handle out of memory situations. Even back in Windows 3.x days when running out of memory was common, most applications simply hanged or crashed (which also took the entire GUI with them) and those that didn't often had a "no memory, kthxbye" dialog that shut down the application. A very few rare cases would try to display some "sorry, no memory, close some programs and try again" dialog but even those were hit and miss, depending on the situation.

Once 32bit hardware-based virtual memory entered the picture, every single application would just assume you have endless RAM - memory checks are reserved for "common cases" like trying to create a 100000x100000 image in a (32bit) image editor.

The reason for all that is simple: out of memory situations are stupidly and increasingly rare and in most cases where they can happen there isn't much you can do (e.g. what would you do if you run out of memory while making the fourth button in a toolbar?) and really in the 99% of the cases there might only be five people in the entire universe that will encounter such a case (two of them will tweet about it though and amass a lot of "lol, those garbage developers" retweets) so littering your codebase to keep those five people (and their Twitter followers) happy is not worth the effort. I mean, are you really going to put a "run out of memory" check after every toolbar button allocation? And what are you going to do if that fails? What can you do?

AFAIK some modern languages nowadays even assume memory allocations wont fail (and if they do they just terminate).

Re: Malloc Never Fails (2012)

#37

Earlier quoted context omitted.

Very relevant, because containers are often under what basically is ulimit. Not that you can do much when malloc fails...

You still have to gracefully shut down the application by closing file handles, aborting database transactions, etc.

The OS will close the file handles and any database worth its salt should also abort the transaction automatically when the connection to it is lost before the transaction is finished.

Re: Malloc Never Fails (2012)

#38

Translation: This is a euphemism for saying Linux blatantly violates the language specification with no remorse. (Yes, I realize Linux is the kernel, etc.)

Which part of the specification does this violate?

Like, every part? It doesn't actually allocate the space requested, it doesn't indicate failure by returning NULL (or by any other reliable means for that matter)...

Re: Malloc Never Fails (2012)

#39

Translation: This is a euphemism for saying Linux blatantly violates the language specification with no remorse. (Yes, I realize Linux is the kernel, etc.)

> [...] Linux blatantly violates the language specification [...]

We can't expect a kernel to be aware of all language specifications.

Yes, I know that in this case C is both the program's and the Kernel's language in this example but even then. Languages go through iterations (versions) and a Kernel can't be required to obey all languages which might run under it.

Re: Malloc Never Fails (2012)

#40
post #36

Earlier quoted context omitted.

> Not that you can do much when malloc fails... Tell the user they can’t do that operation? Use on-disk storage instead? Re-use memory you already have (such as evicting a cache and taking the memory it already had allocated)? Abandon what you were doing if it was only an optimisation and wasn’t essential (such as allocating memory as part of a speculative execution)? Run a garbage collector and try again? Lots of op…

In practice i haven't seen any application to gracefully handle out of memory situations. Even back in Windows 3.x days when running out of memory was common, most applications simply hanged or crashed (which also took the entire GUI with them) and those that didn't often had a "no memory, kthxbye" dialog that shut down the application. A very few rare cases would try to display some "sorry, no memory, close some pro…

> what would you do if you run out of memory while making the fourth button in a toolbar?

Rollback what I’d created so far, evict caches, ask malloc to trim, try again, if that failed roll back again and ask the user to reduce the volume of application data open by closing views or documents or whatever before they try again.

But yeah it’s a lot of engineering.

Post reply on HN