[Python-Dev] funny leak

Neal Norwitz neal@metaslash.com
Mon, 24 Mar 2003 08:43:16 -0500


On Mon, Mar 24, 2003 at 01:40:44PM +0100, Christian Tismer wrote:
> 
> I just tested generators and found a memory leak.
> (Has nothing to do with generators).
> The following code adds one to the overall refcount
> and gc cannot reclaim it.
> 
> def conjoin(gs):
>     def gen():
>         gs      # unbreakable cycle
>         gen     # unless one is commented out

With current CVS:

        >>> gc.collect()
        0
        [23150 refs]
        >>> conjoin(1)
        [23160 refs]
        >>> conjoin(1)
        [23170 refs]
        >>> gc.collect()
        8
        [23151 refs]
        >>> conjoin(1)
        [23161 refs]
        >>> conjoin(1)
        [23171 refs]
        >>> gc.collect()
        8
        [23151 refs]
        >>> conjoin(1)
        [23161 refs]
        >>> conjoin(1)
        [23171 refs]
        >>> gc.collect()
        8
        [23151 refs]

One ref may be leaked the first time gc.collect() is called with
garbage (23150 -> 23151).  But after that, no more refs are leaked
(ref count stays at 23151).

Neal