Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Example: Try this example only if you have > 4G ram

d = 2000; 
f[x_, y_, d_] = 
     Sum[Subscript[a, i, j]*x^i*y^j, 
       {i, 0, d - 1}, {j, 0, d - i}];




f[x_, y_, d_] =.

You will see that the memory usage is not reduced after calling Clear or using Unset.

Question: How to free the memory when I don't need a function anymore which is already evaluated?

Edit: I look at the memory usage in taskmgr under Windows OS. As the answers report that the memory is indeed reduce by using MemoryInUse to check it.

share|improve this question
Try setting $HistoryLength=0. Then you'll not use additional memory for saving results of old computations (anything like %1 won't work in this case). – Ruslan Aug 17 '13 at 17:16
@Ruslan, But I need to save the results for some later use, only to a certain point I don't need some old results anymore. – user565739 Aug 17 '13 at 17:20
User, I don't understand; do you want to save the results to a file? – Mr.Wizard Aug 17 '13 at 17:22
Not saving to a file. I need to use the result of f later at some point in the computation, and after that point, I don't need it anymore. – user565739 Aug 17 '13 at 17:26
If you define f with $HistoryLength=0, it'll still remain defined until you Clear it. You just won't be able to access output of any previous computed cells via %n, and "Show More" etc. buttons won't work. – Ruslan Aug 17 '13 at 17:28

In version 7 I cannot confirm what you report. With my default of $HistoryLength = 3 I see:

d = 2000;
f[x_, y_, d_] = Sum[Subscript[a, i, j]*x^i*y^j, {i, 0, d - 1}, {j, 0, d - i}];





With $HistoryLength = 0 as suggested by Ruslan:



Michael suggested using Block to set $HistoryLength but this doesn't actually work. Instead his code functions by a different method: it never assigns the output of the Sum to Out[], because that line returns MemoryInUse[]. The same thing can be done with with CompoundExpression:

d = 2000;
f[x_, y_, d_] = Sum[Subscript[a, i, j]*x^i*y^j, {i, 0, d - 1}, {j, 0, d - i}]; 1;



The key is:

expr; 1;

Now the value set for Out[] is 1 rather than expr.

You may wonder why expr; assigns anything to Out[] -- this is intended and useful behavior because you often want to suppress the output of a line and reference it in the line below using %. expr; is short for CompoundExpression[expr, Null], which is recognized and handled to set Out[] to the evaluated expr instead of Null.

share|improve this answer
I don't know what is my default $HistoryLength. I just look at the memory usage showed in taskmgr. After using MemoryInUse, it shows that the reduced memory is very little if I don't use $HistoryLength. – user565739 Aug 17 '13 at 17:38
@user565739 If you didn't change it, your default is Infinity. – Ruslan Aug 17 '13 at 17:40
@user565739 You shouldn't use taskmgr is that doesn't show memory that is actually in use but rather that which the application hasn't released; it's not the same thing. If another application needs the memory it should be available. – Mr.Wizard Aug 17 '13 at 17:41
@Mr.Wizard well if an application hasn't released some memory, other apps can't access it (unless it's swapped out). But taskmgr shows memory used by apps + caches etc. (starting from windows vista), which will mislead you if you try to detect when an app releases some memory. – Ruslan Aug 17 '13 at 17:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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