Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Python leaks in cyclic garbage collection (http://www.velocityreviews.com/forums/t743842-python-leaks-in-cyclic-garbage-collection.html)

moerchendiser2k3 02-19-2011 04:10 AM

Python leaks in cyclic garbage collection
 
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 02-19-2011 04:13 AM

Re: Python leaks in cyclic garbage collection
 
Thanks a lot for any help!!!

Bye,
moerchendiser2k3

Chris Rebert 02-19-2011 04:49 AM

Re: Python leaks in cyclic garbage collection
 
On Fri, Feb 18, 2011 at 8:10 PM, moerchendiser2k3
<googler.1.webmaster@spamgourmet.com> 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

Steven D'Aprano 02-19-2011 05:40 AM

Re: Python leaks in cyclic garbage collection
 
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

Andrea Crotti 02-19-2011 08:40 AM

Re: Python leaks in cyclic garbage collection
 

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.


moerchendiser2k3 02-19-2011 11:32 AM

Re: Python leaks in cyclic garbage collection
 
Thanks for your answers! They really helped me out!! :)

n00m 02-21-2011 09:10 AM

Re: Python leaks in cyclic garbage collection
 
> 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>



All times are GMT. The time now is 11:27 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.