# How to automatically restart Mathematica kernel after out-of-memory error

I want to run a Mathematica code repeatedly, even after the kernel shuts down when it is out of memory, because I have designed my program to continue appending the data to a file. Currently, I have to restart the kernel manually every 10 minutes. Does anyone have an idea how one can automate this process? Thanks.

-
There's no way to automate this because a fresh kernel won't know what it did in the past. Why do you want to let things get to the point where the kernel crashes? Instead, you can try to use MemoryConstrained to abort the computation when the memory exceeds a certain threshold and then restart it... –  R. M. Jul 12 '13 at 18:15

If you preselect a range of cells before execution you can continue execution past kernel quit using the option below. Selection and evaluation can be automated and done repeatedly.

SetOptions[\$FrontEnd, "ClearEvaluationQueueOnKernelQuit" -> False]


(MemoryConstrained looks a good solution for your specific problem though.)

-
Thanks! SetOptions works great! –  math-student Jul 13 '13 at 1:07

Here is what rm -rf just mentioned, in the form of an example:

Do[
MemoryConstrained[
a = Range[10^n], 10000];
Print[Total[a]],
{n, 1, 10}
]


Output:

55

5050

500500

500500

500500

500500

500500

500500

500500

500500


By using MemoryConstrained, you don't get any changes in the list a after the maximum memory is reached in the above loop, but the loop continues to run.

-