gc.garbage

7stud bbxx789_05ss at yahoo.com
Thu Aug 30 14:13:14 EDT 2007


On Aug 30, 3:50 am, "Martin v. Löwis" <mar... at v.loewis.de> wrote:
> > gc.set_debug(gc.DEBUG_LEAK)
> > print gc.garbage
>
> > --output:--
> > []
> > gc: uncollectable <Dog 0x56e10>
> > gc: uncollectable <Cat 0x56e30>
> > gc: uncollectable <dict 0x58270>
> > gc: uncollectable <dict 0x43e40>
>
> gc.garbage is filled only after these messages
> are printed, not before. You need to add an explicit
> call to gc.collect() if you want to see what
> uncollectable garbage you have.
>
> Regards,
> Martin

Hi,

Thanks for the response.  Now, when I run the code:

------------
import gc

class Cat(object):
    pass

class Dog(object):
    pass

def some_func():
    the_dog = Dog()
    the_cat = Cat()
    the_dog.cat = the_cat
    the_cat.dog = the_dog

some_func()

gc.set_debug(gc.DEBUG_LEAK)
gc.collect()
print gc.garbage
----------------

I get this output:

-------------
gc: uncollectable <Dog 0x56e10>
gc: uncollectable <Cat 0x56e30>
gc: uncollectable <dict 0x58300>
gc: uncollectable <dict 0x43e40>
[<__main__.Dog object at 0x56e10>, <__main__.Cat object at 0x56e30>,
{'cat': <__main__.Cat object at 0x56e30>}, {'dog': <__main__.Dog
object at 0x56e10>}]
---------------

Why are there two entries in the list for each uncollectable object?
Also, I haven't bound the names "cat" or "dog" anywhere in my
program.  What do those names mean in the list?

Doing some more testing, if I remove the __del__ methods:

---------------
class Cat(object):
    pass

class Dog(object):
    pass

def some_func():
    the_dog = Dog()
    the_cat = Cat()
    the_dog.cat = the_cat
    the_cat.dog = the_dog

some_func()

gc.set_debug(gc.DEBUG_LEAK)
gc.collect()
print gc.garbage
-----------

I get this output:

-----------------
gc: collectable <Dog 0x56e10>
gc: collectable <Cat 0x56e30>
gc: collectable <dict 0x58270>
gc: collectable <dict 0x43e40>
[<__main__.Dog object at 0x56e10>, <__main__.Cat object at 0x56e30>,
{'cat': <__main__.Cat object at 0x56e30>}, {'dog': <__main__.Dog
object at 0x56e10>}]
----------------

Now the objects are marked as collectable.  The docs say:

----
garbage
A list of objects which the collector found to be unreachable but
could not be freed (uncollectable objects).
----

So, I expected gc.garbage to be empty.  The docs also say:

----
garbage
...By default, this list contains only objects with __del__() methods.
----





More information about the Python-list mailing list