Preventing Mathematica from eating up all your memory

A Mathematica user recently contacted me to report a suspected memory leak, wondering if it was a known issue before escalating it any further. At the beginning of his Mathematica notebook he had the following command

Clear[Evaluate[Context[] <> "*"]]

This command clears all the definitions for symbols in the current context and so the user expected it to release all used memory back to the system. However, every time he re-evaluated his notebook, the amount of memory used by Mathematica increased. If he did this enough times, Mathematica used all available memory.

Looks like a memory leak, smells like a memory leak but it isn’t!

What’s happening?

The culprit is the fact that Mathematica stores an evaluation history. This allows you to recall the output of the 10th evaluation (say) with the command

%10

As my colleague ran and re-ran his notebook, over and over again, this history grew without bound eating up all of his memory and causing what looked like a memory leak.

Limiting the size of the history

The way to fix this issue is simply to limit the length of the output history. Personally, I rarely need more than the most recently evaluated output so I suggested that we limit it to one.

$HistoryLength = 1;

This fixed the problem for him. No matter how many times he re-ran his notebook, the memory usage remained roughly constant. However, we observed (in windows at least) that if the Mathematica session was using vast amounts of memory due to history, executing the above command did not release it. So, you can use this trick to prevent the history from eating all of your memory but it doesn’t appear to fix things after the event…to do that requires a little more work. The easiest way, however, is to kill the kernel and start again.