[Numpy-discussion] Numeric memory leak when building Numeric.array from numarray.array

Francesc Altet faltet at carabos.com
Thu Dec 7 10:36:22 CST 2006

El dj 07 de 12 del 2006 a les 16:50 +0100, en/na Alexandre Fayolle va
> Hi,
> I'm facing a memory leak on an application that has to use numarray and
> Numeric (because of external dependencies). 
> The problem occurs when building a Numeric array from a numarray array:
> import Numeric
> import numarray
> import sys
> atest = numarray.arange(200)
> temp =  Numeric.array(atest)
> print sys.getrefcount(atest) # prints 3
> print sys.getrefcount(temp)  # prints 2
> I'm running numarray 1.5.2 and Numeric 24.2

Yeah, it seems like the array protocol implementation in Numeric is
leaking. Unfortunately, as Numeric maintenance has been dropped, there
is small chances that this would be fixed in the future.

> I can work around this by using an intermediate string representation:
> temp = Numeric.fromstring(atest.tostring(), atest.typecode())
> temp.shape = atest.shape

Another (faster) workaround would be:

temp2 = Numeric.fromstring(atest._data, typecode=atest.typecode())

which is pretty fast:

In [20]:Timer("Numeric.fromstring(atest._data,
typecode=atest.typecode())", "import numarray, Numeric;
Out[20]:[0.18092107772827148, 0.13870906829833984, 0.13995194435119629]

i.e. more than 2x faster than your current solution:

In [21]:Timer("Numeric.fromstring(atest.tostring(),
typecode=atest.typecode())", "import numarray, Numeric;
Out[21]:[0.37756705284118652, 0.32852792739868164, 0.32704305648803711]

and similar in speed to the native .array() and .asarray() based on the
array protocol:

In [22]:Timer("Numeric.array(atest)", "import numarray, Numeric;
Out[22]:[0.17277789115905762, 0.12470793724060059, 0.12530016899108887]

In [23]:Timer("Numeric.asarray(atest)", "import numarray, Numeric;
Out[23]:[0.20457005500793457, 0.15211081504821777, 0.15212082862854004]

As an aside, and curiously enough, Numeric.array() (a copy is done) is
faster than Numeric.asarray() (a copy shouldn't be done) :-/


Francesc Altet    |  Be careful about using the following code --
Carabos Coop. V.  |  I've only proven that it works, 
www.carabos.com   |  I haven't tested it. -- Donald Knuth

More information about the Numpy-discussion mailing list