ActiveState Code

Recipe 457665: Debug runtime objects using gc.get_objects()


Since Python 2.2 there is a handy function in the Garbage Collection Module called get_objects(). It gives back a list of all objects that are under control of the Garbeage Collector. This way you can extract informations of your application in runtime.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gc
import inspect

exclude = [
    "function",
    "type",
    "list",
    "dict",
    "tuple",
    "wrapper_descriptor",
    "module",
    "method_descriptor",
    "member_descriptor",
    "instancemethod",
    "builtin_function_or_method",
    "frame",
    "classmethod",
    "classmethod_descriptor",
    "_Environ",
    "MemoryError",
    "_Printer",
    "_Helper",
    "getset_descriptor",
    ]

def dumpObjects():
    gc.collect()
    oo = gc.get_objects()
    for o in oo:
        if getattr(o, "__class__", None):
            name = o.__class__.__name__
            if name not in exclude:
                filename = inspect.getabsfile(o.__class__)            
                print "Object of class:", name, "...",
                print "defined in file:", filename                

if __name__=="__main__":

    class TestClass:
        pass
        
    testObject1 = TestClass()
    testObject2 = TestClass()
    
    dumpObjects()

Discussion

The example dumps a list of all higher level objects. At first it gets the list of all objects, than tests if they are higher level and in the end it it tests if it is not in the list of objects that are more or less always there. Then we can get all informations of the object we want. In this case we show the name of the class and where this class is defined.

A nice tool to write would be a runtime debugger in an own thread that serves as a little HTTPDServer, so that you could look into the running program no mater it is command line or windows based.

Comments

  1. 1. At 7:24 p.m. on 30 jan 2006, thinkbase net said:

    Test on Python 2.4.1. On Python 2.4.1, the code should be modified a little: -- Add "weakref" to the exclude list, such as:

    import gc
    import inspect
    
    exclude = [
        "function",
        "type",
        "list",
        "dict",
        "tuple",
        "wrapper_descriptor",
        "module",
        "method_descriptor",
        "member_descriptor",
        "instancemethod",
        "builtin_function_or_method",
        "frame",
        "classmethod",
        "classmethod_descriptor",
        "_Environ",
        "MemoryError",
        "_Printer",
        "_Helper",
        "getset_descriptor",
        "weakref", "property", "cell", "staticmethod"
        ]
    
    def dumpObjects():
        gc.collect()
        oo = gc.get_objects()
        for o in oo:
            if getattr(o, "__class__", None):
                name = o.__class__.__name__
                #print (name)
                if name not in exclude:
                    filename = inspect.getabsfile(o.__class__)
                    print "Object :", `o`, "..."
                    print "Class  :", name, "..."
                    print "defined:", filename, "\n"
    
    if __name__=="__main__":
    
        class TestClass:
            pass
    
        testObject1 = TestClass()
        testObject2 = TestClass()
    
        from wx import Colour
        color = Colour()
    
        dumpObjects()
    
  2. 2. At 7:28 p.m. on 30 jan 2006, thinkbase net said:

    And the result:

    Object : <__main__.TestClass instance at 0x00954D00> ...
    Class  : TestClass ...
    defined: c:\temp\py\test.py
    
    Object : <__main__.TestClass instance at 0x00954D28> ...
    Class  : TestClass ...
    defined: c:\temp\py\test.py
    
    Object : <class 'string.Template'> ...
    Class  : _TemplateMetaclass ...
    defined: d:\bin\python\lib\string.py
    
    Object : wx.Point(-1, -1) ...
    Class  : Point ...
    defined: d:\bin\python\lib\site-packages\wx-2.6-msw-unicode\wx\_core.py
    
    Object : wx.Size(-1, -1) ...
    Class  : Size ...
    defined: d:\bin\python\lib\site-packages\wx-2.6-msw-unicode\wx\_core.py
    
    Object : wxPython wrapper for UNBORN object! (The C++ object is not initialized yet.) ...
    Class  : _wxPyUnbornObject ...
    defined: d:\bin\python\lib\site-packages\wx-2.6-msw-unicode\wx\_core.py
    
    ...
    
    Object : <wx._core.__DocFilter instance at 0x00957E90> ...
    Class  : __DocFilter ...
    defined: d:\bin\python\lib\site-packages\wx-2.6-msw-unicode\wx\_core.py
    
    Object : wx.Colour(0, 0, 0) ...
    Class  : Colour ...
    defined: d:\bin\python\lib\site-packages\wx-2.6-msw-unicode\wx\_gdi.py
    
    Object : <wx._core.PyEventBinder object at 0x016CEED0> ...
    Class  : PyEventBinder ...
    defined: d:\bin\python\lib\site-packages\wx-2.6-msw-unicode\wx\_core.py
    
    ...
    

Sign in to comment