8
$\begingroup$

I am trying to implement the code which generates some table, works with it, extracts some numbers, add them to some other table, then clears the table (only the table and not the other elements of the running code) to free up RAM; and then repeats the process n times. Naively, I thought that it is enough to type Clear[table], and that's it. But the process manager shows that this is not the case:

tabbb = Table[{x, 1.01^x^(1/10)}, {x, 1, 2*10^7, 1}];

It holds 1.92 Gb, and the command

Clear[tabbb]

does not free up the space. Could you please show how to free up the space carried by tabbb without exiting kernel?

$\endgroup$
1
  • 2
    $\begingroup$ What operating system are you using? Depending on your operating system, freeing memory inside a program can be recycled within the program but it never gets returned to the operating system until the program exits. stackoverflow.com/questions/52417318/… $\endgroup$ Feb 12 at 12:20

3 Answers 3

7
$\begingroup$

(This is a follow-up to the excellent answer by Nate.)

You can tell Mathematica do not keep the output history:

$HistoryLength = 0;

This works for the current Kernel session only. If you wish to make this permanent, you can add this line to your user-specific Kernel initialization file $UserBaseDirectory/Kernel/init.m.

Unfortunately, due to a bug in the latest Mathematica versions, it doesn't automatically clear the output history, just forbids accumulating it further.

In some situations clearing internal caches can be also of help:

ClearSystemCache[]

Examples:

Also related:

$\endgroup$
9
  • $\begingroup$ Thanks! But wouldn't this option lead to not keeping the other output I'm interested in? $\endgroup$ Feb 12 at 10:34
  • $\begingroup$ @JohnTaylor Setting $HistoryLength = 0 will stop the accumulation of the output history, but you can always set it back $HistoryLength = Infinity (or an integer value) if you wish to resume the accumulation of the output history. Alternatively, you can save your outputs in some variable manually. $\endgroup$ Feb 12 at 11:24
  • $\begingroup$ Thanks! I am wondering whether it is applicable to my toy example with tabbb. $\endgroup$ Feb 12 at 14:33
  • $\begingroup$ @JohnTaylor Are you interested specifically in how the memory allocation is seen from the perspective of your operating system (i.e. the Task Manager of Windows)? The answer by Nate shows how it is seen from the Mathematica's perspective. $\endgroup$ Feb 12 at 15:32
  • $\begingroup$ I just would like to make computations that are RAM consuming. So probably from the perspective of the operating system. $\endgroup$ Feb 12 at 16:23
6
$\begingroup$

Unfortunately, Mathematica retains all output by default until the Kernel is quit. You can clear the output by Unprotecting Out and clearing Out. This does clear all output though, so that may not be what you want. The code below shows an example of the memory being released when using this approach. It will retain any variable definitions though.

In[1]:= tabbb = Table[{x, 1.01^x^(1/10)}, {x, 1, 2*10^7, 1}];



In[2]:= MemoryInUse[]

Out[2]= 2011533152

In[3]:= Remove[tabbb]



In[4]:= MemoryInUse[]

Out[4]= 2011808384

In[5]:= Unprotect[Out]

Out[5]= {"Out"}

In[6]:= Clear[Out]

In[7]:= MemoryInUse[]

Out[7]= 90108848

$\endgroup$
6
  • $\begingroup$ Maybe then it would be possible to keep the needed definitions in one kernel while using another kernel inside the notebook for calculating the tabbb? $\endgroup$ Feb 11 at 22:47
  • $\begingroup$ The definitions shouldn't be cleared when using this approach. It just clears what is stored in the Out variable. $\endgroup$
    – Nate
    Feb 11 at 22:48
  • $\begingroup$ But if I have the other output (say, the other computed tables used for calculating tabbb) then Clear[Out] would delete also it? $\endgroup$ Feb 11 at 22:50
  • $\begingroup$ If you had another variable say table1 that was storing those other tables, clearing Out shouldn't get rid of those tables unless you also cleared those individually. $\endgroup$
    – Nate
    Feb 11 at 22:51
  • $\begingroup$ Thanks! Could you please tell me what is wrong with this toy cycle? MemoryInUse[] Clear[tabb] tabb = {{1, 1}}; For[i = 0, i <= 4, i++, Number1Dat = RandomReal[{0, 1}, 10^8]; Number2Dat = RandomReal[{3, 4}, 10^8]; tabb = Join[tabb, {{Mean[Number1Dat], Mean[Number2Dat]}}]; Remove[Number1Dat]; Unprotect[Out]; Clear[Out]; Remove[Number2Dat]; Unprotect[Out]; Clear[Out]; ] MemoryInUse[] tabb $\endgroup$ Feb 11 at 23:09
4
$\begingroup$

You may selectively delete a specific output by changing "$Line".

Assume that we want to change the output from line 1 to an empty string "". Toward this aim, we first save the current value of "\$Line". Then we set "\$Line" to 1 and output the empty string. Subsequently we need to restore "\$Line":

tmp = $Line; $Line = 1; ""
$Line = tmp;

If we now say "%1" we will get an empty string as output.

$\endgroup$
2
  • $\begingroup$ Hi! Is it applicable to my toy example? $\endgroup$ Feb 12 at 14:33
  • 1
    $\begingroup$ This refers to everything that you displayed on your screen, what is stored in Out[]. I does not refer to values stored in variables, There you need Clear. $\endgroup$ Feb 12 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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