# Memory not freed after running Clear when using a Table [duplicate]

When I'm creating large lists with Table I see the memory footprint increasing.

MemoryInUse[]/1024.
(* Out-> 16992.4 *)

data = Table[x, {x, 0, 10000}, {y, 10000}];
MemoryInUse[]/1024.
(* Out-> 408372. *)


I would expect that when I generate the same command data = Table[...] just as above the memory would not increase but in fact it just allocates a new block of memory.

data = Table[x, {x, 0, 10000}, {y, 10000}];
MemoryInUse[]/1024.
(* Out -> 799041. *)


Why is that? Even when I do:

Clear[data];
MemoryInUse[]/1024.
(* Out-> 799044. *)


Is this expected behaviour or am I missing something? I can't seem to find a function that clears the memory.

I'm using Mathematica 8.0.4.0 on Windows 7

• – Kuba Dec 10 '18 at 7:49

Have you tried setting $HistoryLength to zero: $HistoryLength=0;

The memory is not freed, because the previous data is still available by Out[nn] or %nn.
Note that if you do this, using %, %%, %n etc., which some people use in example code will not work. And when a large data set is shown the "Show More" and "Show Less" buttons do not work. You can bypass both problems by setting \$HistoryLength to 1 while still conserving a lot of memory.