Re: Optimizing memory
- To: mathgroup at smc.vnet.net
- Subject: [mg64868] Re: Optimizing memory
- From: Peter Pein <petsie at dordos.net>
- Date: Mon, 6 Mar 2006 05:01:19 -0500 (EST)
- References: <due8jj$a4e$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Sensei schrieb: > 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] > > Consider the following example: In[1]:= HilbertMemoryEater[foo_] := Module[{bar = Array[1/(#1 + #2 - 1), {foo, foo}]}, Print[MemoryInUse[]]] In[2]:= MemoryInUse[] HilbertMemoryEater[3333] MemoryInUse[] Share[] MemoryInUse[] Out[2]= 2408424 From In[2]:= 525622432 Out[4]= 2425616 Out[5]= 123296 Out[6]= 2303064 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