What does gc.get_objects() return?

Terry Reedy tjreedy at udel.edu
Thu Mar 13 03:54:55 CET 2014

On 3/12/2014 3:34 PM, Jurko Gospodnetić wrote:

>    I was wondering if someone could explain gc.get_objects() in a bit
> more detail to me.
>    Does it return a list of 'all objects known to Python'? Only some of
> them? Which does it return? Which it does not?

This took about 10 seconds.

 >>> import gc
 >>> help(gc.get_objects)
Help on built-in function get_objects in module gc:

     get_objects() -> [...]

     Return a list of objects tracked by the collector (excluding the 
list returned).

Now, what object does gc track? CPython answer: objects that could be 
involved in and kept alive by reverence cycles and therefore not deleted 
when not needed by reference counting alone. Object instances, numbers, 
and strings cannot be involved in reference cycles, as there are no user 
settable references. Tuples can be:

a = []
b = (a,)
a[0] = b

A tuple of, for instance, ints would not need to be tracked, but if it 
is, someone probably decided that the cost of checking is not worth the 
benefit. Details are subject to change with versions. In 3.4, gc can 
break cycles of objects with delete methods, whereas they previously 
waiting until program shutdown. Some year ago, I remember a suggestion 
to turn off gc while creating lots of tuples that did not need to be 
tracked, but I do not know if that is still useful.

Terry Jan Reedy

More information about the Python-list mailing list