[Matrix-SIG] Numeric semantics question....

Konrad Hinsen hinsen@cnrs-orleans.fr
Wed, 23 Jun 1999 19:02:48 +0200


> I wanted to confirm that if I assign a subsequence of an array object, the
> entire array is kept in memory. That is, if I wrote some code like this:
> 
[code deleted]
> 
> Numeric would try to allocate memory for 500 million floats, right? 

Yes.

> Does making the following change correctly get around the problem?
> 
> def memory_waster():
>     x = Numeric.zeros((1000000,1), Numeric.Float)
>     return copy.copy(x[1])

Yes. Internally, an array object is actually two objects: an array
data space object and an array reference object (the first one is not
strictly speaking a Python object, but it works in the same way).
Indexing creates a new reference object that points to the same data
object, and the data object is only released when there is no more
reference object pointing to it. That's why no memory is released in
the first example. When you make a copy, a new data object is created,
so the first one can be released.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                            | E-Mail: hinsen@cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.55.69
Rue Charles Sadron                       | Fax:  +33-2.38.63.15.17
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais
-------------------------------------------------------------------------------