MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Optimizing memory

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64887] Re: Optimizing memory
  • From: Maxim <m.r at inbox.ru>
  • Date: Mon, 6 Mar 2006 05:01:57 -0500 (EST)
  • References: <due8jj$a4e$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Sun, 5 Mar 2006 08:47:47 +0000 (UTC), Sensei <senseiwa at mac.com> wrote:

> Hi!
>
> I wrote a parser for PDB files that calculates atom and residue
> inertia matrices, and I am sure I have written the worst code ever :)
> I'm new to Mathematica, so I don't think I know how to optimize my
> code. One particular concern is this.
>
> Using Module[] and defining many local symbols, is the memory
> associated to them deallocated when the call exits? Or is it possible
> to force memory deallocation?
>
> myParser[filename_]:=Module[
>     { (* many symbols *) },
>     symbol1 = ...;
>     symbol2 = ...;
>     (* very BIG memory allocation *)
>     ...
> ]
>
>
> Thanks for any information!
>
> --
> Sensei <senseiwa at mac.com>
>
> The optimist thinks this is the best of all possible worlds.
> The pessimist fears it is true.      [J. Robert Oppenheimer]
>
>

The basic idea is that local Module variables are discarded if no  
references to them exist when Module quits:

In[1]:= Module[{x = 1}, x++]; Names["x$*"]

Out[1]= {}

In[2]:= Module[{x = 1}, f[] := x++]; Names["x$*"]

Out[2]= {"x$39"}

In[3]:= Array[f[]&, 5]

Out[3]= {1, 2, 3, 4, 5}

There is one complication, though, which is related to assignments to  
parts of expressions:

In[4]:= $Version

Out[4]= "5.2 for Microsoft Windows (June 20, 2005)"

In[5]:= Table[
   Module[{L = Array[0&, 32764]},
     L[[#]]++& /@ Range[32764]];
   Names["L$*"],
   {2}]

Out[5]= {{"L$40"}, {"L$51"}}

In[6]:= Table[
   Module[{L = Array[0&, 32765]},
     L[[#]]++& /@ Range[32765]];
   Names["L$*"],
   {2}]

Out[6]= {{"L$61"}, {"L$61", "L$71"}}

We can see that in both cases the local variable still exists after the  
Module terminates. In the first case we can say that it gets overwritten  
on the subsequent calls to Module. In the second example, however, the  
memory taken by the list L is never freed. This is also confirmed by  
MaxMemoryUsed: the amount of allocated memory will remain almost constant  
after each subsequent iteration in the first case and increase linearly in  
the second case. This definitely seems to be a problem with part  
assignments.

Maxim Rytin
m.r at inbox.ru


  • Prev by Date: Re: Optimizing memory
  • Next by Date: Distinguished logarithm, branch cuts, etc.
  • Previous by thread: Re: Optimizing memory
  • Next by thread: Numerical solutions of SDE