# Old values are not freed/garbage collected when you re-evaluate an assignment

For this code:

(* Cell 1 *)
generate := Module[{x}, x = Range[100 * 1000 * 1000]; x];

(* Cell 2 *)
g = generate[];
MemoryInUse[]


If I evaluate cell 2 repeatedly, the memory consumption keeps growing by 400 MB each time (as seen in the OS task manager). For some reason, it seems that Mathematica does not free the memory taken by the old value of g.

Can someone please explain what is going on here? And is there a way to force the freeing of old g values? Due to this issue I need to keep on quitting the Mathematica kernel.

-
Sometimes just calling Share[] already frees substantial memory. –  celtschk Mar 22 '12 at 21:20
@celtschk Share[] works by making sure that the same expression is not stored twice. It uses pointers for duplicate occurrences, such as in x^2 in f[x^2, x^2]. Unfortunately it won't help with packed arrays like the one in the OP's example. –  Szabolcs Mar 22 '12 at 21:27

You need to set

\$HistoryLength = 0


(or other small value) at the beginning of the session to prevent Out from remembering previous outputs. One non-obvious thing about Out is that if we do

In[1]:= a=1;


then Out[1] will still be set to 1 despite the semicolon at the end of the input!

There's also the CleanSlate package which has a ClearInOut function that clears old inputs and outputs.

-
If you define ignore[_]:=Null you can selectively avoid this storage by writing ignore[a=1] instead of a=1;. Note that (a=1;Null) seems not to be sufficient. –  celtschk Mar 22 '12 at 21:26
@celtschk Yep, though seems like a lot of work. Interesting observation about adding Null. –  Szabolcs Mar 22 '12 at 21:30
@celtschk I sometimes would do a=1 //Null& which is the same as you do. Probably the Null doesn't work because the actual full form of a; is CompoundExpression[a, Null], equivalent to appending Null –  Rojo Mar 23 '12 at 1:52
That comment leads me to the following solution to get the expected behaviour: Unprotect[CompoundExpression];CompoundExpression[x___]:=Last[Evaluate/@{x}];Prote‌​ct[CompoundExpression] –  celtschk Mar 23 '12 at 7:04
@Rojo: I just notice that your code assigns Null to a. I guess you meant (a=1)//Null& (but then, Null&[a=1] is shorter and indeed a nice alternative to my ignore` function) –  celtschk Mar 23 '12 at 7:09