What does gc.get_objects() return?

Dan Stromberg drsalists at gmail.com
Wed Mar 12 15:53:27 EDT 2014


On Wed, Mar 12, 2014 at 12:34 PM, Jurko Gospodnetić
<jurko.gospodnetic at pke.hr> wrote:
>   Hi all.
>
>   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?
>
>   For example (done using CPython 3.4 interactive interpreter) here it does
> not contain an object I know exists:
>
>> >>> a = object()
>> >>> a in gc.get_objects()
>> False
>
>
>   but here it does:
>
>> >>> class Foo: pass
>> ...
>> >>> a = Foo()
>> >>> a in gc.get_objects()
>> True

I don't know why some objects aren't showing up in get_objects, but
here's a quick note to say it's not just 3.4:

$ ./pythons 'import gc; var="data"; print(var in gc.get_objects())'
/usr/local/cpython-2.4/bin/python
   [6600 refs]
   False
/usr/local/cpython-2.5/bin/python
   False
   [7536 refs]
/usr/local/cpython-2.6/bin/python
   False
   [15233 refs]
/usr/local/cpython-2.7/bin/python
   False
   [18526 refs]
/usr/local/cpython-3.0/bin/python
   Traceback (most recent call last):
     File "<string>", line 1, in <module>
     File "/usr/local/cpython-3.0/lib/python3.0/_weakrefset.py", line
121, in __eq__
       return self.data == set(ref(item) for item in other)
     File "/usr/local/cpython-3.0/lib/python3.0/_weakrefset.py", line
121, in <genexpr>
       return self.data == set(ref(item) for item in other)
   TypeError: cannot create weak reference to 'str' object
   [34510 refs]
/usr/local/cpython-3.1/bin/python
   False
   [37270 refs]
/usr/local/cpython-3.2/bin/python
   False
   [37700 refs]
/usr/local/cpython-3.3/bin/python
   False
   [42662 refs]
/usr/local/cpython-3.4/bin/python False
/usr/local/pypy-2.2/bin/pypy True
/usr/local/jython-2.7b1/bin/jython
   Traceback (most recent call last):
     File "<string>", line 1, in <module>
   NotImplementedError: not applicable to Java GC

Interesting that it is present in Pypy.



More information about the Python-list mailing list