Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

An hour ago, I had to hard reset my laptop again because Mathematica froze the system again. When import a large file or make a mistake that produces huge arrays with complicated entries etc., I often see that the system becomes unresponsive. Holding the power button for seconds seems to be the only solution.

I was forced to do so about 20 times in recent 3 months, because of a project I was working at. Today, chkdsk had to start already when Windows was starting which I think is creepy. No errors found on the disk, thank God.

Is there a way to constrain this behavior so that it doesn't become hopeless? Some setting which makes Mathematica say "I give up, low memory" instead of doing the impossible and suicidal caching?

share|improve this question

marked as duplicate by C. E., Michael E2, Öskå, Oleksandr R., m_goldberg Aug 22 '14 at 13:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

I believe this is a duplicate of: (2789). Also I have had some success using this: $Pre = Function[Null, MemoryConstrained[Unevaluated @ #, 2^32, "Memory Exceeded"], HoldAll] (see (30600)) -- it doesn't always work but it seems to most of the time. – Mr.Wizard Aug 22 '14 at 6:53
You may have to be patient, but normally Windows does not actually freeze when this happens. If you can stand to wait for a few minutes (possibly up to 10), it's a better alternative than switching off the computer. Obviously it's better still to prevent this situation occurring in the first place, but that's not always possible. – Oleksandr R. Aug 22 '14 at 11:02
LOL, right, I actually got out of the doom after 10 minutes once. But it seems so uncertain... – Luboš Motl Aug 22 '14 at 11:52
up vote 3 down vote accepted

Use MemoryConstrained

MemoryConstrained[yourCode, memoryLimit, actionOnMemoryLimitOverflow]
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.