# How to remove all of the memory used by Mathematica

### Backgroud

When I create the notebook,it will be made mess by me sometimes.If I use Remove to remove those variables,the memory cannot release still.So I have to close this mess notebook and restart the MMA to create another new notebook.I want to find way by programal method to avoid restarting the MMA,which can remove all of the variable and can release the memory.

Test code:

In[1]:= memory := Row[{"Memory Used:", MemoryInUse[]/1024^2., " MB"}];
memory
Out[2]= Memory Used:99.3511 MB

In[3]:= a = RandomReal[1, 20000000];
memory
Out[4]= Memory Used:255.345 MB


As we see,the value of memory used by Mathematica currently is $255.345MB$.How to release it and make it back to $99.3511M$?As I try,the ClearAll,Remove and ClearSystemCache cannot do this.

Update: As this answer,I change the test code to be following:

In[1]:= cmemory :=Row[{"Memory Used:", MemoryInUse[]/1024^2., " MB"}];
cmemory
Out[2]= Memory Used:58.117 MB

In[3]:= a = RandomReal[1, 20000000];
cmemory
Out[4]= Memory Used:216.487 MB

In[5]:= Unprotect[In, Out];
Clear[In, Out]
Quiet[Remove["*"]]
$Line = 0; In[1]:= cmemory Out[1]= Memory Used:63.2716 MB  The last output is$63-58=5M$lager than first output still.How to release it? • Did you try $HistoryLength = 0; ? – Sumit Nov 13 '16 at 9:44
• @Sumit It relate to this topic? – yode Nov 13 '16 at 9:47
• the large chunk of memory might be a history. By telling MMA not to store any history you might be able to free the memory (works for me sometimes). You can also try Clear["Global*"] or simply a=. . – Sumit Nov 13 '16 at 9:55
• from what youve written I wonder if you know you can simply restart the kernel? Im not sure these more complicated approaches are worth the effort to save 3-4 seconds. – george2079 Nov 13 '16 at 13:44

As Szabolcs wrote in this great answer, there are, from the old days, two functions that are meant to restore the kernel to its former state and recover as much memory as possible without having to restart it.

Try this on a fresh kernel:

memory := Row[{"Memory Used:", MemoryInUse[]/1024^2., " MB"}];
Needs["UtilitiesCleanSlate"]
memory
a = RandomReal[1, 20000000];
memory
CleanSlate[];
ClearInOut[];
memory


All memory was recovered. Note that it only resets definitions that were made after the package was loaded, therefore in this case it didn't reset the definition for memory.

• I cannot understand what meaning of slate,and I found a strange thing,when I use the ClearInOut,It will result to more memory to be used.This is a reproduce phenomenon. – yode Nov 13 '16 at 12:16
• @yode It recovers as much memory as possible but not all of it in some cases. Szalbocs notes in his original answer that memory cannot be recovered perfectly, this is the best that we have. As for the meaning of "slate" it could be thought of as coming from the common translation of "tabula rasa" to either "clean slate" or "blank slate," but originally, as the article says, it came from the use of slate sheets. – C. E. Nov 13 '16 at 12:47
• I's seem InternalRestart can do it more competely. – yode Dec 7 '16 at 15:50

You need to clear InString and MessageList as well since these take up memory.

Quit[]

cmem := UnitConvert[Quantity[MemoryInUse[], "Bytes"], "Megabytes"] //N

cmem

69.6199 MB

a = RandomReal[1, 20000000];

cmem

228.553 MB

Unprotect[In, Out, InString, MessageList];
Clear[In, Out, InString, MessageList];
Remove["*"];
Protect[In, Out, InString, MessageList];
$Line = 0; cmem  68.5551 MB  I got this information while at WTC2014 from a talk titled Beyond Quit[]: Master Your Memory in the Wolfram Language; more info in there. There is a utility in the notebook called UtilitiesCleanSlateClearInOut[] that does not seem to work when called but putting the commands directly in your notebook seems to do the trick. Hope this helps. Here is a result with and without $HistoryLength = 0; with MMA10.1 Linux.

As you can see \$HistoryLength = 0; does save some extra memory.

• But I want to save that result such as %3 or %4 before I release the memory*. – yode Nov 13 '16 at 10:31