# May I restrict caching to disk when memory runs low? [duplicate]

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?

-

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

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
Use MemoryConstrained
MemoryConstrained[yourCode, memoryLimit, actionOnMemoryLimitOverflow]