[NumPy-Tickets] [NumPy] #1548: matrix unbounded memory leak using +=

NumPy Trac numpy-tickets@scipy....
Wed Jul 28 18:24:32 CDT 2010


#1548: matrix unbounded memory leak using +=
--------------------------------+-------------------------------------------
 Reporter:  cardinal            |       Owner:  somebody   
     Type:  defect              |      Status:  new        
 Priority:  high                |   Milestone:  Unscheduled
Component:  numpy.core          |     Version:  1.2.1      
 Keywords:  matrix memory leak  |  
--------------------------------+-------------------------------------------

Comment(by pv):

 In 1.4.x and SVN trunk it's the `__array_prepare__` stuff in
 `ufunc_object.c:1497` that's "leaking" memory. Comment it out, and the
 leak disappears.

 (In earlier versions of Numpy there were other bugs that could also cause
 memory leaks with subclasses -- those have been fixed.)

 This is actually not a real memory leak, but an infinite chain of views
 being created. Note:
 {{{
 import numpy as np

 niter = 2000000
 X = np.matrix(np.zeros(4), float).reshape(2,2)

 def baselen(a):
     n = 0
     while a.base is not None:
         a = a.base
         n += 1
     return n

 for iter in range(niter):
     X = np.add(X, X, X)
     print id(X), baselen(X)
 }}}
 and observe `baselen` approaching infinity. This is the same sort of stuff
 as in #466, except that `matrix` objects are a bit bulkier and take more
 memory.

 Anyway, the correct fix is to *not* call `__array_prepare__` for output
 arguments that are passed in as the out= parameter to the ufunc. (Not sure
 how to determine this ATM that late in the code.)

     ***

 Also, why does `ndarray` have its own `__array_prepare__` implemented? I
 fail to see the purpose for that, since it only creates a view.

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1548#comment:3>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list