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 <sens...@mac.com>
The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer]
Out[2] shows the memory used by the freshly started system (I load some packages via init.m).
"From In[2]" shows the memory used by the system plus the huge amount used by the local variable bar.
Out[4] shows that the system uses slightly more memory after the execution of HilbertMemoryEater[] than before.
Out[5] shows the amount of memory, which has been freed by the forced garbage collection (gc)
and finally Out[5] shows that the gc led to less memory consumption than the freshly started system used.
This indicates that memory used by local variables is freed as soon as the variable isn't used any more.
Hope this helps,
Peter
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...@inbox.ru