[Numpy-discussion] Memory leak with matrices?

Robert Kern robert.kern at gmail.com
Tue Mar 9 13:28:27 EST 2010

On Tue, Mar 9, 2010 at 11:31, David Paul Reichert
<D.P.Reichert at sms.ed.ac.uk> wrote:
> Hi,
> I've got two issues:
> First, the following seems to cause a memory leak,
> using numpy 1.3.0:
> a = matrix(ones(1))
> while True:
>    a += 0
> This only seems to happen when a is a matrix rather
> than an array, and when the short hand '+=' is used.

Yes, I can verify that there is a leak in the current SVN, too. It
appears that we are creating dictionaries with the value {'_getitem':
False} and never decrefing them. The matrix object itself owns the

> Second, I'm not sure whether that's a bug or whether
> I just don't understand what's going on, but when a is a column
> array, (e.g. a = ones((10, 1))), then
> a -= a[0,:]
> only subtracts from a[0, 0], whereas not using the short hand
> or using something else than a on the righthand side seems
> to subtract from all rows as expected.

a[0,:] creates a view onto the same memory of the original array.
Since you modify the values in-place, a[0,0] gets set to
a[0,0]-a[0,0]==0, then a[1,0] gets set to a[1,0] - a[0,0] == a[1,0] -
0 == a[1,0], etc. Try this instead:

a -= a[0,:].copy()

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list