Skip to main content

kernel - Aborting evaluation when the memory exceeds a certain limit


Sometimes when generating a lot of data the memory usage shoots beyond its physical realm and my laptop freezes. There is no way to stop Mathematica when this happens. Task manager (or its equivalent in other OSes) won't even start.


I was wondering if it's possible to set a threshold somewhere in Mathematica to stop it automatically after a certain amount of memory.




Answer



Since one may not always accurately predict when MemoryContrained is needed, I recommend setting up a watch-dog task. Belisarius described how to do this here in answer to my question. I will reproduce it below as answers that are merely links are discouraged.





In Mathematica 8 you could start a memory watchdog, something along the lines of:


maxMemAllowed        = 1.3 1024^3; (*1.3 GB*);
intervalBetweenTests = 1; (*seconds*)
iAmAliveSignal = 0;
Dynamic[iAmAliveSignal]
RunScheduledTask[

If[MemoryInUse[] > maxMemAllowed , Quit[], iAmAliveSignal++],
intervalBetweenTests];

Remember to run


RemoveScheduledTask[ScheduledTasks[]];

to disable it.



Comments