Python leaks in cyclic garbage collection

Discussion in 'Python' started by moerchendiser2k3, Feb 19, 2011.

  1. Hi, I have some problems with Python and the garbage collection. In
    the following piece of code I create a simple gargabe collection but I
    am still wondering why the finalizers are never called - at least on
    exit of Py they should be called somehow. What do I miss here? I know,
    there is no deterministic way how to resolve this though.

    class Foo():

    def __init__(self):
    self.b=Bar(self)

    def __del__(self):
    print "Free Foo"

    class Bar():
    def __init__(self, f):
    self.f=f

    def __del__(self):
    print "Free Bar"

    f=Foo()
    print f
    moerchendiser2k3, Feb 19, 2011
    #1
    1. Advertising

  2. Thanks a lot for any help!!!

    Bye,
    moerchendiser2k3
    moerchendiser2k3, Feb 19, 2011
    #2
    1. Advertising

  3. moerchendiser2k3

    Chris Rebert Guest

    On Fri, Feb 18, 2011 at 8:10 PM, moerchendiser2k3
    <> wrote:
    > Hi, I have some problems with Python and the garbage collection. In
    > the following piece of code I create a simple gargabe collection but I
    > am still wondering why the finalizers are never called - at least on
    > exit of Py they should be called somehow. What do I miss here?

    <code snippet involving circularly-referenced objects with __del__
    methods snipped>

    Read The Fine Manual (all emphases added):

    http://docs.python.org/reference/datamodel.html#object.__del__ :
    "***It is not guaranteed that __del__() methods are called for objects
    that still exist when the interpreter exits.***"
    "Note: [...] x.__del__() — [...] is only called when x‘s reference
    count reaches zero. Some common situations that may prevent the
    reference count of an object from going to zero include: circular
    references between objects [...] Circular references which are garbage
    are detected when the option cycle detector is enabled (it’s on by
    default), ***but can only be cleaned up if there are no Python-level
    __del__() methods involved***. Refer to the documentation for the `gc`
    module for more information about how __del__() methods are handled by
    the cycle detector, particularly the description of the `garbage`
    value."

    And following the pointer to gc.garbage's docs:
    http://docs.python.org/library/gc.html#gc.garbage :
    "Objects that have __del__() methods and are part of a reference cycle
    ***cause the entire reference cycle to be uncollectable*** [...]
    Python doesn’t collect such cycles automatically because, in general,
    it isn’t possible for Python to guess a safe order in which to run the
    __del__() methods. [...] It’s generally better to avoid the issue by
    not creating cycles containing objects with __del__() methods"

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Feb 19, 2011
    #3
  4. On Fri, 18 Feb 2011 20:49:09 -0800, Chris Rebert wrote:

    > And following the pointer to gc.garbage's docs:
    > http://docs.python.org/library/gc.html#gc.garbage : "Objects that have
    > __del__() methods and are part of a reference cycle ***cause the entire
    > reference cycle to be uncollectable*** [...] Python doesn’t collect such
    > cycles automatically because, in general, it isn’t possible for Python
    > to guess a safe order in which to run the __del__() methods. [...] It’s
    > generally better to avoid the issue by not creating cycles containing
    > objects with __del__() methods"


    Another solution is to manually break the cycle, perhaps by providing a
    "close" method, and requiring the caller to use it.


    --
    Steven
    Steven D'Aprano, Feb 19, 2011
    #4
  5. Il giorno 19/feb/2011, alle ore 05.10, moerchendiser2k3 ha scritto:

    > Hi, I have some problems with Python and the garbage collection. In
    > the following piece of code I create a simple gargabe collection but I
    > am still wondering why the finalizers are never called - at least on
    > exit of Py they should be called somehow. What do I miss here? I know,
    > there is no deterministic way how to resolve this though.
    >
    > class Foo():
    >
    > def __init__(self):
    > self.b=Bar(self)
    >
    > def __del__(self):
    > print "Free Foo"
    >
    > class Bar():
    > def __init__(self, f):
    > self.f=f
    >
    > def __del__(self):
    > print "Free Bar"
    >
    > f=Foo()
    > print f
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    Wild guess:
    maybe when python exits they are called but sys.stdout has already been closed and nothing gets written on it anymore.
    Andrea Crotti, Feb 19, 2011
    #5
  6. Thanks for your answers! They really helped me out!! :)
    moerchendiser2k3, Feb 19, 2011
    #6
  7. moerchendiser2k3

    n00m Guest

    > Wild guess:
    > maybe when python exits they are called but sys.stdout has already been closed and nothing gets written on it anymore.


    Certainly NOT.


    class Foo():
    def __init__(self):
    self.b = Bar(self)
    def __del__(self):
    print "Free Foo"

    class Bar():
    def __init__(self, f):
    self.f = f
    def __del__(self):
    print "Free Bar"


    f = Foo()
    print f.b.f.b.f.b.f.b.f.b.f.b.f.b.f.b.f
    print f.b.f.b.f.b.f.b.f.b.f.b.f.b.f.b.f.b



    *********Console output:***********

    <__main__.Foo instance at 0x00AF7328>
    <__main__.Bar instance at 0x00AF7350>

    D:\v3>
    n00m, Feb 21, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Mailund

    Cyclic garbage collection and segfaults...

    Thomas Mailund, Jan 14, 2004, in forum: Python
    Replies:
    3
    Views:
    306
    Thomas Mailund
    Jan 15, 2004
  2. Joe Peterson
    Replies:
    1
    Views:
    290
    Klaas
    Dec 2, 2006
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    946
    Øyvind Isaksen
    May 18, 2007
  4. Aaron Brady

    garbage collection / cyclic references

    Aaron Brady, Mar 21, 2009, in forum: Python
    Replies:
    9
    Views:
    305
    Aaron Brady
    Mar 22, 2009
  5. Tom Cole
    Replies:
    1
    Views:
    83
    Benjamin
    Mar 10, 2007
Loading...

Share This Page