6
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ Did you try $HistoryLength = 0; ? $\endgroup$
    – Sumit
    Nov 13, 2016 at 9:44
  • $\begingroup$ @Sumit It relate to this topic? $\endgroup$
    – yode
    Nov 13, 2016 at 9:47
  • $\begingroup$ 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=. . $\endgroup$
    – Sumit
    Nov 13, 2016 at 9:55
  • $\begingroup$ 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. $\endgroup$
    – george2079
    Nov 13, 2016 at 13:44

3 Answers 3

8
$\begingroup$

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

Mathematica graphics

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – yode
    Nov 13, 2016 at 12:16
  • 1
    $\begingroup$ @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. $\endgroup$
    – C. E.
    Nov 13, 2016 at 12:47
  • $\begingroup$ I's seem Internal`Restart can do it more competely. $\endgroup$
    – yode
    Dec 7, 2016 at 15:50
4
$\begingroup$

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

Quit[]

c`mem := UnitConvert[Quantity[MemoryInUse[], "Bytes"], "Megabytes"] //N

c`mem
69.6199 MB
a = RandomReal[1, 20000000];

c`mem
228.553 MB
Unprotect[In, Out, InString, MessageList];
Clear[In, Out, InString, MessageList];
Remove["`*"];
Protect[In, Out, InString, MessageList];
$Line = 0;

c`mem
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 Utilities`CleanSlate`ClearInOut[] that does not seem to work when called but putting the commands directly in your notebook seems to do the trick.

Hope this helps.

$\endgroup$
0
$\begingroup$

Here is a result with and without $HistoryLength = 0; with MMA10.1 Linux.

enter image description here

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

$\endgroup$
1
  • $\begingroup$ But I want to save that result such as %3 or %4 before I release the memory*. $\endgroup$
    – yode
    Nov 13, 2016 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.