Find all references to an object in python

后端 未结 2 1288
滥情空心
滥情空心 2020-12-02 23:02

What is a good way to find all of the references to an object in python?

The reason I ask is that it looks like we have a \"memory leak\". We are up

相关标签:
2条回答
  • 2020-12-02 23:20

    Python's gc module has several useful functions, but it sounds like gc.get_referrers() is what you're looking for. Here's an example:

    import gc
    
    
    def foo():
        a = [2, 4, 6]
        b = [1, 4, 7]
    
        l = [a, b]
        d = dict(a=a)
        return l, d
    
    l, d = foo()
    r1 = gc.get_referrers(l[0])
    r2 = gc.get_referrers(l[1])
    
    print r1
    print r2
    

    When I run that, I see the following output:

    [[[2, 4, 6], [1, 4, 7]], {'a': [2, 4, 6]}]
    [[[2, 4, 6], [1, 4, 7]]]
    

    You can see that the first line is l and d, and the second line is just l.

    In my brief experiments, I've found that the results are not always this clean. Interned strings and tuples, for example, have more referrers than you would expect.

    0 讨论(0)
  • 2020-12-02 23:27

    Python's standard library has gc module containing garbage collector API. One of the function you possible want to have is

    gc.get_objects()
    

    This function returns list of all objects currently tracked by garbage collector. The next step is to analyze it.

    If you know the object you want to track you can use sys module's getrefcount function:

    >>> x = object()
    >>> sys.getrefcount(x)
    2
    >>> y = x
    >>> sys.getrefcount(x)
    3
    
    0 讨论(0)
提交回复
热议问题