python内存泄露

 

转自:http://code.activestate.com/recipes/457665-debug-runtime-objects-using-gcget_objects/
Debug runtime objects using gc.get_objects() (Python recipe) by Dirk Holtwick
ActiveState Code (http://code.activestate.com/recipes/457665/)
 
  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, 45 linesDownload
Copy to clipboard

 1

 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()

 

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.


Tags: debugging 

2 comments

thinkbase net5 years, 8 months ago # |flag
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()

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
 
...

 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭