referrers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raghu

    #1

    referrers

    Hi All,

    The sys.getrefcount () is very useful to get the number of references on
    a particular object.

    Is there any companion function to get "who" the referrers are ?

    for e.g.

    global x
    global y
    global z


    x0=24012
    y=x0
    z=x0

    print "ref count ",sys.getrefcou nt(x0)


    This prints a ref count of 5.

    Basically, I need to know which are the 5 entities who are referring to
    x0 ?

    Is there any way of doing it ?

    Thanks.

    Bye,
    raghavan V

  • Diez B. Roggisch

    #2
    Re: referrers

    raghu wrote:
    [color=blue]
    > Hi All,
    >
    > The sys.getrefcount () is very useful to get the number of references on
    > a particular object.
    >
    > Is there any companion function to get "who" the referrers are ?
    >
    > for e.g.
    >
    > global x
    > global y
    > global z
    >
    >
    > x0=24012
    > y=x0
    > z=x0
    >
    > print "ref count ",sys.getrefcou nt(x0)
    >
    >
    > This prints a ref count of 5.
    >
    > Basically, I need to know which are the 5 entities who are referring to
    > x0 ?
    >
    > Is there any way of doing it ?[/color]

    Look into the gc-module.

    Diez

    Comment

    • raghu

      #3
      Re: referrers

      Diez,

      I did look into gc, specifically gc.get_referrer s(), but it seemed to
      give me something that I cant decipher.

      I added the following line.

      print "referrers ",gc.get_referr ers(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.

      Thanks.

      Bye,
      Raghavan V

      Comment

      • Diez B. Roggisch

        #4
        Re: referrers

        raghu wrote:
        [color=blue]
        > Diez,
        >
        > I did look into gc, specifically gc.get_referrer s(), but it seemed to
        > give me something that I cant decipher.
        >
        > I added the following line.
        >
        > print "referrers ",gc.get_referr ers(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.[/color]

        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

        Comment

        Working...