def clear(self): """ Removes all modes from the editor. All modes are removed from list and deleted. """ import sys while len(self._modes): key = sorted(list(self._modes.keys()))[0] mode = self.remove(key) refcount = sys.getrefcount(mode) if refcount > 2: try: import objgraph except ImportError: _logger().warning( 'potential memory leak detected on mode %r...\n' 'Install the objgraph package to know what objects are' ' holding references the mode.' % mode) else: _logger().warning( 'potential memory leak detected on mode: %r.\n' 'see stderr for a backrefs dot graph...' % mode) objgraph.show_backrefs([mode], output=sys.stderr) del mode
def pytest_unconfigure(config): os.environ.pop('GLUE_TESTING') # Reset configuration directory to original one from glue import config config.CFG_DIR = CFG_DIR_ORIG # Remove reference to QApplication to prevent segmentation fault on PySide try: from glue.utils.qt import app app.qapp = None except Exception: # for when we run the tests without the qt directories # Note that we catch any exception, not just ImportError, because # QtPy can raise a PythonQtError. pass if OBJGRAPH_INSTALLED and not ON_APPVEYOR: # Make sure there are no lingering references to GlueApplication obj = objgraph.by_type('GlueApplication') if len(obj) > 0: objgraph.show_backrefs(objgraph.by_type('GlueApplication')) warnings.warn( 'There are {0} remaining references to GlueApplication'.format( len(obj)))
def __init__(self): global first_scene global old_sprite spyral.Scene.__init__(self, SIZE) self.background = spyral.Image(size=SIZE).fill(BG_COLOR) first_scene = self v_top = spyral.View(self) v_bottom = spyral.View(v_top) over = spyral.Sprite(v_bottom) over.image = spyral.Image(size=(50, 50)).fill((255, 0, 0)) over.should_be_dead = lambda: 10 old_sprite = over self.khan = over.should_be_dead spyral.event.register("system.quit", sys.exit) spyral.event.register("input.keyboard.down.k", over.should_be_dead) spyral.event.register("input.keyboard.down.e", over._get_mask) spyral.event.register("input.keyboard.down.j", self.advance) objgraph.show_backrefs( [old_sprite], filename="sprite-alive.png", filter=lambda x: not isinstance(x, types.FrameType), extra_ignore=[id(locals()), id(globals())], max_depth=7, )
def clear(self): """ Removes all panel from the editor. """ import sys for i in range(4): while len(self._panels[i]): key = sorted(list(self._panels[i].keys()))[0] panel = self.remove(key) panel.setParent(None) panel.deleteLater() refcount = sys.getrefcount(panel) if refcount > 2: try: import objgraph except ImportError: _logger().warning( 'potential memory leak detected on panel: %r\n' 'Install the objgraph package to know what objects' ' are holding references the panel...', panel) else: _logger().warning( 'potential memory leak detected on panel: %r\n' 'see stderr for a backrefs dot graph...', panel) objgraph.show_backrefs([panel], output=sys.stderr) del panel
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 analyzeRefs(obj): import gc refs = gc.get_referrers(obj) print refs import objgraph import time objgraph.show_backrefs(obj, filename='Refs' + str(time.time()) + '.png', extra_ignore=[id(refs), id(objgraph)], refcounts=True, max_depth=15)
def app_list(): apps = [] yield apps gc.collect() alive_apps = [] for i, (app, request) in enumerate(apps[1:-1]): app = app() request = request() if request is None: request = '<dead request>' if app is not None: alive_apps.append((app, request)) logging.error('Memory leak: failed to release app for test ' + repr(request)) import objgraph objgraph.show_backrefs([app], filename=r'E:\backrefs{}.png'.format(i), max_depth=50, too_many=1) # objgraph.show_chain( # objgraph.find_backref_chain( # last_app(), objgraph.is_proper_module), # filename=r'E:\chain.png') assert not len(alive_apps), 'Memory leak: failed to release all apps'
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 func(): a = [1, 2, 3, 4] b = [1, 2, 3, 4] a.append(b) b.append(a) objgraph.show_refs([a]) objgraph.show_backrefs([a])
def log_mem_usage(signum, frame, fname=None): global _count _count += 1 gc.collect() if not fname: fname = filename + '_memory_%02d.log' % _count with open(fname, 'wb') as f: f.write('gc.garbage: %d\n\n' % len(gc.garbage)) objgraph.show_most_common_types(limit=50, file=f) f.write('\n\n') buf = StringIO() objgraph.show_growth(limit=50, file=buf) buf = buf.getvalue() f.write(buf) if _count < 2: return for tn, l in enumerate(buf.splitlines()[:10]): l = l.strip() if not l: continue type_ = l.split()[0] objects = objgraph.by_type(type_) objects = random.sample(objects, min(50, len(objects))) objgraph.show_chain( objgraph.find_backref_chain( objects[0], objgraph.is_proper_module), filename=fname[:-4] + '_type_%02d_backref.png' % tn ) objgraph.show_backrefs( objects, max_depth=5, extra_info=lambda x: hex(id(x)), filename=fname[:-4] + '_type_%02d_backrefs.png' % tn, )
def objgraph(): import objgraph, gc gc.set_debug(gc.DEBUG_UNCOLLECTABLE) run() gc.collect() objgraph.show_backrefs([a for a in gc.garbage if hasattr(a, '__del__')], filename='%s.png' % __file__[:-3])
def on_tear_down_class(cls): gc.collect() tr.print_diff() objects = gc.get_objects() counter = 0 with open(cls.tempdir + "/python_objects.txt", "w") as f: interesting = [ o for o in objects if isinstance(o, (VppPapiProvider, vpp_papi.VPP)) ] del objects gc.collect() for o in interesting: objgraph.show_backrefs([o], max_depth=5, filename="%s/%s.png" % (cls.tempdir, counter)) counter += 1 refs = gc.get_referrers(o) pp = pprint.PrettyPrinter(indent=2) f.write("%s\n" % pp.pformat(o)) for r in refs: try: f.write("%s\n" % pp.pformat(r)) except: f.write("%s\n" % type(r))
def check_all(cls, scope, names): gc.collect() cls.all_checks.update((scope, name) for name in names) # We expect three refs: 1) Booger.all, 2) the b local, # and 3) the argument to sys.getrefcount. OK_REF_COUNT = 3 # The things we expect to be referring to boogers. us = [cls.all, inspect.currentframe()] for b in cls.all: if b.reported: continue if b.scope != scope: continue if b.name not in names: continue if sys.getrefcount(b) > OK_REF_COUNT: # Show the unexpected referrers. referrers = [r for r in gc.get_referrers(b) if r not in us] print("** {!r} unpicked, with these referrers:".format(b), file=sys.stderr) pprint.pprint(referrers, stream=sys.stderr) objgraph.show_backrefs( [b], filename="booger_{0:x}.png".format(id(b)), extra_ignore=map(id, us), max_depth=10, too_many=100, ) b.reported = True
def teardown_method(self, method): if self.viewer is not None: self.viewer.close() self.application.close() # Matplotlib 3.5 introduced a memory leak when resizing the viewer # in https://github.com/matplotlib/matplotlib/pull/19255 so for now # we skip the affected test for the objgraph testing if method.__name__ == 'test_aspect_resize': return # The following seems to fail on Python 3.10 - to be investigated if sys.version_info[:2] >= (3, 10) and method.__name__ == 'test_session_round_trip': return # The following is a check to make sure that once the viewer and # application have been closed, there are no leftover references to # the data viewer. This was introduced because there were previously # circular references that meant that viewer instances were not # properly garbage collected, which in turn meant they still reacted # in some cases to events. if OBJGRAPH_INSTALLED: self.viewer = None self.application = None if self.viewer_count > self.viewer_count_start: objgraph.show_backrefs(objgraph.by_type(self.viewer_cls.__name__)) raise ValueError("No net viewers should be created in tests")
def log_mem_usage(signum, frame, fname=None): global _count _count += 1 gc.collect() if not fname: fname = filename + '_memory_%02d.log' % _count with open(fname, 'wb') as f: f.write('gc.garbage: %d\n\n' % len(gc.garbage)) objgraph.show_most_common_types(limit=50, file=f) f.write('\n\n') buf = StringIO() objgraph.show_growth(limit=50, file=buf) buf = buf.getvalue() f.write(buf) if _count < 2: return for tn, l in enumerate(buf.splitlines()[:10]): l = l.strip() if not l: continue type_ = l.split()[0] objects = objgraph.by_type(type_) objects = random.sample(objects, min(50, len(objects))) objgraph.show_chain( objgraph.find_backref_chain(objects[0], objgraph.is_proper_module), filename=fname[:-4] + '_type_%02d_backref.png' % tn) objgraph.show_backrefs( objects, max_depth=5, extra_info=lambda x: hex(id(x)), filename=fname[:-4] + '_type_%02d_backrefs.png' % tn, )
def __reduce__(self): if objgraph: database.session.expire_all() objgraph.show_backrefs([self], max_depth=5) raise pickle.PicklingError( 'This object is not picklable: {!r}'.format(self))
def fail_obj_graph(Klass): """ Fail is a given class _instances weakset is non empty and print the object graph. """ try: import objgraph except ImportError: return if not len(Klass._instances) == 0: global COUNTER COUNTER += 1 import gc gc.collect() objgraph.show_backrefs( list(Klass._instances), max_depth=20, filename=f'{Klass.__name__}-leak-backref-graph-{COUNTER}.pdf', ) # DO not remove len, this can break as C++ obj are gone, but python objects # still hang around and _repr_ would crash. assert False, len(Klass._instances)
def object_list(cls, start_response): status = '200 OK' for k, objects in group_by_class().iteritems(): if k == cls: break else: headers = [('Content-type', 'text/html')] start_response(status, headers) return ['There are no any objects of class %s, try to refresh page later' ' or go to <a href="%s">index</a>.' % ( cls, '/')] if RETURN_DOT_FILE: objgraph.show_backrefs(objects, filename="/tmp/objgraph.dot") headers = [('Content-type', 'text/vnd.graphviz'), ('Content-Disposition', 'attachment; filename=objgraph.dot')] start_response(status, headers) return open('/tmp/objgraph.dot') objgraph.show_backrefs(objects) status = '302 Moved temporarily' headers = [('Location', '/')] start_response(status, headers) return ''
def generate_object_graphs_by_class(classlist): """ Generate reference and backreference graphs for objects of type class for each class given in classlist. Useful for debugging reference leaks in framework etc. Usage example to generate graphs for class "someclass": >>> import someclass >>> someclassobject = someclass() >>> generate_object_graphs_by_class(someclass) Needs "objgraph" module installed. """ try: import objgraph import gc except ImportError: return graphcount = 0 if not isinstance(classlist, list): classlist = [classlist] for class_item in classlist: for obj in gc.get_objects(): if isinstance(obj, class_item): graphcount += 1 objgraph.show_refs( [obj], filename='%d_%s_%d_refs.png' % (ogcounter, obj.__class__.__name__, graphcount)) objgraph.show_backrefs( [obj], filename='%d_%s_%d_backrefs.png' % (ogcounter, obj.__class__.__name__, graphcount))
def cycle_ref(): x = [] y = [x, [x], dict(x=x)] objgraph.show_refs([y], filename='smaple-graph.png') objgraph.show_backrefs([x], filename='sample-backref-graph.png') objgraph.show_most_common_types()
def show_backref(target, max_depth=3): """Returns a dot graph of all the objects that are referencing the target. A object referencing graph is useful to debug memory leak like circular reference. objgraph provides a good visualization of the memory graph than most python built-in utilities like gc.get_referrers(), which are not human-readable sometimes. The dot graph will be written to a string IO object, and can be rendered with graphviz in operating system. E.g. dot -Tpng {$dot_graph} -o output.png Args: target: The target object for the memory graph. max_depth: The maximum depth of the graph. By default 3 layers of references are used. Increases this a lot may result in the graph growing too big. Returns: A string that contains the object reference graph. Raises: NotImplementedError: if objgraph is not installed. """ if objgraph is None: raise NotImplementedError("objgraph is not installed.") string_io = io.StringIO() objgraph.show_backrefs(target, max_depth=max_depth, output=string_io) graph = string_io.getvalue() string_io.close() return graph
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 show_backrefs_by_type(t): o = objgraph.by_type(t) if len(o) == 0: return False obj = o[random.randint(0, len(o) - 1)] objgraph.show_backrefs([obj], max_depth=10) return True
def __init__(self): global first_scene global old_sprite spyral.Scene.__init__(self, SIZE) self.background = spyral.Image(size=SIZE).fill(BG_COLOR) first_scene = self v_top = spyral.View(self) v_bottom = spyral.View(v_top) over = spyral.Sprite(v_bottom) over.image = spyral.Image(size=(50, 50)).fill((255, 0, 0)) over.should_be_dead = lambda: 10 old_sprite = over self.khan = over.should_be_dead spyral.event.register("system.quit", spyral.director.quit) spyral.event.register("input.keyboard.down.k", over.should_be_dead) spyral.event.register("input.keyboard.down.e", over._get_mask) spyral.event.register("input.keyboard.down.j", self.advance) objgraph.show_backrefs( [old_sprite], filename='sprite-alive.png', filter=lambda x: not isinstance(x, types.FrameType), extra_ignore=[id(locals()), id(globals())], max_depth=7)
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 test_memory_leak_in_update(sas: StockAnalysisSystem): sas = StockAnalysisSystem() data_hub = sas.get_data_hub_entry() data_center = data_hub.get_data_center() data_utility = data_hub.get_data_utility() # data_center.update_local_data('Market.SecuritiesInfo', force=False) # data_center.update_local_data('Market.NamingHistory') # data_center.update_local_data('Market.TradeCalender') # stock_list = data_utility.get_stock_list() # data_center.update_local_data('Finance.Audit', '600000.SSE', force=True) # data_center.update_local_data('Finance.Audit', '600036.SSE', force=True) # # data_center.update_local_data('Finance.BalanceSheet', '600000.SSE', force=True) # data_center.update_local_data('Finance.BalanceSheet', '600036.SSE', force=True) # # data_center.update_local_data('Finance.IncomeStatement', '600000.SSE', force=True) # data_center.update_local_data('Finance.IncomeStatement', '600036.SSE', force=True) # # data_center.update_local_data('Finance.CashFlowStatement', '600000.SSE', force=True) # data_center.update_local_data('Finance.CashFlowStatement', '600036.SSE', force=True) data_hub = sas.get_data_hub_entry() data_center = data_hub.get_data_center() data_utility = data_hub.get_data_utility() stock_list = data_utility.get_stock_list() counter = 0 for stock_identity, name in stock_list: counter += 1 data_center.update_local_data('Finance.Audit', stock_identity, force=True) data_center.update_local_data('Finance.BalanceSheet', stock_identity, force=True) data_center.update_local_data('Finance.IncomeStatement', stock_identity, force=True) data_center.update_local_data('Finance.CashFlowStatement', stock_identity, force=True) data_center.update_local_data('Stockholder.PledgeStatus', stock_identity, force=True) data_center.update_local_data('Stockholder.PledgeHistory', stock_identity, force=True) if counter > 100: break print(gc.collect()) objs = objgraph.by_type('OBJ') if len(objs) > 0: objgraph.show_backrefs(objs[0], max_depth=10, filename='obj.dot') return True
def runWithObjgraph(cls, tests=None): import objgraph gc.set_debug(gc.DEBUG_UNCOLLECTABLE) cls.runTests(tests) gc.collect() objgraph.show_backrefs( [a for a in gc.garbage if hasattr(a, '__del__')], filename='%s.png' % __file__[:-3])
def check_first(self): global first_scene global old_sprite #first_scene.clear_all_events() gc.collect() objgraph.show_backrefs([old_sprite], filename='sprite-old.png', filter= lambda x: not isinstance(x, types.FrameType), extra_ignore = [id(locals()), id(globals())], max_depth=7) old_sprite.kill() objgraph.show_backrefs([old_sprite], filename='sprite-dead.png', filter= lambda x: not isinstance(x, types.FrameType), extra_ignore = [id(locals()), id(globals())], max_depth=7)
def print_proxies(typename_part, depth): labels = [ x for x in objgraph.by_type('Reloadable') if typename_part in str(x) ] if len(labels) > 0: objgraph.show_backrefs(labels, max_depth=depth, filename=os.path.join(os.getcwd(), 'graph.png'))
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 __reduce__(self): if objgraph: database.session.expire_all() objgraph.show_backrefs([self], max_depth=5) raise pickle.PicklingError( 'This object is not picklable: {!r}'.format(self) )
def _check(type_name): """Utility function to debug references""" import objgraph objects = objgraph.by_type(type_name) if objects: obj = objects[0] objgraph.show_backrefs(obj, max_depth=3, filename='graph.png')
def gc_debug_backtraces(magictype=None, count=1): all_objects = list(objgraph.by_type(magictype)) sio = StringIO.StringIO() objgraph.show_backrefs(all_objects[:count], max_depth=10, shortnames=False, output=sio) #objgraph.show_chain(*chains, output=sio2) logging.info("%s", sio.getvalue())
def dump_objgrpah(objgraph_file): new_ids = objgraph.get_new_ids() new_ids_list = new_ids['list'] new_objs = objgraph.at_addrs(new_ids_list) objgraph.show_backrefs(new_objs, highlight=inspect.isclass, refcounts=True, filename=objgraph_file) new_ids = objgraph.get_new_ids()
def _drawgraph(self): objgraph.show_backrefs( [self.root], max_depth=5, filter=lambda x: type(x) not in [list, tuple, set], highlight=lambda x: type(x) in [ConfigurableComponent], filename="backref-graph.png", ) self.log("Backref graph written.", lvl=critical)
def object_backref(obj, max_depth=10, backref_file="object_backref.dot"): print("========object backref========") trace_objects = objgraph.by_type(obj) try: objgraph.show_backrefs(trace_objects[0], max_depth, filename=backref_file) except IndexError: pass
def func(): show_memory_info('initial') a = [i for i in range(10000000)] b = [i for i in range(10000000)] show_memory_info('after, a,b created') a.append(b) b.append(a) # objgraph.show_refs([a]) objgraph.show_backrefs([a])
def objgraph(state, num=200, max_depth=10): # pragma: no cover try: import objgraph except ImportError: raise ImportError("Requires the objgraph library") with tempfile.NamedTemporaryFile(prefix="cobjg", suffix=".png", delete=False) as fh: objects = objgraph.by_type("Request")[:num] objgraph.show_backrefs(objects, max_depth=max_depth, highlight=lambda v: v in objects, filename=fh.name) return {"filename": fh.name}
def main(): try: raise Exception('here') except: pass # (exceptionType , value, traceback ) exc_info = sys.exc_info() import objgraph objgraph.show_backrefs([exc_info, main])
def showBackrefs(self): objType = str(self.inputWidget.text()) with self.showTempImage() as fn: objects = objgraph.by_type(objType) if len(objects) == 0: return objgraph.show_backrefs(objects[0], max_depth=self.depthLimitBox.value(), extra_ignore=(id(gc.garbage),id(objects)), too_many=self.widthLimitBox.value(), filename=fn)
def run_objgraph(types): import objgraph import os import random objgraph.show_most_common_types(limit=50, shortnames=False) for type_ in types: count = objgraph.count(type_) print '%s objects: %d' % (type_, count) if count: objgraph.show_backrefs( random.choice(objgraph.by_type(type_)), max_depth=20, filename='/tmp/backrefs_%s_%d.dot' % (type_, os.getpid()))
def dump_memory_usage(): """ This is a function that prints the memory usage of w3af in real time. :author: Andres Riancho ([email protected]) """ if not DEBUG_MEMORY: return else: if DEBUG_REFERENCES: print 'Object References:' print '==================' interesting = ['tuple', 'dict', 'list'] for interesting_klass in interesting: interesting_instances = objgraph.by_type(interesting_klass) sample = random.sample(interesting_instances, min( SAMPLE_LEN, len(interesting_instances))) for s in sample: fmt = 'memory-refs/%s-backref-graph-%s.png' fname = fmt % (interesting_klass, id(s)) ignores = [id(interesting_instances), id(s), id(sample)] ignores.extend([id(v) for v in locals().values()]) ignores.extend([id(v) for v in globals().values()]) ignores.append(id(locals())) ignores.append(id(globals())) try: objgraph.show_backrefs(s, highlight=inspect.isclass, extra_ignore=ignores, filename=fname, extra_info=_extra_info) except: pass print print 'Most common:' print '============' objgraph.show_most_common_types() print print 'Memory delta:' print '=============' objgraph.show_growth(limit=25) sorted_cmds, shareds, _, _ = get_memory_usage(None, True, True, True) cmd = sorted_cmds[0] msg = "%8sB Private + %8sB Shared = %8sB" % (human(cmd[1] - shareds[cmd[0]]), human(shareds[cmd[0] ]), human(cmd[1]) ) print 'Total memory usage:', msg
def objgraph(panel, num=200, max_depth=10, ): try: import objgraph except ImportError: raise ImportError('Requires the objgraph library') with tempfile.NamedTemporaryFile(prefix='cobjg', suffix='.png', delete=False) as fh: objects = objgraph.by_type('Request')[:num] objgraph.show_backrefs( objects, max_depth=max_depth, highlight=lambda v: v in objects, filename=fh.name, ) return {'filename': fh.name}
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 show_cycles(sccs, joined=False): import objgraph a = sccs if joined: a = [] for scc in sccs: a.extend(scc) a = [a] for scc in a: objs = objgraph.at_addrs(scc) print(objgraph.typestats(objs)) objgraph.show_backrefs(objs, max_depth=len(scc) + 5, filter=lambda x: id(x) in scc)
def get(self): count = int(self.request.get('count', '20')) max_depth = int(self.request.get('max_depth', '10')) type_name = self.request.get('type') all_objects = list(objgraph.by_type(type_name)) # ignore the references from our all_objects container ignore = [ id(all_objects), id(sys._getframe(1)), id(sys._getframe(1).f_locals), ] sio = StringIO.StringIO() objgraph.show_backrefs(all_objects[:count], max_depth=max_depth, shortnames=False, extra_ignore=ignore, output=sio) self.response.headers["Content-Type"] = "text/plain" self.response.out.write(sio.getvalue())
def objgraph(state, num=200, max_depth=10, type='Request'): # pragma: no cover try: import objgraph except ImportError: raise ImportError('Requires the objgraph library') print('Dumping graph for type %r' % (type, )) with tempfile.NamedTemporaryFile(prefix='cobjg', suffix='.png', delete=False) as fh: objects = objgraph.by_type(type)[:num] objgraph.show_backrefs( objects, max_depth=max_depth, highlight=lambda v: v in objects, filename=fh.name, ) return {'filename': fh.name}
def wipe(self, signum, frame): logger.info('caulk: Received signal %s, wiping memory' % signum) gc.collect() logger.info('caulk: Wipe complete.') filename = self.get_filename('wipe') logger.info('caulk: Dumping %s uncollectable object(s) to %s' % (len(gc.garbage), filename)) with file(filename, 'w') as f: dump(f, gc.garbage) if objgraph: filename = self.get_filename('dot') objs = random.sample(gc.garbage, 5) try: objgraph.show_backrefs(objs, filename=filename) except: logger.error('caulk: error dumping graphs', exc_info=sys.exc_info()) logger.info('caulk: Dumping 5 random object graphs to %s' % filename) logger.info('caulk: Dump complete')
def getGarbageGraph(): try: import gc except ImportError: message = 'Could not import gc module; garbage collection support is not compiled in' return message leakCount = 0 gcDebugEnable() leakCount = gc.collect() copy = gc.garbage[:] del gc.garbage[:] if len(copy) > 0: garbageGraph = StringIO.StringIO() objgraph.show_backrefs(copy, max_depth=10, output=garbageGraph) return garbageGraph.getvalue() return 'Nothing to collect'
def objgraph(state, num=200, max_depth=10, type="Request"): # pragma: no cover """Create graph of uncollected objects (memory-leak debugging). Arguments: num (int): Max number of objects to graph. max_depth (int): Traverse at most n levels deep. type (str): Name of object to graph. Default is ``"Request"``. """ try: import objgraph as _objgraph except ImportError: raise ImportError("Requires the objgraph library") logger.info("Dumping graph for type %r", type) with tempfile.NamedTemporaryFile(prefix="cobjg", suffix=".png", delete=False) as fh: objects = _objgraph.by_type(type)[:num] _objgraph.show_backrefs(objects, max_depth=max_depth, highlight=lambda v: v in objects, filename=fh.name) return {"filename": fh.name}
def back_ref(): import objgraph gc.collect() #classtypes = ('TextView', 'Document', 'RangeInfo', 'SourceBuffer', 'FileDocSource', 'DocumentView') classtypes = ('TopContainer', 'Workspace', 'SplittedContainer', 'NotebookContainer', 'DocumentView') #i = 0 #for o in objgraph.by_type(classtype): # l = objgraph.find_backref_chain(o, objgraph.is_proper_module) # objgraph.show_chain(l, filename="backref%d.png"%i) # i += 1 #print "test |%s|"% classtype #count() list_ = [] for type_ in classtypes: list_.extend(objgraph.by_type(type_)) #print list_ objgraph.show_backrefs(list_, max_depth=3, too_many=100, filename="backref.png")
def cleanup_gc(self): gc.collect() leaked = [obj for obj in gc.get_objects() + gc.garbage if isinstance(obj, (qubes.Qubes, qubes.vm.BaseVM, libvirt.virConnect, libvirt.virDomain))] if leaked: try: import objgraph objgraph.show_backrefs(leaked, max_depth=15, extra_info=extra_info, filename='/tmp/objgraph-{}.png'.format(self.id())) except ImportError: pass assert not leaked
def teardown_method(self, method): if self.viewer is not None: self.viewer.close() self.application.close() # The following is a check to make sure that once the viewer and # application have been closed, there are no leftover references to # the data viewer. This was introduced because there were previously # circular references that meant that viewer instances were not # properly garbage collected, which in turn meant they still reacted # in some cases to events. if OBJGRAPH_INSTALLED: self.viewer = None self.application = None if self.viewer_count > self.viewer_count_start: objgraph.show_backrefs(objgraph.by_type(self.viewer_cls.__name__)) raise ValueError("No net viewers should be created in tests")
def object_list(request, cls, format): for k, objects in group_by_class().iteritems(): if k == cls: break else: return HttpResponse('There are no any objects of class %s, try to refresh page later' ' or go to <a href="%s">index</a>.' % ( cls, reverse('drainhunter-index'))) objgraph.show_backrefs(objects, filename="/tmp/objgraph.dot") if format == 'dot': response = HttpResponse(open('/tmp/objgraph.dot'), mimetype='text/vnd.graphviz') response['Content-Disposition'] = 'attachment; filename=objgraph.dot' return response elif format == 'png': return HttpResponse(open('/tmp/objgraph.png'), mimetype='image/png') convert_to_svg("/tmp/objgraph.dot", "/tmp/objgraph.svg") return HttpResponse(open('/tmp/objgraph.svg'), mimetype='image/svg+xml')
def __init__(self): spyral.Scene.__init__(self, SIZE) i = spyral.Image(size=(50,50)) i.fill((255, 255, 255)) v_top = spyral.View(self) v_bottom = spyral.View(v_top) import objgraph over = spyral.Sprite(v_bottom) self.pos = (0, 0) over.image = i #over.kill() v_top.kill() del over objgraph.show_backrefs([v_top], filename='sample-graph.png') self.register("system.quit", sys.exit)