Is "Out of Memory" a recoverable error?

Don nospam at
Tue Dec 2 05:57:49 PST 2008

Walter Bright wrote:
> I asked this over on to see what people using other 
> languages have to say, as well as the D community. The reason I ask is 
> to see if memory allocation can be allowed in functions marked "nothrow".
I don't think it can be recoverable. Or rather, if it is recoverable, it 
shouldn't have happened in the first place.

As far as I can tell, the only thing you could do to recover from an 
out-of-memory condition is (1) to free some memory, or (2) to switch to 
an algorithm which doesn't need as much memory.

Strategy (1):
Windows used to have a WM_COMPACTING message (maybe it still does) which 
was sent when the system was running low on memory. In D, you could 
imagine a similar sort of system callback, which is called when memory 
is short -- it means, free some memory now, otherwise you'll get an out 
of memory error.
This is much simpler and more powerful than catching an 
OutOfMemoryException, freeing some memory, and then repeating what you 
were doing.

Strategy (2):
If you've got a second algorithm to use, why weren't you checking 
available memory, and choosing the correct algorithm in the first place?

I don't think either of these strategies make sense. The technique of 
catching exceptions works because you have locality of reference of 
almost all resources. If you get a FileException, the bit of code which 
deals with that particular file is small, and you can unroll the call 
stack to get past it. But memory usage is a whole-program thing. The 
biggest unnecessary memory allocation might be a completely unrelated 
part of the program.

More information about the Digitalmars-d mailing list