[Python-ideas] More details in MemoryError

MRAB python at mrabarnett.plus.com
Tue Jan 22 19:36:07 CET 2013


On 2013-01-22 13:04, Chris Angelico wrote:
> On Tue, Jan 22, 2013 at 11:42 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> Something like this could be used to decide whether or not to flush
>> unimportant in-memory caches, compact data structures, etc., or just
>> give up and exit.
>
> That's a nice idea, but unless the requested allocation was fairly
> large, there's a good chance you don't have room to allocate anything
> more. That may make it a bit tricky to do a compaction operation. But
> if there's some sort of "automatically freeable memory" (simple
> example: exception-triggered stack unwinding results in a whole bunch
> of locals disappearing), and you can stay within that, then you might
> be able to recover. Would require some tightrope-walking in the
> exception handler, but ought to be possible.
>
FYI, allocating memory specially for such cases is sometimes called a
"memory parachute".

I wonder whether you could have a subclass of MemoryError called
LowMemoryError.

If allocation fails and there's a parachute, it would free the
parachute and raise LowMemoryError. That would gave you a chance to
tidy up before quitting or even, perhaps, free enough stuff to make a
new parachute and continue working.

If allocation fails and there's no parachute, it would raise
MemoryError as at present.

With LowMemoryError as a subclass of MemoryError, existing code would
still work the same.



More information about the Python-ideas mailing list