Re: Compile + Module => Memory Leak

*To*: mathgroup at smc.vnet.net*Subject*: [mg39945] Re: Compile + Module => Memory Leak*From*: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>*Date*: Thu, 13 Mar 2003 03:00:17 -0500 (EST)*Organization*: Universitaet Leipzig*References*: <b4mocf$inm$1@smc.vnet.net>*Reply-to*: kuska at informatik.uni-leipzig.de*Sender*: owner-wri-mathgroup at wolfram.com

Bill Rowe wrote: > > On 3/11/03 at 2:37 AM, bgress at mail.DOTucr.DOT.edu (Bern) wrote: > > [example snipped] > > >Presumably, upon exit, a Module should release any memory used, no? > > No, I don't believe this is the way Mathematica deals with memory. My understanding is Mathematica releases memory that is no longer needed as determined by whether there are expressions that reference that memory. Once all of the expressions that reference a piece of memory are deleted the referenced memory is released. > > In the code you posted (that I snipped) the Module construct creates symbolic references. The code does not delete the created symbols. As far as I know, neither Module nor Block deletes symbols created unless you write code to specifically do this. Since the symbols are not specifically deleted, the memory these symbols reference is not released. Hi, ?Temporary Temporary is an attribute assigned to symbols which are created as local \ variables by Module. and the help say "Symbols with attribute Temporary are automatically removed when they are no longer needed." so you have *not* to write code to remove the symbols from Module[]. Block[] only add the new symbols to the global symbol stack, an when you leave the Block[] the stack pointer is reseted to it's original value -- nothing to release. The problem has nothing to do with Module[] or Block[] because the memory is allmost constant with the uncompiled w[XJ_,XX_,nj_Integer,ni_Integer]:= Block[{u},u=Table[Random[],{i,ni},{j,nj},{p,0,1}]; Table[Last[ Inverse[Sum[ Outer[Times,u[[i,j]],u[[i,j]]*(XX[[i]]-XJ[[j]])^2],{i,1, ni}]].Sum[ Table[u[[i,j]]*XX[[i]]*(XX[[i]]-XJ[[j]])^2],{i,1,ni}]],{j,nj}]] while the compiled w increments the memory by very huge values, and I assume the memory for the u array is not released. You can try it with u; w = Compile[{{XJ, _Real, 1}, {XX, _Real, 1}, {nj, _Integer}, {ni, _Integer}}, Block[{p}, u = Table[Random[], {i, ni}, {j, nj}, {p, 0, 1}]; Table[Last[ Inverse[Sum[ Outer[Times, u[[i, j]], u[[i, j]]*(XX[[i]] - XJ[[j]])^2], {i, 1, ni}]].Sum[ Table[u[[i, j]]*XX[[i]]*(XX[[i]] - XJ[[j]])^2], {i, 1, ni}]], {j, nj}]], {{u, _Real, 3}}] that does not change the memory *and* is significant faster than the original function with the local u. Regards Jens