Negative array indicies and slice()

Ian Kelly ian.g.kelly at gmail.com
Tue Oct 30 04:46:46 EDT 2012


On Tue, Oct 30, 2012 at 1:21 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> I'm not entirely certain why collection objects get this special
> treatment, but there you have it.

Thinking about it some more, this makes sense.  The GC header is there
to support garbage collection for the object.  Atomic types like ints
do not need this header because they do not reference other objects
and so cannot be involved in reference cycles.  For those types,
reference counting is sufficient.  For types like collections that do
reference other objects, garbage collection is needed.

Expanding on this, I suspect it is actually a bug that slice objects
are not tracked by the garbage collector.  The following code appears
to result in a memory leak:

import gc
gc.disable()
while True:
    for i in range(100):
        l = []
        s = slice(l)
        l.append(s)
        del s, l
    _ = gc.collect()

Try running that and watch your Python memory usage climb and climb.
For contrast, replace the slice with a list and observe that memory
usage does *not* climb.  On each iteration, the code constructs a
reference cycle between a slice and a list.  It seems that because
slices are not tracked by the garbage collector, it is unable to break
these cycles.



More information about the Python-list mailing list