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]:= c`memory :=Row[{"Memory Used:", MemoryInUse[]/1024^2., " MB"}];
c`memory
Out[2]= Memory Used:58.117 MB
In[3]:= a = RandomReal[1, 20000000];
c`memory
Out[4]= Memory Used:216.487 MB
In[5]:= Unprotect[In, Out];
Clear[In, Out]
Quiet[Remove["`*"]]
$Line = 0;
In[1]:= c`memory
Out[1]= Memory Used:63.2716 MB
The last output is $63-58=5M$ lager than first output still.How to release it?
Answer
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["Utilities`CleanSlate`"]
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
.
Comments
Post a Comment