def showRefs(self): objType = str(self.inputWidget.text()) with self.showTempImage() as fn: objgraph.show_refs(objgraph.by_type(objType), filter=self.filterPrimitives, max_depth=self.depthLimitBox.value(), too_many=self.widthLimitBox.value(), filename=fn)
def get_counts(prefix=''): test_types = [ 'Var', 'Piecewise', 'ScenarioTree', 'ScenarioTreeNode', 'Scenario', '_SetContainer', '_ConstraintArray', ] # use objgraph to check if these pyomo objects still exist objects = {} test_counts = {} for name in test_types: objects[name] = objgraph.by_type( name ) test_counts[name] = len(objects[name]) if True: for name in test_types: if test_counts[name]==0: continue else: obj = objects[name][0] fname = prefix+'-'+'objgraph-{}-'.format(name) objgraph.show_refs( [obj], filename=fname+'refs.png') # too_many=50, objgraph.show_backrefs([obj], filename=fname+'backref.png') objgraph.show_chain( objgraph.find_backref_chain( obj, inspect.ismodule), filename=fname+'chain.png' ) return test_counts
def test_memory_usage(self): ''' Links about debugging memory usage http://stackoverflow.com/questions/110259/which-python-memory-profiler-is-recommended?answertab=votes#tab-top http://mg.pov.lt/objgraph/ ''' return # setup the pins and activity chunk import objgraph x = [] y = [x, [x], dict(x=x)] objgraph.show_refs([y], filename='sample-graph.png') admin_user_id = 1 pins = list(Pin.objects.filter(user=admin_user_id)[:3]) activities = [p.create_activity() for p in pins] # try a batch import for x in range(500): feedly.batch_import(admin_user_id, activities, 10) if x % 10 == 0: print 'growth', x print objgraph.show_growth(limit=5) print 'growth' print objgraph.show_growth(limit=5) objgraph.show_refs([Mutation], filename='sample-graph.png')
def search(): print("---objects at start---") objgraph.show_growth(limit=3) rows = arcpy.SearchCursor("C:/temp/address_point.shp", "", "", "STREET_NAM", "STREET_NAM A") objgraph.show_refs([rows], filename='objgraph.png') objgraph.show_backrefs([rows], filename='objgraph-backrefs.png') current_street = "" print("---objects before---") objgraph.show_growth() for row in rows: if current_street != row.STREET_NAM: current_street = row.STREET_NAM print(current_street) print("--objects after loop---") objgraph.show_growth() del rows print("---objects after cursor delete---") objgraph.show_growth()
def showGarbage(self): with self.showTempImage() as fn: objgraph.show_refs( gc.garbage, filter=self.filterPrimitives, max_depth=self.depthLimitBox.value(), too_many=self.widthLimitBox.value(), filename=fn, )
def enqueue(self, obj): data = pickle.dumps(obj) if getattr(self, "_debug_pickle", False): import objgraph restored = pickle.loads(data) objgraph.show_refs(restored, too_many=40) data = snappy.compress(data) self.debug("Broadcasting %d bytes" % len(data)) zmq_connection = getattr(self, "zmq_connection") if zmq_connection is not None: zmq_connection.send(data)
def objgraph(shipment, filename): """Dump object graph using objgraph.""" import objgraph def filter_entities(obj): return isinstance(obj, (BaseShip, BaseFile, Volume, Container, Shipment, dict, list)) def highlight(obj): return not isinstance(obj, (dict, list)) # Max depth is 14 because so many nodes are from Shipment (top) to File object (bottom) object # This value should be changed if graph depth changes objgraph.show_refs(shipment, filename=filename, max_depth=14, filter=filter_entities, highlight=highlight)
def draw_it(*args): """Draw and show it. """ _, tmpf = tempfile.mkstemp(suffix=".png") objgraph.show_refs(args, filename=tmpf) if sys.platform.startswith("darwin"): subprocess.call(("open", tmpf)) elif os.name == "nt": subprocess.call(("start", tmpf)) elif os.name == "posix": subprocess.call(("xdg-open", tmpf))
def __call__(self, environ, start_response): request = webob.Request(environ) if request.path_info.startswith(self.signature): query = request.GET.get('query') obj_type = request.GET.get('object_type') obj_address = request.GET.get('object_address') if obj_address: # Create a graph for the object leaking = [objgraph.at(int(obj_address))] filename = tempfile.mktemp(suffix='.png') objgraph.show_refs(leaking, filename=filename) output = open(filename, 'r').read() os.unlink(filename) start_response( '200 Ok', [('Content-Type', 'image/png')]) return output output = StringIO() leaking = objgraph.get_leaking_objects() formats.htmlHeader(output, self.signature, request.host_url, query) output.write('<h3>Memory usage</h3>') output.write('<table>' + ''.join( map(lambda info: MEMORY_LINE.format(*info), objgraph.most_common_types(10, leaking))) + '</table>') if obj_type: # Display detail about leaking objects of a given type. output.write('<h4>Memory detail for %s</h4>' % obj_type) output.write('<ul>') for obj in leaking: if type(obj).__name__ == obj_type: output.write(OBJECT_LINE.format( id(obj), cgi.escape(str(obj)))) output.write('</ul>') output.write('<h3>Timing</h3>') formats.htmlFormat(output, query=query) start_response( '200 Ok', [('Content-Type', 'text/html; charset=UTF8')]) return output.getvalue() scale = self.find(request.path_info) scale.requests.mark() with scale.latency.time(): response = request.get_response(self.app) result = scale.statuses(response.status_int) if result is not None: result.mark() start_response( response.status, [a for a in response.headers.iteritems()]) return response.app_iter
def on_selection_change(self, *args): #pass if self.selection: #from listscreen import ListScreenItem objgraph.show_growth() print '...' roots = objgraph.get_leaking_objects() objgraph.show_most_common_types(objects=roots) print '...' objgraph.show_refs(roots[:3], refcounts=True, filename='sad.png') #objgraph.show_chain(objgraph.find_backref_chain(self.selection[0].__self__, objgraph.is_proper_module),filename='chain.png') #objgraph.show_backrefs(self.selection[0].__self__, filename='sample-backref-graph.png') print '...'
def example(): x = [] y = [x, [x], dict(x=x)] objgraph.show_refs( (x, y), filename='show_refs.png', refcounts=True ) objgraph.show_backrefs( (x, y), filename='show_backrefs.png', refcounts=True )
def _objgraphs(): import objgraph print(objgraph.show_backrefs(objgraph.by_type("requirement"), filename='/home/anaphora/backrefs.png')) print(objgraph.show_backrefs(objgraph.by_type("Stat"), filename='/home/anaphora/statrefs.png')) print(objgraph.show_backrefs(objgraph.by_type("QueryAPI"), filename='/home/anaphora/dbrefs.png')) print(objgraph.show_backrefs(objgraph.by_type("Noun"), filename='/home/anaphora/nounrefs.png')) print(objgraph.show_refs(objgraph.by_type("Noun"), filename='/home/anaphora/nounrefs2.png'))
def make_graph(filename): import objgraph def extra_info(o): if isinstance(o, dict): return o.keys() else: # list return "id={} val={}\nprev={} next={}".format(hex(id(o)), o[RESULT], o[PREV][RESULT], o[NEXT][RESULT]) objgraph.show_refs([cache, nonlocal_root[0]], filename=filename, refcounts=True, max_depth=10, filter=lambda o: isinstance(o, (dict, list)), highlight=lambda o: o is nonlocal_root[0], too_many=maxsize or 100000, extra_info=extra_info)
def print_memory(i): global object_counts print("\n\n--------------------- MEMORY -------------------------\n") print("TOTAL OBJECTS\n") o = len(gc.get_objects()) print(o) object_counts.append(o) del o print("\n") print("GROWTH\n") objgraph.show_growth() print("\n") print("COMMON TYPES\n") objgraph.show_most_common_types() print("\n") print("LEAKING OBJECTS\n") roots = objgraph.get_leaking_objects() print("\n") log.info("ROOTS pre-collect : {}\n".format(len(roots))) print("COMMON TYPES IN ROOTS\n") objgraph.show_most_common_types(objects=roots) print("\n") objgraph.show_refs(roots[:3], refcounts=True, filename=TEST_PATH + '/roots_' + str(i) + '.png') print("\n") log.info("Garbage pre collect: " + str(len(gc.garbage))) gc.collect() log.info("Garbage post collect: " + str(len(gc.garbage))) print("\n") roots = objgraph.get_leaking_objects() log.info("ROOTS post-collect : {}".format(len(roots))) print("\n\n---------------------------------------------------\n")
def __dbg_mem(strn): if __DBGMEM: import gc, objgraph print print '#' * 80 print '#' * 80 print '##', strn print 'Collect', gc.collect() print 'Collect', gc.collect() roots = objgraph.get_leaking_objects() if roots: print len(roots) objgraph.show_most_common_types(objects=roots) objgraph.show_refs(roots[:3], refcounts=True, filename='tmp/%s.png' % strn.lower()) else: print 'Nothing' print '#' * 80 print '#' * 80 print
def checkCbRefcount(self): if not self.should_check_refcount: return import gc if platform.python_implementation() == 'PyPy': return if os.environ.get("PYCBC_TRACE_GC") in ['FULL','GRAPH_ONLY']: import objgraph graphdir=os.path.join(os.getcwd(),"ref_graphs") try: os.makedirs(graphdir) except: pass for attrib_name in ["cb.tracer.parent", "cb"]: try: logging.info("evaluating "+attrib_name) attrib = eval("self." + attrib_name) options = dict(refcounts=True, max_depth=3, too_many=10, shortnames=False) objgraph.show_refs(attrib, filename=os.path.join(graphdir, '{}_{}_refs.dot'.format(self._testMethodName, attrib_name)), **options) objgraph.show_backrefs(attrib, filename=os.path.join(graphdir, '{}_{}_backrefs.dot'.format(self._testMethodName, attrib_name)), **options) logging.info("got referrents {}".format(repr(gc.get_referents(attrib)))) logging.info("got referrers {}".format(repr(gc.get_referrers(attrib)))) except: pass gc.collect() for x in range(10): oldrc = sys.getrefcount(self.cb) if oldrc > 2: gc.collect() else: break
def get_leaking_objects(file=None, limit=5): roots = objgraph.get_leaking_objects() objgraph.show_refs(roots[:limit], refcounts=True, output=file)
def __init__(self): self.b = B(self) def main(): a = A() a = None if __name__ == '__main__': import gc, objgraph, inspect main() gc.set_debug(gc.DEBUG_SAVEALL) gc.collect() isclass = inspect.isclass #My optimization ignore = lambda x: not isclass(x) objgraph.show_refs(gc.garbage, filename='graph.png', max_depth=4, filter=ignore) ############################## # weakref # Example for Py 2.7 # Use for free from cycles import weakref class A(object): def __init__(self): self.callback = None def bind(self, callback): self.callback = callback def main():
def test1(): objgraph.show_refs(example(3), filename="obj.png", refcounts=True)
def test2(): x = range(100) y = map(example, x) objgraph.show_refs(y, filename="obj2.png", refcounts=True)
# pickle.debug.trace(True) # import pickle # get all objects for testing from dill import load_types load_types(pickleable=True, unpickleable=True) from dill import objects if __name__ == "__main__": import sys if len(sys.argv) != 2: print("Please provide exactly one type name (e.g. 'IntType')") msg = "\n" for objtype in list(objects.keys())[:40]: msg += objtype + ", " print(msg + "...") else: objtype = str(sys.argv[-1]) obj = objects[objtype] try: import objgraph objgraph.show_refs(obj, filename=objtype + ".png") except ImportError: print("Please install 'objgraph' to view object graphs") # EOF
x = [1, 2, 3] y = [x, dict(key1=x)] z = [y, (x, y)] import objgraph objgraph.show_refs([z], filename="ref_topo.png")
l = func() show_memory_info("列表变量完成后") jishu() print("手动回收垃圾") show_memory_info("初始化前") a = [i for i in range(10000000)] show_memory_info("初始化后") del a gc.collect() show_memory_info("完成") # print(a) # 循环引用 func3() show_memory_info("循环引用完成") gc.collect() show_memory_info("手动垃圾回收完成") # objgraph a = [1, 2, 3] b = [4, 5, 6] a.append(b) b.append(a) objgraph.show_refs([a], filename="objref.png") objgraph.show_backrefs([a], filename="backref.png")
def test3(): objgraph.show_refs(example2(), filename="obj3.png", refcounts=True, max_depth=5, too_many=10)
x = [1,2,3] y = [x, dict(key1=x)] z = [y,(x,y)] import objgraph objgraph.show_refs([z],filename='ref_topo.png')
| gc.DEBUG_OBJECTS) def print_mem(): mem = process.get_memory_info()[0] / _TWO_20 f = sys._getframe(1) line = linecache.getline(f.f_code.co_filename, f.f_lineno - 1).replace('\n', '') print('%s:%d \t| %.6f \t| %s' % (f.f_code.co_name, f.f_lineno, mem, line)) c = perf('Line', 100, 500) print_mem() a = c.render() print_mem() import objgraph objgraph.show_refs([c], filename='sample-graph.png') gc.collect() print_mem() print(gc.garbage) print_mem() del a print_mem() del c print_mem() sys.exit(0) charts = CHARTS if '--all' in sys.argv else 'Line', for impl in ['lxml', 'etree']: if impl == 'lxml':
def test5(): objgraph.show_refs(example3(), filename="obj5.png", refcounts=True, max_depth=10, too_many=20)
skip_undefined_matrices=True) else: model = OP2() model._cleanup_words() if 0: model.remove_empty_results() del model.case_control_deck del model.matrices for name in model.object_methods(): try: delattr(model, name) except AttributeError: pass objgraph.show_growth() objgraph.show_refs(model, too_many=100, filename='sample-graph.png') #objgraph.show_backrefs(model, too_many=1000, filename='sample-backref-graph.png') # bad... #objgraph.show_chain(objgraph.find_backref_chain(model), filename='chain.png') objgraph.show_chain(objgraph.find_backref_chain( random.choice(objgraph.by_type('dict')), objgraph.is_proper_module), filename='chain.png') roots = objgraph.get_leaking_objects() objgraph.show_most_common_types(objects=roots) objgraph.show_refs(roots[:3], refcounts=True, too_many=100, filename='roots.png')
def show_references(): x = [] y = [x, [x], dict(x=x)] objgraph.show_refs([y], filename='y-references.png')
"""Grafické zobrazení referencí.""" import objgraph x = "Foo" y = [x, "bar"] # zobrazení referencí na dva řetězce uložené v seznamu objgraph.show_refs([y], filename="objgraph3.png")
#!/bin/env python3 import objgraph a = list(range(10)) objgraph.show_refs([a], filename='python_memory_layout.dot')
gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_INSTANCES | gc.DEBUG_OBJECTS) def print_mem(): mem = process.get_memory_info()[0] / _TWO_20 f = sys._getframe(1) line = linecache.getline( f.f_code.co_filename, f.f_lineno - 1).replace('\n', '') print('%s:%d \t| %.6f \t| %s' % ( f.f_code.co_name, f.f_lineno, mem, line)) c = perf('Line', 100, 500) print_mem() a = c.render() print_mem() import objgraph objgraph.show_refs([c], filename='sample-graph.png') gc.collect() print_mem() print(gc.garbage) print_mem() del a print_mem() del c print_mem() sys.exit(0) charts = CHARTS if '--all' in sys.argv else 'Line', for impl in ['lxml', 'etree']:
p = [1, 2] o = obj(p) print id(p) print id(o.para) print p is o.para #garbage collection --generation 0 1 2 #import gc #print gc.get_threshold() #get_threshold() -> (threshold0, threshold1, threshold2) #Return the current collection thresholds #gc.set_threshold(700,10,10) #gc.collect() #manually collect #objgraph #install #sudo apt-get install python-pip #sudo pip install xdot #sudo pip install objgraph print "objgraph test" x = [1, 2] y = [x, dict(key1=x, k=1)] z = [y, (x, y)] import objgraph objgraph.show_refs([z], filename='./objgraph_test1.dot') #reference cycle x = [] y = [x] x.append(y) objgraph.show_refs([x], filename='./objgraph_RefCycle.dot')
def og(): objgraph.show_refs([f])
import objgraph import xdot a = [1, 2, 3] b = [4, 5, 6] a.append(b) b.append(a) objgraph.show_refs([a], filename='show_refs.png') objgraph.show_backrefs([a], filename='show_backrefs.png') xdot.dot.parser()
def showGraph(self): import objgraph objgraph.show_refs(self.graphObj(), max_depth = 2, filter = lambda x : isinstance(x, (dict, HyperEdge)))
""" #XXX: useful if could read .pkl file and generate the graph... ? import dill as pickle #pickle.debug.trace(True) #import pickle # get all objects for testing from dill import load_types load_types(pickleable=True, unpickleable=True) from dill import objects if __name__ == "__main__": import sys if len(sys.argv) != 2: print("Please provide exactly one type name (e.g. 'IntType')") msg = "\n" for objtype in list(objects.keys())[:40]: msg += objtype + ', ' print(msg + "...") else: objtype = str(sys.argv[-1]) obj = objects[objtype] try: import objgraph objgraph.show_refs(obj, filename=objtype + '.png') except ImportError: print("Please install 'objgraph' to view object graphs") # EOF
distro = settings.RHEL6 comps = common.get_default_components() def filter_c(c): if not inspect.isclass(c): return False if c is object: return False return True actions = settings.ACTIONS for action in actions: klss = list() for c in comps.keys(): kls = common.get_action_cls(action, c, distro) klss.append(kls) max_depth = 5 fn = "%s.png" % (action) objgraph.show_refs( klss, filename=fn, max_depth=max_depth, highlight=inspect.isclass, filter=filter_c, extra_ignore=[id(locals())])
#!usr/bin/python # -*- coding:utf8 -*- import objgraph a = [1, 2, 3] b = [4, 5, 6] a.append(b) b.append(a) objgraph.show_refs([a])
#! /usr/bin/env python # coding:utf-8 # 两个对象可能相互引用,从而构成所谓的引用环(reference cycle) # 引用环会给垃圾回收机制带来很大的麻烦 # 即使是一个对象,只需要自己引用自己,也能构成引用环 import objgraph from sys import getrefcount a = [] b = [a] a.append(b) print(getrefcount(a)) print(getrefcount(b)) objgraph.show_refs([a], filename='memoryManagerDemo4_a.png') objgraph.show_refs([b], filename='memoryManagerDemo4_b.png') c = [] c.append(c) print(getrefcount(c)) objgraph.show_refs([c], filename='memoryManagerDemo4_c.png')
#!/usr/bin/env python # -*- coding: utf-8 -*- import objgraph foo = {} foo['A'] = 1 foo['B'] = 2 objgraph.show_refs([foo], filename='foo.png')
def obj(inp ,bot=None): # print hpy().heap()[0].byvia o = str(hpy().heap().bytype) o += str(hpy().heap().bytype[0].byvia) objgraph.show_refs(bot, filename='/var/www/homeromem2.png', max_depth=8, refcounts=True) return o
# def check(func): # def inner(): # print("登录验证") # func() # return inner class check: def __init__(self, func): self.f = func def __call__(self, *args, **kwargs): print("登录验证") self.f() @check def fashuoshuo(): print("发说说") # fashuoshuo = check(fashuoshuo) fashuoshuo() x = "abc" y = [x] z = [x, y] import objgraph # objgraph.show_refs(y, filename='test.png') objgraph.show_refs(z, filename="test.png")
#XXX: useful if could read .pkl file and generate the graph... ? import dill as pickle #pickle.debug.trace(True) #import pickle # get all objects for testing from dill import load_types load_types(pickleable=True,unpickleable=True) from dill import objects if __name__ == "__main__": import sys if len(sys.argv) != 2: print ("Please provide exactly one type name (e.g. 'IntType')") msg = "\n" for objtype in list(objects.keys())[:40]: msg += objtype + ', ' print (msg + "...") else: objtype = str(sys.argv[-1]) obj = objects[objtype] try: import objgraph objgraph.show_refs(obj, filename=objtype+'.png') except ImportError: print ("Please install 'objgraph' to view object graphs") # EOF
"""Grafické zobrazení referencí.""" import objgraph x = "Foo" y = [x, "bar"] # zobrazení referencí ze seznamu y objgraph.show_refs(y, filename='objgraph2.png')
def clickedDump(self): format.writer.xml.write("tmp.auc", self.afefuc['project']) import objgraph objgraph.show_refs(self.afefuc['project'], refcounts=True, filename='project-refs.png')
func2() show_memory_info('finished') # 手动回收 show_memory_info("局部变量初始化") a = [i for i in range(1000000)] show_memory_info("局部变量创建后") del a gc.collect() # print(a) # 循环引用 func3() show_memory_info("循环引用完成") gc.collect() show_memory_info("手动垃圾回收完成") # objgraph a = [1, 2, 3] b = [4, 5, 6] a.append(b) b.append(a) objgraph.show_refs( [a], filename= " /Users/huangjiancong/Desktop/Python3Demo/Python核心技术与实践/objref.png") objgraph.show_backrefs( [a], filename= " /Users/huangjiancong/Desktop/Python3Demo/Python核心技术与实践/backref.png")
''' 容器对象的引用可能构成很复杂的拓扑结构。 我们可以用objgraph包来绘制其引用关系。 objgraph是Python的一个第三方包。安装之前需要安装xdot。 objgraph官网: http://mg.pov.lt/objgraph/ https://pypi.python.org/pypi/objgraph ''' x = [1, 2, 3] y = [x, dict(key1=x)] z = [y, (x, y)] import objgraph objgraph.show_refs([z], filename='ref_topo.png') #两个对象可能相互引用,从而构成所谓的引用环(reference cycle)。 a = [] b = [a] a.append(b) #即使是一个对象,只需要自己引用自己,也能构成引用环。 a = [] a.append(a) print(getrefcount(a))
"""Grafické zobrazení referencí.""" import objgraph x = "Foo" y = x z = y # zobrazení referencí vedoucích až na řetězec "Foo" objgraph.show_refs(z, filename="objgraph1.png")