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.
Share[]
already frees substantial memory. $\endgroup$Share[]
works by making sure that the same expression is not stored twice. It uses pointers for duplicate occurrences, such as inx^2
inf[x^2, x^2]
. Unfortunately it won't help with packed arrays like the one in the OP's example. $\endgroup$