Re: Garbage collection and mutable data structures
- To: mathgroup at smc.vnet.net
- Subject: [mg85639] Re: Garbage collection and mutable data structures
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Sun, 17 Feb 2008 07:16:12 -0500 (EST)
- Organization: University of Bergen
- References: <fp67h9$5f6$1@smc.vnet.net>
massyah at gmail.com wrote: > Hi MathGroup, > > This snippet of code is giving me headaches: > > ----- > $HistoryLength = 0; > NewCell[i_] := Module[{c}, CellVal[c] ^= i; c] > MemoryInUse[] > NewCell /@ Range[10^5]; > MemoryInUse[] > ----- > > It occurred while trying to build mutable data structures by using > upvalues. I can't understand why the temporary variable c is not freed > since I don't keep any reference to it. > Have I missed something in the reference counting scheme ? Hi, It looks like if you let a temporary escape from the Module[] (i.e. you create references to it outside the Module[] by returning it), then it won't be cleaned up. In[1]:= Module[{var}, var; 1] Out[1]= 1 In[2]:= ?Global`* Global`var OK, temporary was cleaned up. Now let us return it from the Module[]: In[3]:= Module[{var}, var] Out[3]= var$79 In[4]:= ?Global`* Global` var var$79 As you see, now it persists. Now let's try assigning something to it. In[5]:= Module[{var}, var = 10; Hold[var]] Out[5]= Hold[var$87] In[6]:= ?Global`* Global` var var$79 var$87 In[7]:= ?var$87 Global`var$87 Attributes[var$87]={Temporary} var$87=10 It looks like it keeps the value.