Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1997
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Memory Leaks In Matrix Summation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg5863] Re: [mg5830] Re: [mg5822] Memory Leaks In Matrix Summation
  • From: Tom Wickham-Jones <twj>
  • Date: Sun, 2 Feb 1997 01:30:44 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

G. David Lange wrote:
> 
> >In summing a matrix I write over previous values
> >in a do loop.  However, as the resulting sample
> >shows, the memory continues to grow (as it is in a loop
> >it should, of course, write over itself).  Is this
> >a bug, and if so is there some means of avoiding it?
> >
> >
> >In[1]:=
> >xx=Array[Random[]+I Random[],{41,206}];
> >
> >In[2]:=
> >(* 206 x 206 Matrix of zeros *)
> >res0=Table[0,{i,1,206},{j,1,206}];
> >
> >In[3]:=
> >(* Generate Matrix 206 x 206 ten times & sum over loop *)
> >Do[
> >
> >xxk=xx[[k]];
> >Print["#: ",k];
> >res=Re[Outer[Times,Conjugate[#],#]&[xxk]];
> >res0=res0+res;
> >Print["Mem: ",N[MaxMemoryUsed[]/10^6]],
> >
> >{k,1,10}]
> >
> >#: 1
> >Mem: 10.9363
> >#: 2
> >Mem: 13.3135
> >#: 3
> >Mem: 14.332
> >#: 4
> >Mem: 15.5276
> >#: 5
> >Mem: 17.7384
> >#: 6
> >Mem: 19.9492
> >#: 7
> >Mem: 22.16
> >#: 8
> >Mem: 24.3708
> >#: 9
> >Mem: 26.5816
> >#: 10
> >Mem: 28.7924
> >
> Mathematica keeps an infinite memory of all "In" and "Out" regardless of the way you program your loops.  To defeat this use a command like
> $HistoryLength=1.
> The only penalty for this is that you cannot use % to refer to "Out"s farther back than your HistoryLength.


Actually the reason for the increase in memory usage each trip round the
Do loop is not a consequence of the main loop history mechanism.  The
history mechanism would only keep the result of evaluating In[3].  
The example here shows an increase in memory inside of the Do.

To see what is happening here it might be better to work with smaller
examples...  this would quickly show that the Array command is not
generating a matrix of complex numbers.  Array applies its first argument
to elements generated from the iterator arguments.



In[13]:= Array[f, {2,2}]

Out[13]= {{f[1, 1], f[1, 2]}, {f[2, 1], f[2, 2]}}

so in this example xx is not a matrix of numbers but a more
complicated symbolic quantity.

In[14]:= Array[ Random[]+I Random[], {2,2}]

Out[14]= {{(0.670193 + 0.623252 I)[1, 1], (0.670193 + 0.623252 I)[1, 2]}, 
 
>    {(0.670193 + 0.623252 I)[2, 1], (0.670193 + 0.623252 I)[2, 2]}}


Everything goes wrong after this,  the res0 result gets bigger and bigger
(as a general symbolic matrix) which is why memory usage increases.

The solution is to generate the random matrix with Table.

In[15]:= xx = Table[ Random[ Complex], {2},{2}]

Out[15]= {{0.820949 + 0.896121 I, 0.0745521 + 0.841437 I}, 
 
>    {0.52045 + 0.158571 I, 0.82055 + 0.117863 I}}



Tom Wickham-Jones
Wolfram Research.


  • Prev by Date: Restrict computations to Reals
  • Next by Date: MMA 2.2.3 For Sale
  • Previous by thread: Restrict computations to Reals
  • Next by thread: MMA 2.2.3 For Sale