1
$\begingroup$

Actually relate my this post.But I don't want to clear all memory took up by Mathematica.

memory:=Row[{"Memory Used:",MemoryInUse[]/1024^2.," MB"}]
memory
(* Memory Used:109.582 MB *)

a=RandomInteger[1,{10^8}];
memory
(* Memory Used:871.666 MB *)

b=RandomInteger[1,{10^8}];
memory
(* Memory Used:1634.61 MB *)

I want to clear the variable a and release the space took up by variable a but keep the b. Is possible?

$\endgroup$
4
  • $\begingroup$ Have you tried $HistoryLength=0 and Clear[a]? $\endgroup$
    – Carl Woll
    Jun 20, 2017 at 20:09
  • $\begingroup$ @CarlWoll Yes,I have.But it don't work.. $\endgroup$
    – yode
    Jun 20, 2017 at 21:19
  • $\begingroup$ We really need a canonical question for this sort of problem. It's not clear, it's not easy to find in the documentation, it has finer points such as the requirement to delete the Out cells and make sure they're not anywhere in the DownValues for Out either. I say, this should stay open. $\endgroup$
    – LLlAMnYP
    Jun 22, 2017 at 6:03
  • $\begingroup$ See common pitfalls / $HistoryLength $\endgroup$
    – Kuba
    Dec 10, 2018 at 7:47

1 Answer 1

4
$\begingroup$

Using $HistoryLength = 0 and Clear[a] works for me:

$HistoryLength = 0;
memory:=Row[{"Memory Used:",MemoryInUse[]/1024^2.," MB"}]
memory

a=RandomInteger[1,{10^8}];
memory

b=RandomInteger[1,{10^8}];
memory

Clear[a];
memory

Memory Used:227.932 MB

Memory Used:990.87 MB

Memory Used:1753.81 MB

Memory Used:990.868 MB

$\endgroup$
2
  • $\begingroup$ Oh,I have misunderstand it before.Thanks a lot.. $\endgroup$
    – yode
    Jun 20, 2017 at 21:47
  • $\begingroup$ a = RandomInteger[1, {10^8}]; (* <-- this is In[136] *) Clear[a]; Unprotect[Out]; Out[136] =.; Protect[Out]; is also a working approach. $\endgroup$
    – LLlAMnYP
    Jun 21, 2017 at 13:36

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