Package gmisclib :: Module findleak
[frames] | no frames]

Source Code for Module gmisclib.findleak

  1  # -*- Mode: Python; tab-width: 4 -*- 
  2   
  3  __author__ = "Samual M. Rushing" 
  4  # See http://www.nightmare.com/medusa/memory-leaks.html 
  5   
  6  import sys 
  7  import types 
  8  import os 
  9  import re 
 10   
11 -def get_refcounts():
12 d = {} 13 # collect all classes 14 for m in sys.modules.values(): 15 for sym in dir(m): 16 o = getattr (m, sym) 17 if type(o) is types.ClassType: 18 d[o] = sys.getrefcount (o) 19 # sort by refcount 20 pairs = [ (x[1], x[0]) for x in d.items() ] 21 pairs.sort() 22 pairs.reverse() 23 return pairs
24 25 30 31
32 -def readvm():
33 fd = open("/proc/%d/status" % os.getpid(), "r") 34 lines = fd.readlines() 35 fd.close() 36 nvms = 0 37 for line in lines: 38 a = line.split() 39 if a[0] == 'VmData:': 40 assert a[2] == 'kB' 41 nvms = int(a[1]) 42 return nvms
43 44 _a1kid = 1
45 -def alloc1k():
46 global _a1kid 47 tmp = ('%10d' % _a1kid) * 100 48 _a1kid += 1 49 return tmp
50 51 _vmstat = 0
52 -def vm(comment=''):
53 global _vmstat 54 nvms = 0 55 prefix = '#VMSTAT: ' 56 size = 0 57 tmp1 = readvm() 58 memstore = [] 59 while 1: 60 memstore.append( alloc1k() ) 61 size += 1 62 tmp2 = readvm() 63 if tmp2 > tmp1: 64 break 65 # print 'len(ms)', len(memstore), tmp2, tmp1 66 nvms = tmp1 + 1 - size 67 delta = nvms - _vmstat 68 _vmstat = nvms 69 print '%s%d # %s' % (prefix, delta, comment)
70 71 vm('STARTUP') 72 73
74 -def lookvmstat(fname):
75 def usage_add(u, id, v): 76 try: 77 u_id = u[id] 78 x = (1 + u_id[0], v+u_id[1]) 79 80 except KeyError: 81 x = (1, v) 82 u[id] = x
83 84 fd = open(fname, 'r') 85 vml = re.compile('^#VMSTAT:') 86 usage = {} 87 while 1: 88 l = fd.readline() 89 if l == '': 90 break 91 if vml.match(l): 92 a = l.strip().split() 93 if a[2] != '#': 94 continue 95 id = ' '.join(a[3:]) 96 vec = float(a[1]) 97 usage_add(usage, id, vec) 98 fd.close() 99 tmp = [ ( -v[1]/float(v[0]), id, v) for (id, v) in usage.items() ] 100 tmp.sort() 101 for (fom, id, v) in tmp[:20]: 102 print id, v 103 104 105 if __name__ == '__main__': 106 vm() 107 print_top_N() 108 vm() 109 print_top_N() 110 vm() 111 tmp = 'adadsadadasd' * 1000 112 vm() 113 tmp = None 114 vm() 115