help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Memory management in .oct files


From: John W. Eaton
Subject: Memory management in .oct files
Date: Fri, 18 Mar 2005 10:00:40 -0500

On 16-Mar-2005, Richard Hindmarsh <address@hidden> wrote:

| If, in Octave, I do 
| 
| %Step 1
| a = rand(3000,3000);
| 
| %and then 
| % Step 2
| b = a;
| 
| the memory used does not increase hardly at all upon exectuation of
| Step 2 as presumably Octave only creates new memory b's data when it
| is written to.

Correct, the "Step 2" operation simply creates a new name for the same
data and increments a reference count.

| However, it seems that when I run the matrixdemo.cc example in
| http://perso.wanadoo.fr/prthomas/intro.html#matrixdemo
| at some stage (perhaps the copy constructor #2, or is it the return
| statement #7) a third matrix is created in memory.
| 
| Could someone kindly inform me as to which of the two steps it is
| and how I can avoid creating unnecessary copies?

| #include <octave/oct.h>
| DEFUN_DLD (matrixdemo, args, ,
| "My first matrixdemo \n\n\
| transpose_of_x=matrixdemo(x) \n") {
|   octave_value retval;
|   if ( args.length() !=  1 ) {                             // #1 
|     error("this version of matrixdemo only takes one argument");
|     return retval;
|   }
|   Matrix xin1( args(0).matrix_value() );                   // #2

The copy constructor for the Matrix class increments the reference
count to the underlying data here, so the reference count is 2.

|   int ir = xin1.rows();                                    // #3
|   int ic = xin1.cols();
|   Matrix xout1(ic,ir);                                     // #4
|   for (int ir1 = 0 ; ir1 < ir ; ir1++)                     // #5
|     for (int ic1 = 0 ; ic1 < ic ; ic1++) {
|       xout1(ic1,ir1) = xin1(ir1,ic1);                      // #6

                         ^^^^^^^^^^^^^
Since Octave does not know that this indexing operation appears on the
RHS of the assignment (I think it would be a real mess to try to do
that automatically), it splits off a copy here.  If you don't plan on
modifying the argument that is pass to this function, then you can
avoid the extra copy and the checks on the reference count that are
done each time you use the indexing operator by declaring xin1 as
"const".

| Another useful piece of information would be the Octave equivalent
| of (cout,endl) <-> (octave_stdout,?) so that I can
| be sure that writes to the terminal correspond to the changes in
| memory usage I see.

Does the function flush_octave_stdout, declared in pager.h do what you
want?

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]