3 ms·
Interesting observation that contradicts my experience: > A common misconception is that checking for allocation failure is pointless, since a program might be
by vlovich123 4y ago
Interesting observation that contradicts my experience:
> A common misconception is that checking for allocation failure is pointless, since a program might be terminated by the OS if memory is exhausted, or because platforms that implement memory overcommit (such as Linux) will always return a pointer as though allocation had succeeded, and crash when writing to that pointer. This is a misconception for the following reasons:
> Memory overcommit on Linux can be turned off.
> Linux is not the only platform.
> Memory exhaustion is not the only situation where allocation might fail: if memory is sufficiently fragmented that a chunk of the requested size is not available, allocation will fail.
This is kind of the eternal vim vs eMacs debate but for malloc. In practice, I’ve not found this philosophy to be useful and actually problematic and the reasons given are the “easy” ones to convince more junior engineers. They’re true btw and the counter points provided aren’t really convincing. Regardless, the real reasons are:
* Memory allocation failures happen infrequently in practice.
* No one writes tests that simulate behavior of code under allocation failures.
* Memory allocation failures are basically treated as contract violations anyway in terms of triage - it’s not different from any other bug and you’d still need to fix it.
Crashing on a memory allocation failure:
* there’s no untested error recovery code to worry about
* the cause of the malloc failure means something else is broken / misconfigured. A crash tells you what to fix / when to fix it. A silent recovery attempt (when successful) will mask this and shift the problem (eg bug in error handling later or something)
There are times when you allocate memory and could handle failure gracefully by rejecting the request or something. However, as I mentioned. Memory allocation failures are rare and when they happen you need to fix a bug anyway. So crashing on a rare condition instead of running untested error recovery code is probably preferable in general.
Context: I have experience in mobile, embedded, desktop, and cloud so I’ve seen the gamut of dev environments. And everywhere the “abandon on malloc failure” turns out to be a more maintainable strategy that results in more robust code that’s simpler and shorter and faster/cheaper to develop because you’re not writing and thinking about error handling paths that never get run.
Regardless, it is kind of interesting to see a language adopt this philosophy. Maybe that can address some of the problems of trying to do this manually in other languages. I’m skeptical because the error paths problem remains, but I’m open to the experiment
- dboreham 4y agoCan't disagree, but still it feels wrong, doesn't it? If we consider instead filesystem space exhaustion, it's easier to imagine a reasonable expectation that the system could enter a degraded service state (e.g. reads work but writes don't), then recover without restart. In fact I've worked on products that expected to provide exactly that kind of behavior, and we (tried to) test it.
- vlovich123 4y agoIf you enter file system exhaustion (or a read only file system), most applications should fail similarly. You want to be very very careful to only add error handling for known failure modes you know, expect, have seen in production, and have test coverage for. Everything else should fail as gracefully as possible. Anything else is “safety theater” where you feel better but it’s totally illusory. So yes, some applications should handle a file system full failure. I’ve written code where the file system code was problematic (an infinite loop causing 100% cpu usage is common because it’s a daemon). I still will take that because it was caught so early in testing and we had a clear repro case to go after to reproduce the problem, fix it etc. The other huge difference is that memory allocation is everywhere. Technically, everything should be passing a memory allocator if the function might allocate. After all, maybe I want my container to allocate the next set of nodes somewhere else or at the very least I want to use a pool allocator for some scope of code. In practice, this too is too much even though it’s infinitely more practical and useful than failable allocations. TLDR: memory allocations are qualitatively different enough because it’s hidden global state that’s used everywhere. If you’re not going to have a memory allocator passed into every function as an override able parameter, faillable allocations aren’t practically interesting. This was a lesson learned about a decade ago but it seems like there’s pockets in the community that still disagree and push back against this. I wish them luck.
- teknico 4y ago> Technically, everything should be passing a memory allocator if the function might allocate. This is how Zig works. > In practice, this too is too much even though it’s infinitely more practical and useful than failable allocations. It seems to be working well enough for Zig.