Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python leaks in cyclic garbage collection

Reply
Thread Tools

Python leaks in cyclic garbage collection

 
 
moerchendiser2k3
Guest
Posts: n/a
 
      02-19-2011
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
 
Reply With Quote
 
 
 
 
moerchendiser2k3
Guest
Posts: n/a
 
      02-19-2011
Thanks a lot for any help!!!

Bye,
moerchendiser2k3
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-19-2011
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/dat...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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-19-2011
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
 
Reply With Quote
 
Andrea Crotti
Guest
Posts: n/a
 
      02-19-2011

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.

 
Reply With Quote
 
moerchendiser2k3
Guest
Posts: n/a
 
      02-19-2011
Thanks for your answers! They really helped me out!!
 
Reply With Quote
 
n00m
Guest
Posts: n/a
 
      02-21-2011
> 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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
garbage collection / cyclic references Aaron Brady Python 9 03-22-2009 02:33 PM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Checking for Memory Leaks and Garbage Collection Tom Cole Javascript 1 03-10-2007 03:29 AM
Possible to assure no "cyclic"/"uncollectible" memory leaks? Joe Peterson Python 1 12-02-2006 11:39 PM
Cyclic garbage collection and segfaults... Thomas Mailund Python 3 01-15-2004 02:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57