cpython/Objects
Guido van Rossum 5ce78f8e4e Patch by Charles G Waldman to avoid a sneaky memory leak in
_PyTuple_Resize().  In addition, a change suggested by Jeremy Hylton
to limit the size of the free lists is also merged into this patch.

Charles wrote initially:

"""
Test Case:  run the following code:

class Nothing:
    def __len__(self):
        return 5
    def __getitem__(self, i):
        if i < 3:
            return i
        else:
            raise IndexError, i

def g(a,*b,**c):
    return

for x in xrange(1000000):
    g(*Nothing())


and watch Python's memory use go up and up.


Diagnosis:

The analysis begins with the call to PySequence_Tuple at line 1641 in
ceval.c - the argument to g is seen to be a sequence but not a tuple,
so it needs to be converted from an abstract sequence to a concrete
tuple.  PySequence_Tuple starts off by creating a new tuple of length
5 (line 1122 in abstract.c).  Then at line 1149, since only 3 elements
were assigned, _PyTuple_Resize is called to make the 5-tuple into a
3-tuple.  When we're all done the 3-tuple is decrefed, but rather than
being freed it is placed on the free_tuples cache.

The basic problem is that the 3-tuples are being added to the cache
but never picked up again, since _PyTuple_Resize doesn't make use of
the free_tuples cache.  If you are resizing a 5-tuple to a 3-tuple and
there is already a 3-tuple in free_tuples[3], instead of using this
tuple, _PyTuple_Resize will realloc the 5-tuple to a 3-tuple.  It
would more efficient to use the existing 3-tuple and cache the
5-tuple.

By making _PyTuple_Resize aware of the free_tuples (just as
PyTuple_New), we not only save a few calls to realloc, but also
prevent this misbehavior whereby tuples are being added to the
free_tuples list but never properly "recycled".
"""

And later:

"""
This patch replaces my submission of Sun, 16 Apr and addresses Jeremy
Hylton's suggestions that we also limit the size of the free tuple
list.  I chose 2000 as the maximum number of tuples of any particular
size to save.

There was also a problem with the previous version of this patch
causing a core dump if Python was built with Py_TRACE_REFS.  This is
fixed in the below version of the patch, which uses tupledealloc
instead of _Py_Dealloc.
"""
2000-04-21 21:15:05 +00:00
..
Makefile.in Marc-AAndre Lemburg: add new unicode files 2000-03-10 22:55:40 +00:00
abstract.c Marc-Andre's third try at this bulk patch seems to work (except that 2000-04-05 20:11:21 +00:00
bufferobject.c PyBuffer_New(): Raise ValueError if size is negative (the other 1999-08-04 13:08:19 +00:00
classobject.c Mark Hammond: 2000-04-10 13:03:19 +00:00
cobject.c New CObject from Jim Fulton, adds PyCObject_FromVoidPtrAndDesc() and 1997-10-21 19:48:35 +00:00
complexobject.c Massive patch by Skip Montanaro to add ":name" to as many 2000-02-29 13:59:29 +00:00
dictobject.c Add PyDict_Copy() function to C API for dicts. It returns a new 2000-03-30 22:27:31 +00:00
fileobject.c Checking in the new, improve file.writelines() code. 2000-03-13 16:27:06 +00:00
floatobject.c Marc-Andre's third try at this bulk patch seems to work (except that 2000-04-05 20:11:21 +00:00
frameobject.c Christian Tismer's "trashcan" patch: 2000-03-13 16:01:29 +00:00
funcobject.c Make function objects somewhat mutable -- the members func_code, 1998-05-22 00:55:34 +00:00
intobject.c Marc-Andre's third try at this bulk patch seems to work (except that 2000-04-05 20:11:21 +00:00
listobject.c Christian Tismer's "trashcan" patch: 2000-03-13 16:01:29 +00:00
longobject.c Simple optimization by Christian Tismer, who gives credit to Lenny 2000-04-10 17:31:58 +00:00
methodobject.c The rest of the changes by Trent Mick and Dale Nagata for warning-free 2000-01-20 22:32:56 +00:00
moduleobject.c Patch by Chris Petrilli to display the origin of a module in its 1999-02-15 14:47:16 +00:00
object.c Fix PR#7 comparisons of recursive objects 2000-04-14 19:13:24 +00:00
rangeobject.c Use 'xrange', not 'range', in error messages. Reported by Nathan Sullivan. 1999-01-09 21:40:35 +00:00
sliceobject.c Ellipses -> Ellipsis rename (the dictionary really says that it should 1996-10-11 16:25:41 +00:00
stringobject.c Marc-Andre Lemburg: 2000-04-11 15:39:26 +00:00
tupleobject.c Patch by Charles G Waldman to avoid a sneaky memory leak in 2000-04-21 21:15:05 +00:00
typeobject.c American spelling in doc string. 1997-06-02 14:43:07 +00:00
unicodectype.c Marc-Andre Lemburg: 2000-04-11 15:39:02 +00:00
unicodeobject.c Marc-Andre Lemburg: 2000-04-11 15:38:46 +00:00
xxobject.c Correct typo (Py_MethodDef doesn't exist). Reported by Uwe Zessin. 1999-01-29 14:39:12 +00:00