referrers

Diez B. Roggisch deets at nospam.web.de
Wed May 24 12:56:36 CEST 2006


raghu wrote:

> Diez,
> 
> I did look into gc, specifically gc.get_referrers(), but it seemed to
> give me something that I cant decipher.
> 
> I added the following line.
> 
> print "referrers ",gc.get_referrers(x0)
> 
> This is what I got.
> 
> referrers  [{'__builtins__': <module '__builtin__' (built-in)>,
> '__file__': './tst1.py', 'pdb': <module 'pdb' from
> '/home/raghavan/Python-2.4/my_install/lib/python2.4/pdb.pyc'>, 'sys':
> <module 'sys' (built-in)>, 'y': 24012, 'gc': <module 'gc' (built-in)>,
> 'myfuncs': <module 'myfuncs' from '/home/Raghavan/tst/myfuncs.dll'>,
> '__name__': '__main__', 'x0': 24012, 'z': 24012, 'os': <module 'os'
> from '/home/raghavan/Python-2.4/my_install/lib/python2.4/os.pyc'>,
> '__doc__': None, 'types': <module 'types' from
> '/home/raghavan/Python-2.4/my_install/lib/python2.4/types.pyc'>},
> (None, '/home/Raghavan/tst', 'my pid is ', 24012, 'ref count ',
> 'referrers ')]
> 
> Also the len of this is 2, while I got refcount=5. So I dont know
> whether this can be used in the same way.

The refcount is always precise - as it is a simple integer that happens to
be part of every python object. But the 1:n-relation to its referres is
computed and not necessarily complete, as the docs state very clear.

Additionally, they state that this method is only to be used for
debugging-purposes. 

I'm not sure what you are after here - if it is about solving a mem-leak, gc
might help. If it is some application logic, the advice must be clearly to
explicitly model the object graph bi-directional.

Diez



More information about the Python-list mailing list