       Compile + Module => Memory Leak

• To: mathgroup at smc.vnet.net
• Subject: [mg39910] Compile + Module => Memory Leak
• From: Bern <bgress at mail.DOTucr.DOT.edu>
• Date: Tue, 11 Mar 2003 02:37:18 -0500 (EST)
• Organization: University of California, Riverside
• Sender: owner-wri-mathgroup at wolfram.com

```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;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

```

• Prev by Date: Re: Running ssh in Mathematica
• Next by Date: SetPrecision & Plot bug?
• Previous by thread: Re: general solution for element of series
• Next by thread: Re: Compile + Module => Memory Leak