Search code examples
pythonmemory-leaks

Tracing the references for a specific object in Python (memory leak)


I have a 3rd party library creating huge memory leaks. I've called gc.collect, and have no references to any objects created by this library, but it still leaks.

Using gc.get_objects() I've identified numerous objects which should not be alive, as they are specific to this library.

How can I trace which objects are keeping these leaked objects alive (the goal being to trace it to a global variable/list, which I can reset to fix the leak)?

Files and line numbers would be nice, but even having the instances of the holder objects would help a great deal.


Solution

  • You could try gc.get_referrers. Demo:

    import gc
    
    def test():
        a = ['foo', b := {'bar'}]
        for r in gc.get_referrers(b):
            print(r)
    
    test()
    

    Output (Attempt This Online!):

    ['foo', {'bar'}]