help-octave
[Top][All Lists]

Re: octave memory allocation problem ?

 From: John Eaton Subject: Re: octave memory allocation problem ? Date: Fri, 28 Apr 95 22:18:32 EDT

```address@hidden (Ludger Leushacke) wrote:

: There seems to be a memory problem within octave.
: Octave claims more and more memory, even if no new variables are created!

: Main problem: memory of old variables doesn't get reused. For each assignment
: new memory is allocated, the old mem never get free.

I don't think what you describe below is actually a memory leak.

: Example:
: silly m-function halfs the input values:
: ----------------------------
: function h=half(x)
:
: h=x*0.5;
: ----------------------------
:
: Starting octave and looking at the program size (with ps -l) gives:
: \$ octave
:
:  F    UID   PID  PPID PRI NI SIZE  RSS WCHAN      STAT TTY   TIME COMMAND
:   0   127  2930   164   1  0 2679 1088 189c1f     S    pp2   0:00 octave
:
: octave:1> x=rand(300);
:
:   0   127  2930   164   9  0 3383 1840 189c1f     S    pp2   0:01 octave

Octave has grown by ~700k (300*300*8 bytes for the random matrix + a
few bytes for parsing and evaluating the command).

: octave:2> y1=half(x);
:
:   0   127  2930   164   7  0 4131 2588 189c1f     S    pp2   0:01 octave

This takes another 700k to create the new matrix y1.

: octave:3> y2=half(x);
:
:   0   127  2930   164   7  0 4835 3292 189c1f     S    pp2   0:01 octave

Likewise, to create y2.

: octave:4> y1=half(y2);
:
:   0   127  2930   164   9  0 5539 3996 189c1f     S    pp2   0:02 octave
:
: The last statement (4) uses existing variables and therefore the
: size shoudn't increase.

This would be true if Octave freed the memory in y1 before evaluating
half (y2), but that's not the way things work.  Instead, the right
hand side is evaluated (which is in this case causes Octave to grow
because there is no suitable chunk of memory available to reuse) and
then it is assigned to y1.  After that assignment happens, there will
be a block of memory available, so another statement like

y1 = rand (300);

or

y1 = half (x);

should not cause Octave to grow.  However, something like

y1 = rand (400);

might (and probably will, in most cases) cause Octave to grow because
malloc might not be able to reuse the old 700k chunk of memory that's
available.

Octave evaluates the right hand side first and then assigns the value
to the left hand side so that the value on the left hand side is
preserved if an error occurs during the evaluation of the right hand
side.  I like this feature, but if there is some good reason for not
doing this, I will consider changing it.

jwe

```