# [Numpy-discussion] Memory leak with matrices?

josef.pktd@gmai... josef.pktd@gmai...
Tue Mar 9 11:55:16 CST 2010

```On Tue, Mar 9, 2010 at 12:31 PM, David Paul Reichert
<D.P.Reichert@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.
>
> 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.

this is because a[0,0] is set to zero after the first inplace
subtraction, then zero is subtracted from all other rows

>>> a = np.ones((10, 1))
>>> a
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
>>> a += a[0,:]
>>> a
array([[ 2.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.]])
>>> a -= a[0,:]
>>> a
array([[ 0.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.],
[ 3.]])

Josef

>
> Thanks a lot,
>
> David
>
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
```