Skip to main content

How to un-eat memory?


Consider this code:


MemoryInUse[]
T = Table[RandomComplex[], {i, 1, 6000}, {j, 1, 6000}];
MemoryInUse[]
T += T\[ConjugateTranspose];
MemoryInUse[]
{Es, Ys} = Eigensystem[T];
MemoryInUse[]

T = Table[RandomComplex[], {i, 1, 6000}, {j, 1, 6000}];
MemoryInUse[]
T += T\[ConjugateTranspose];
MemoryInUse[]
{Es, Ys} = Eigensystem[T];
MemoryInUse[]
$HistoryLength = 0;
MemoryInUse[]
Clear[T]
MemoryInUse[]

Clear[Es, Ys]
MemoryInUse[]
ClearSystemCache[]
MemoryInUse[]

It gives me the following results:



15808208


880820520


1456822832



4919500424


5783503032


6359505096


9822181440


9822182648


9822182112


9822181384


9822162952



Clearly, the memory clears negligibly on any of ClearSystemCache, Clear and zeroing $HistoryLength. Repeating its execution leads to swapping, after start of which I hurry up to kill MathKernel before my X or WM or anything else are OOM-killed.



So what are the working ways to release the memory?



Answer



$HistoryLength is just a global variable. It not clear the history. You can still access the history by


ByteCount@Out[12]
ByteCount@%10

It can be cleared by


Unprotect[In, Out]
Clear[In, Out]


However, it would be better if you set $HistoryLength=0 before your resource-intensive code.


P.S. It would be great to have $HistoryMemoryLimit or something like this.


Comments