473,626 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1891
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.loewi s.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.loewi s.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...@y ahoo.comwrote:
On Aug 30, 3:50 am, "Martin v. Löwis" <mar...@v.loewi s.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 "unreachabl e 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**********@y ahoo.comwrote:
On Aug 30, 12:36 pm, "Chris Mellon" <arka...@gmail. comwrote:
On 8/30/07, 7stud <bbxx789_0...@y ahoo.comwrote:
On Aug 30, 3:50 am, "Martin v. Löwis" <mar...@v.loewi s.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 "unreachabl e 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
2037
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
3032
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 the mainline. The class that instantiated the object in question is definitely still in existence at the point garbage collection swoops in and yanks it out from under my processing. Is there a way to ensure an instantiated object cannot be freed...
28
3163
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 this work be library based or language based and will it be based on that of managed C++? Then of course there are the finer technical questions raised (especially due to pointer abuse). Is a GC for C++ just a pipe dream or is there a lot of work...
56
3656
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 = null; Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null; The Constructor: public OutlookObject()
158
7789
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 discouraged due to some specific reason. If someone can give inputs on the same, it will be of great help. Regards, Pushpa
0
8272
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8644
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7206
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.