[Numpy-tickets] [NumPy] #394: Memory leak in .argsort() method

NumPy numpy-tickets at scipy.net
Mon Dec 4 11:39:22 CST 2006

#394: Memory leak in .argsort() method
 Reporter:  faltet  |       Owner:  somebody
     Type:  defect  |      Status:  new     
 Priority:  normal  |   Milestone:          
Component:  Other   |     Version:  devel   
 Severity:  normal  |    Keywords:          
 It seems that a memory leak in recent !NumPy 1.0.1 has slipped in.

 The next code snipped reproduces the problem:

 from numpy import *

 # Toggle to this for valgrind

 for i in xrange(N):
     a = arange(M)
     b = a.argsort()


 and see evolution of memory consumption with an appropriate tool.

 Running the above against valgrind, I've got:


 ==10942== 4,480 (400 direct, 4,080 indirect) bytes in 10 blocks are
 definitely l
 ost in loss record 36 of 44
 ==10942==    at 0x401C38B: malloc (vg_replace_malloc.c:149)
 ==10942==    by 0x45A414C: array_alloc (arrayobject.c:6671)
 ==10942==    by 0x45A075B: PyArray_NewFromDescr (arrayobject.c:5323)
 ==10942==    by 0x45C706D: PyArray_ArangeObj (multiarraymodule.c:6476)
 ==10942==    by 0x45C738E: array_arange (multiarraymodule.c:6532)
 ==10942==    by 0x80B954A: PyEval_EvalFrame (in /usr/bin/python2.4)
 ==10942==    by 0x80BA6B4: PyEval_EvalCodeEx (in /usr/bin/python2.4)
 ==10942==    by 0x80BA718: PyEval_EvalCode (in /usr/bin/python2.4)
 ==10942==    by 0x80DD046: PyRun_FileExFlags (in /usr/bin/python2.4)
 ==10942==    by 0x80DD243: PyRun_SimpleFileExFlags (in /usr/bin/python2.4)
 ==10942==    by 0x8055BA7: Py_Main (in /usr/bin/python2.4)
 ==10942==    by 0x8055031: main (in /usr/bin/python2.4)


 So, it seems that the arrays created by .arange() method don't got freed.

 An inspection of the latests commits in trunk pointed directly to the
 r3466 changeset. Below is a fix:

 Index: numpy/core/src/multiarraymodule.c
 --- numpy/core/src/multiarraymodule.c   (revision 3466)
 +++ numpy/core/src/multiarraymodule.c   (working copy)
 @@ -2490,6 +2490,7 @@

         /* Determine if we should use new algorithm or not */
         if (op2->descr->f->argsort[which] != NULL) {
 +               Py_DECREF(op);
                 return _new_argsort(op2, axis, which);


 This patch seems to behave well with both !NumPy and !PyTables tests.

Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/394>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.

More information about the Numpy-tickets mailing list