Understanding gc.get_referrers

I am trying to track a memory leak in Python (2.7). I found gc.get_referrers but don't understand the way out. After removing dying_node (which should get rid of all the links except the list that I created as part of my hunt), I have in my code:

 gc.collect() print "done dying: ", getrefcount(dying_node) #note, includes the reference from getrefcount referrers = gc.get_referrers(dying_node) print "referrers: " for referrer in referrers: print referrer 

which gives an output:

 > done dying: 4 > referrers: > [<__main__.Node instance at 0x104e53cb0>, <__main__.Node instance at 0x104e53b90>, <__main__.Node instance at 0x104e53b00>, <__main__.Node instance at 0x104e53d40>, <__main__.Node instance at 0x104e53ab8>, <__main__.Node instance at 0x104e53bd8>, <__main__.Node instance at 0x104e53a70>, <__main__.Node instance at 0x104e53c20>, <__main__.Node instance at 0x104e53c68>, <__main__.Node instance at 0x104e53b48>] > [<__main__.Node instance at 0x104e53c20>, <__main__.Node instance at 0x104e53ab8>, <__main__.Node instance at 0x104e53c68>, <__main__.Node instance at 0x104e53a70>, <__main__.Node instance at 0x104e53cb0>, <__main__.Node instance at 0x104e53b00>, <__main__.Node instance at 0x104e53d40>, <__main__.Node instance at 0x104e53b90>, <__main__.Node instance at 0x104e53b48>, <__main__.Node instance at 0x104e53bd8>] > <frame object at 0x104516300> 

I think this means that I have two Node lists that apply to this node and to the frame object. I assume that the frame object is the name dying_node that I am looking at. One of the lists will be a list that I created to help me in my hunt. But is there any way to figure out what the other list will be?

+5
source share
1 answer

Ok so the answer

 def namestr(obj, namespace): return [name for name in namespace if namespace[name] is obj] 

Example:

 gc.collect() #make sure all garbage cleared before collecting referrers. referrers = gc.get_referrers(object_of_interest) for referrer in referrers: print namestr(referrer, globals()) 

or if it is local:

  print namestr(referrer, locals()) 

This will print something like ['referrer', 'name_Im_interested_in'] . 'referrer' is because I just called it. Another thing on the list is what I'm trying to find.

I took it from here . If anyone has a better answer, send it and I will be happy to accept it.

+1
source

Source: https://habr.com/ru/post/1212935/


All Articles