473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

gc.garbage

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>

Aug 30 '07 #1
5 1885
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
Aug 30 '07 #2
On Aug 30, 3:50 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
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.
----
Aug 30 '07 #3
On Aug 30, 3:50 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
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>}]
-----

The docs say:

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

Since debugging doesn't show any uncollectable objects, why isn't
gc.garbage empty? The docs also say:

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

Does set_debug() change the default?

Aug 30 '07 #4
On Aug 30, 12:36 pm, "Chris Mellon" <arka...@gmail.comwrote:
On 8/30/07, 7stud <bbxx789_0...@yahoo.comwrote:
On Aug 30, 3:50 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
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.

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:

-----
y = Cat()
del y
-----

That object doesn't make it into gc.garbage.

Aug 30 '07 #5
On 8/30/07, 7stud <bb**********@yahoo.comwrote:
On Aug 30, 12:36 pm, "Chris Mellon" <arka...@gmail.comwrote:
On 8/30/07, 7stud <bbxx789_0...@yahoo.comwrote:
On Aug 30, 3:50 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
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.

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?
>
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:
Anything that the gc would have collected.
-----
y = Cat()
del y
-----

That object doesn't make it into gc.garbage.
Because it's already dead. In your previous code, you made dog and cat
unreachable by virtue of making them a refcycle.
>

--
http://mail.python.org/mailman/listinfo/python-list
Aug 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: pachanga | last post by:
The Hans-Boehm garbage collector can be successfully used with C and C++, but not yet a standard for C++.. Is there talks about Garbage Collector to become in the C++ standard?
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.