gc.garbage

7

7stud

gc.garbage returns an empty list even though the command:

gc.set_debug(gc.DEBUG_LEAK)

produces the following output:

gc: uncollectable <Dog 0x56e10>
gc: uncollectable <Cat 0x56e30>
gc: uncollectable <dict 0x58270>
gc: uncollectable <dict 0x43e40>

I expected all those objects to be in the list returned by
gc.garbage. Here's the code:

import gc

class Cat(object):
def __del__():
pass

class Dog(object):
def __del__():
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)
print gc.garbage

--output:--
[]
gc: uncollectable <Dog 0x56e10>
gc: uncollectable <Cat 0x56e30>
gc: uncollectable <dict 0x58270>
gc: uncollectable <dict 0x43e40>
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

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
 
7

7stud

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>}]
 
7

7stud

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. I had a cut and paste error in my reply, so
here it is again with the corrections...

Now, if I run the code:

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

class Cat(object):
def __del__():
pass

class Dog(object):
def __del__():
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(same addresses)? Also, I haven't bound the names "cat" or
"dog" anywhere in my program. What do those names mean in the list?

Doing some further testing, if I eliminate the __del__ methods:

-----------
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: 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>}]
 
C

Chris Mellon

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. I had a cut and paste error in my reply, so
here it is again with the corrections...

Now, if I run the code:

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

class Cat(object):
def __del__():
pass

class Dog(object):
def __del__():
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(same addresses)? Also, I haven't bound the names "cat" or
"dog" anywhere in my program. What do those names mean in the list?
Read your output carefully!

gc.garbage is a list of objects. The objects are printed just as they
would be anywhere else in Python. You've got the dog object, the cat
object, and the __dict__ of each instance.
Doing some further testing, if I eliminate the __del__ methods:

-----------
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: 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>}]
From the last line of the documentation block you quoted above: "If
DEBUG_SAVEALL is set, then all unreachable objects will be added to
this list rather than freed."
 
7

7stud

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

Thanks for the response. I had a cut and paste error in my reply, so
here it is again with the corrections...
Now, if I run the code:
class Cat(object):
def __del__():
pass
class Dog(object):
def __del__():
pass
def some_func():
the_dog = Dog()
the_cat = Cat()
the_dog.cat = the_cat
the_cat.dog = the_dog

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(same addresses)? Also, I haven't bound the names "cat" or
"dog" anywhere in my program. What do those names mean in the list?

Read your output carefully!

gc.garbage is a list of objects. The objects are printed just as they
would be anywhere else in Python. You've got the dog object, the cat
object, and the __dict__ of each instance.

Ah. I missed the braces inside the list, but I still don't understand
where the names "cat" and "dog" come from.

DEBUG_SAVEALL is set, then all unreachable objects will be added to
this list rather than freed."

What is the definition of an "unreachable object"? If I add the
following:
 
C

Chris Mellon

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.


Thanks for the response. I had a cut and paste error in my reply, so
here it is again with the corrections...
Now, if I run the code:
class Cat(object):
def __del__():
pass
class Dog(object):
def __del__():
pass
def some_func():
the_dog = Dog()
the_cat = Cat()
the_dog.cat = the_cat
the_cat.dog = the_dog

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(same addresses)? Also, I haven't bound the names "cat" or
"dog" anywhere in my program. What do those names mean in the list?

Read your output carefully!

gc.garbage is a list of objects. The objects are printed just as they
would be anywhere else in Python. You've got the dog object, the cat
object, and the __dict__ of each instance.

Ah. I missed the braces inside the list, but I still don't understand
where the names "cat" and "dog" come from.

What happens when you print a dictionary?
What is the definition of an "unreachable object"? If I add the
following:

Anything that the gc would have collected.

Because it's already dead. In your previous code, you made dog and cat
unreachable by virtue of making them a refcycle.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top