python webpy中显示进程中的所有类型对象占用的内存大小,pythonwebpy,webpy的python


webpy的python进程占用内存很大,为了查清到底什么类型的对象占用了内存,写了下面的controller,希望对别人有帮助

class WatchMemory:    def GET(self):        import gc        import sys        from collections import defaultdict        d = defaultdict(int)        objects = gc.get_objects()        #print 'gc objects size:', len(objects)        for o in objects:            d[type(o)] += sys.getsizeof(o)        from pprint import pformat        data = dict(d)        dataList = [(t,tSize/1024.0/1024.0) for (t,tSize) in data.items()]        for i in xrange(0,len(dataList)-1):            for j in xrange(i+1,len(dataList)):                if dataList[i][1] < dataList[j][1]:                    temp = dataList[i]                    dataList[i] = dataList[j]                    dataList[j] = temp        return pformat(dataList)

使用的时候需要给这个controller配置url映射。

输出的结果类似:

[(<type 'dict'>, 40.14639663696289), (<type 'list'>, 0.9796180725097656), (<type 'function'>, 0.3652381896972656), (<type 'type'>, 0.24182510375976562), (<type 'frame'>, 0.08407211303710938), (<type 'tuple'>, 0.07085418701171875), (<type 'builtin_function_or_method'>, 0.05657958984375), (<type 'wrapper_descriptor'>, 0.0490570068359375), (<type 'weakref'>, 0.04254913330078125), (<type 'instancemethod'>, 0.0397491455078125), (<type 'method_descriptor'>, 0.02574920654296875), (<type 'classobj'>, 0.025440216064453125), (<type 'set'>, 0.022708892822265625), (<type 'getset_descriptor'>, 0.020496368408203125), (<type 'StgDict'>, 0.016193389892578125),

评论关闭