Re: Compile + Module => Memory Leak
- To: mathgroup at smc.vnet.net
- Subject: [mg39983] Re: Compile + Module => Memory Leak
- From: atelesforos at hotmail.com (Orestis Vantzos)
- Date: Thu, 13 Mar 2003 03:05:40 -0500 (EST)
- References: <b4k3ls$8ek$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
My bet is that your memory is used up to store the Out[..] expressions. Try: $HistoryLength=0; and then: MemoryInUse[] memorySoaker[...] MemoryInUse[] MemoryInUse[] (*Yes, twice...despite $HistoryLength=0 Mathematica still stores the previous expression*) I must note that there are no new symbols created inside Compile.. Moral lesson: when looking for memory leaks, the storage of Out[..] is the usual suspect. Orestis Bern <bgress at mail.DOTucr.DOT.edu> wrote in message news:<b4k3ls$8ek$1 at smc.vnet.net>... > Hello Mathgroup, > > (Paste the following functions into a Notebook:) > > Here is how Compile and Module conspire to leak memory: > > w = Compile[{{XJ, _Real, 1}, {XX, _Real, 1}, > {nj, _Integer, 0}, {ni, _Integer, 0}}, > Module[{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}]]] > > This demonstrates that 'w' is completely Compiled: > > > w[[-2]] > > > Here is some test data: > > > xi=Range[300];xj=Table[Random[],{300}]; > memorySoaker[xi_,xj_,n_]:=Module[{n1=Length[xi],n2=Length[xj]},Do[ > Print[ToString[MemoryInUse[]]];w[xi,xj,n1,n2];dummy;Print[ToString[\ > MemoryInUse[]]];dummy,{n}]] > > > Presumably, upon exit, a Module should release any memory used, no? > > > MemoryInUse[] > memorySoaker[xi,xj,20] > MemoryInUse[] > > > You will notice that memorySoaker is of the same structure as the > function, RP from COMBINATORICA.M, the DiscreteMath StandardPackage > included in Mathematica. RP is called by RandomPermutation which is, in > turn, called by RandomKSubset, which is a function I complained about > leaking memory many months ago. I am guessing that this is the source > of the problem, something to do with the use of a local variable defined > in a Module within a Compile statement. > > > RP=Compile[{{n,_Integer}},Module[{p=Range[n],i,x,t}, > Do[x=Random[Integer,{1,i}]; > t=p[[i]];p[[i]]=p[[x]];p[[x]]=t,{i,n,2,-1}]; > p],{{Range[_],_Integer,1},{Random[___],_Integer}}] > > > Regards, > > Bernard