Context
I run long Mathematica sessions; sometimes I have Output up to Out[3000+]. Hence I would like to be able to clear out some previous outputs which do use lots of memory.
Attempt
I am aware of clearing some outputs via unprotecting Out as follows
In[1]:= MemoryInUse[]/10^9.
Out[1]= 0.0179439
In[2]:= a = Table[1, {10^8}];
In[3]:= MemoryInUse[]/10^9.
Out[3]= 0.419424
In[4]:= Unprotect[Out];
In[5]:= Out[2] =.
In[6]:= Protect[Out];
In[7]:= MemoryInUse[]/10^9.
Out[7]= 0.0297147
My problem is if I have 3000+ outputs, which one are using a lot of memory so I can decide if it is safe to clear them.
Question
What I am after is a way of finding out which amongst the previous Out are using most of the memory?
I would like to have a function, say MemoryHog[n] which would return a list of numbers corresponding to the nth sorted Outputs using most memory.
$Linebefore I did, but you seem to have forgotten aboutSortBy. – Mr.Wizard Aug 20 '12 at 11:06list=SortBy[list, -Last[#] &]would work as well... – Ajasja Aug 20 '12 at 11:16Instring/@ Transpose[MemoryHog[3]][[1]]would allow me to review the corresponding Inputs. On the other hand its not very readable. Would you know how to print it out as the equivalent of??Inbut restricted to the hogs? It would help to decide if they can be cleared without pain. – chris Aug 20 '12 at 11:55DisplayForm@ToExpression@InString[#] & /@ MemoryHog[3][[All, 1]]– Ajasja Aug 20 '12 at 12:08