1

I have a certain object obj What I want is to get all the other objects referencing it. I know that I can get a reference count as sys.getrefcount(obj), but that's not of much use in my case - I need the actual referencing objects, although I'm not sure that's even possible in Python...

Also, getting the line of code that created the object isn't of much use to me either - I know, where the object was created - I just don't know who is referencing it at the moment.

P.S. I need this to debug a leak.

4
  • I don't think Python keeps a list of the actual references; probably no reference-counting language keeps such a list. Commented Jan 21, 2013 at 7:50
  • @lanzz: Don't forget that CPython isn't purely reference-counted. It has a secondary garbage collector that deals with reference cycles. Commented Jan 21, 2013 at 8:09
  • What are you really trying to do? Suppose you find out this information; what do you propose to do with it? Commented Jan 21, 2013 at 8:53
  • @KarlKnechtel: If I find that out, then I'll know where the reference to the object wasn't deleted (but should have been) and can investigate, why it wasn't. Commented Jan 21, 2013 at 9:41

2 Answers 2

3

Take a look at the gc module. This is the closest you can get.

A good site is, e.g., this one.

Sign up to request clarification or add additional context in comments.

3 Comments

I guess gc.get_referrers is pretty much what I'm looking for. I don't know, how I overlooked that. Thanks!
You're welcome. If this helps you, please mark this as the solution so it doesn't show up in the "unanswered" section anymore.
For anyone else looking for a non-dead link for the the reference of the accepted answer, it ultimately points to this article: pymotw.com/2/gc Note that it's written specifically for Python 2, and does not appear to have been updated to Python 3 (and I'm unsure what still applies and what doesn't)
0

This is just an idea: If you know where the object is created, I can imagine that you know the potential variables and classes that can have a reference to your object. So you may be able to use the id function to check who is referencing what.

I hope it helps

2 Comments

The data structure I'm dealing with is quite complex and changing constantly with a lot of pickling and unpickling going on, so tracking by IDs would get really messy, besides I might not be aware of some of the possible referrers (there's just way to much code, to know it all)
Ok, I understand. That was just an idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.