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 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 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 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 trace_memory_stop(self): """ Stops measuring memory consumption """ self.trace_memory_clean_caches() objgraph.show_growth(limit=30) trace_type = context.get_current_config()["trace_memory_type"] if trace_type: filename = '%s/%s-%s.png' % (context.get_current_config( )["trace_memory_output_dir"], trace_type, self.id) chain = objgraph.find_backref_chain( random.choice(objgraph.by_type(trace_type)), objgraph.is_proper_module) objgraph.show_chain(chain, filename=filename) del filename del chain gc.collect() self._memory_stop = self.worker.get_memory()["total"] diff = self._memory_stop - self._memory_start context.log.debug("Memory diff for job %s : %s" % (self.id, diff)) # We need to update it later than the results, we need them off memory # already. self.collection.update({"_id": self.id}, {"$set": { "memory_diff": diff }}, w=1)
def show_backref_from_coro(): import random objgraph.show_chain( objgraph.find_backref_chain( random.choice(objgraph.by_type('coroutine')), objgraph.is_proper_module), filename='chain-to-leaked.png')
def print_objects(f): gc.collect() with open(f, 'r') as fd: for line in fd: line = line.strip() try: obj = random.choice(objgraph.by_type(line)) except Exception as e: LOGGER.info('exception trying to objgraph a random %s: %s', line, str(e)) break with tempfile.NamedTemporaryFile(dir='/tmp', prefix=line, suffix='.dot', mode='w') as out: try: objgraph.show_chain(objgraph.find_backref_chain( obj, objgraph.is_proper_module), output=out) LOGGER.info('object %s file %s', line, out.name) except Exception as e: LOGGER.info( 'exception trying to show_chain a random %s: %s', line, str(e)) try: os.remove(f) except Exception as e: LOGGER.info('exception %s removing memory_crawler file %s', str(e), f)
def trace_memory_stop(self): """ Stops measuring memory consumption """ objgraph.show_growth(limit=10) trace_type = get_current_config()["trace_memory_type"] if trace_type: objgraph.show_chain( objgraph.find_backref_chain( random.choice(objgraph.by_type(trace_type)), objgraph.is_proper_module ), filename='%s/%s-%s.png' % (get_current_config()["trace_memory_output_dir"], trace_type, self.id) ) gc.collect() self._memory_stop = self.worker.get_memory() diff = self._memory_stop - self._memory_start log.debug("Memory diff for job %s : %s" % (self.id, diff)) # We need to update it later than the results, we need them off memory already. self.collection.update({ "_id": self.id }, {"$set": { "memory_diff": diff }}, w=1)
def run_last(): run_once("last") objgraph.show_chain(objgraph.find_backref_chain( random.choice(objgraph.by_type('BoundField')), objgraph.is_proper_module), filename=r'c:\temp\chain.png') print("DONE LAST")
def show_memory_refs(name): try: obj=objgraph.by_type(name)[0] except IndexError: print 'no object of type',name return objgraph.show_chain( objgraph.find_backref_chain(obj, inspect.ismodule), filename='chain-{}.png'.format(name))
def run_last(): run_once("last") objgraph.show_chain( objgraph.find_backref_chain( random.choice(objgraph.by_type('BoundField')), objgraph.is_proper_module), filename=r'c:\temp\chain.png') print( "DONE LAST")
def _render_ref_graph(self, objs): io = StringIO() objgraph.show_chain(objs, output=io) dot = io.getvalue() png_image = self._run_dot(dot) return Response(png_image, mimetype='image/png')
def show_chain_of_leaked(): # LeakedOne must be leaked for this to work import random func_that_will_leak() objgraph.show_chain(objgraph.find_backref_chain( random.choice(objgraph.by_type('LeakedOne')), objgraph.is_proper_module), filename='chain-to-leaked.png')
def show_chain(self, obj_type): # obj_type: Myobj_Type, type:string ref_chain = objgraph.find_backref_chain( random.choice(objgraph.by_type(obj_type)), objgraph.is_proper_module, max_depth=5) objgraph.show_chain(ref_chain, filename='chain.dot') cmd = "dot -Tpng chain.dot -o chain.png" commands.getstatusoutput(cmd)
def process_response(self, request, response): objgraph.show_growth(limit=100) track_class_name = getattr(settings, 'ASPARAGUS_TRACK_CLASS', None) if track_class_name: objgraph.show_chain(objgraph.find_backref_chain( objgraph.by_type(track_class_name)[-1], objgraph.is_proper_module), filename='leaks.png') return response
def graph_randobj(line): import objgraph args = shlex.split(line) if len(args) > 1: fname = args[1] else: fname = 'chain.png' print 'Getting random %s object...' % args[0] obj = random.choice(objgraph.by_type(args[0])) print 'Creating chain...' chain = objgraph.find_backref_chain(obj, objgraph.is_proper_module) print 'Saving chain...' objgraph.show_chain(chain, filename=fname)
def gc_debug_backtraces(magictype=None, count=1): sio = StringIO.StringIO() orig_out = sys.stdout try: sys.stdout = sio all_objects = list(objgraph.by_type(magictype)) random.shuffle(all_objects) chains = [] for obj in all_objects[:count]: chains.append( objgraph.find_backref_chain(obj, objgraph.is_proper_module)) sio2 = StringIO.StringIO() objgraph.show_chain(*chains, output=sio2) logging.info("%s", sio2.getvalue()) finally: sys.stdout = orig_out
def dump_memory(): now = time() - start if PROFILE_MEMORY_GRAPH_BACKREF_TYPES: for type_ in types: for sample_number in xrange( PROFILE_MEMORY_GRAPH_BACKREF_AMOUNT): objects = objgraph.by_type(type_) if objects: objgraph.show_chain(objgraph.find_backref_chain( random.choice(objects), objgraph.is_proper_module), filename=objgraph_out_file % (type_, now, sample_number)) else: logger.error("No objects of type %s found!", type_) scanner.dump_all_objects(meliae_out_file % now)
def check_memory(): logger.debug('Checking memory.') logger.debug(objgraph.by_type('SpiderTask')) logger.debug(objgraph.by_type('TaskExecutor')) logger.debug(objgraph.by_type('Future')) logger.debug(objgraph.by_type('PeriodicCallback')) logger.debug(objgraph.by_type('Workspace')) logger.debug(objgraph.by_type('MultipartRequestBodyProducer')) logger.debug(objgraph.by_type('HTTPRequest')) future_objs = objgraph.by_type('Future') if future_objs: objgraph.show_chain( objgraph.find_backref_chain(future_objs[-1], objgraph.is_proper_module), filename='chain.png' ) all_objects = muppy.get_objects() sum1 = summary.summarize(all_objects) # Prints out a summary of the large objects summary.print_(sum1)
def print_objects(f): gc.collect() with open(f, 'r') as fd: for line in fd: line = line.strip() try: obj = random.choice(objgraph.by_type(line)) except Exception as e: LOGGER.info('exception trying to objgraph a random %s: %s', line, str(e)) break with tempfile.NamedTemporaryFile(dir='/tmp', prefix=line, suffix='.dot', mode='w') as out: try: objgraph.show_chain(objgraph.find_backref_chain(obj, objgraph.is_proper_module), output=out) LOGGER.info('object %s file %s', line, out.name) except Exception as e: LOGGER.info('exception trying to show_chain a random %s: %s', line, str(e)) try: os.remove(f) except Exception as e: LOGGER.info('exception %s removing memory_crawler file %s', str(e), f)
def graphMemProfile(func, args, kw_args): import objgraph # objgraph.show_most_common_types() objgraph.show_growth() func(*args, **kw_args) objgraph.show_growth() import random str( objgraph.show_chain(objgraph.find_backref_chain( random.choice(objgraph.by_type('staticmethod')), objgraph.is_proper_module), filename='chain.png')) #objgraph.show_refs([func(*args,**kw_args)], filename='refs.png') #objgraph.show_backrefs([func(*args,**kw_args)], filename='backrefs.png') #import pdb; pdb.set_trace() objgraph.show_growth()
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #=============================================================================== #============= enthought library imports ======================= #============= standard library imports ======================== #============= local library imports ========================== import os path = os.environ['PATH'] os.environ['PATH'] = '{}:/usr/local/bin'.format(path) import objgraph import random import inspect d = dict(a=1, b='2', c=3.0) objgraph.show_chain( objgraph.find_backref_chain( random.choice(objgraph.by_type('dict')), inspect.ismodule), filename='chain.png') #============= EOF =============================================
# Notes on debugging memory leaks in the worker # apt-get update && apt-get install graphviz # pip install objgraph import objgraph import gc import random from aleph.logic.documents import process_documents objgraph.show_most_common_types() process_documents(21) # objgraph.show_most_common_types() objgraph.show_chain(objgraph.find_backref_chain(random.choice(objgraph.by_type('_DebugQueryTuple')), objgraph.is_proper_module), filename='chain.png') # noqa gc.collect()
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=100, max_depth=5, 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') break del model if collect: print('Unreachable objects:', gc.collect()) asdf #del gc.garbage[:] #print('Unreachable objects:', gc.collect())
def show_chain(i, obj): objgraph.show_chain( objgraph.find_backref_chain(obj, inspect.ismodule), filename='chain.png')
import objgraph class Dict(dict): def __init__(self, args={}): dict.__init__(self, args) class List(list): def __init__(self, args=()): list.__init__(self, args) class MyClass: def __init__(self): self.a = [] d1 = Dict({1: 1}) d2 = Dict({2: 2}) l = List((1, 2, 3)) self.a.append(d1) self.a.append(d2) c = MyClass() print 'objgraph.by_type:', objgraph.by_type('Dict') chain = objgraph.find_backref_chain( objgraph.by_type('Dict')[-1], inspect.ismodule) objgraph.show_chain(chain, filename='chain.png')
def showBackrefs(self): objType = str(self.inputWidget.text()) with self.showTempImage() as fn: objgraph.show_chain(objgraph.find_backref_chain(objgraph.by_type(objType)[0], objgraph.is_proper_module), filename=fn)
# snapshot = tracemalloc.take_snapshot() # top_stats = snapshot.statistics('lineno') # for stat in top_stats[:5]: # print(stat) print("==========================================================") # lc.loger.info("disconnected") main = MainEngine() lc.loger.info("loginning") main.login() lc.loger.info("logined") i = i + 1 lc.loger.info(i) og.show_growth() og.show_chain( og.find_backref_chain( og.by_type('tuple'), og.is_proper_module ), filename='obj_chain.dot' ) # og.show_backrefs(og.by_type('tuple')[0], extra_ignore=(id(gc.garbage),), max_depth = 10, filename = 'tuple.dot') time.sleep(main.brunt_sub_settle*20) time.sleep(10) except: traceback.print_exc() raise Exception("a") # main.logout() # main.disconnect() app.exec_()
import objgraph as objg from ogpath import ogpath class MyBigFatObject(object): pass def comps(_cache={}): _cache[42] = dict(foo=MyBigFatObject(), bar=MyBigFatObject()) x = MyBigFatObject() objg.show_growth() comps() objg.show_growth() print 1, objg.by_type('MyBigFatObject') import random objg.show_chain(objg.find_backref_chain( random.choice(objg.by_type('MyBigFatObject')), objg.is_proper_module), filename=ogpath('chain.png'))
def show_chain(ob): objgraph.show_chain( objgraph.find_backref_chain(ob, objgraph.is_proper_module))
def show_chain(i, obj): objgraph.show_chain(objgraph.find_backref_chain(obj, inspect.ismodule), filename='chain.png')
# Notes on debugging memory leaks in the worker # apt-get update && apt-get install graphviz # pip install objgraph import objgraph import gc import random from aleph.logic.documents import process_documents objgraph.show_most_common_types() process_documents(21) # objgraph.show_most_common_types() objgraph.show_chain( objgraph.find_backref_chain( random.choice(objgraph.by_type("_DebugQueryTuple")), objgraph.is_proper_module), filename="chain.png", ) # noqa gc.collect()
_cache.remove(o) if __name__ == '__main__': print(type(func_to_leak)) print('counter OBJ:', objgraph.count('OBJ')) print('counter func_to_leak:', objgraph.count('function')) objgraph.show_growth() try: func_to_leak() except: pass print('after call func_to_leak') # 统计该类型对象的数目 print('counter OBJ:', objgraph.count('OBJ')) print('counter func_to_leak:', objgraph.count('function')) # 更具对象类型返回对象列表 print('999999999', objgraph.by_type('str')) # 打印实例化做多的前10个对象 print(objgraph.show_most_common_types(10)) # 统计自上次调用以来增加得最多的对象,这个函数非常有利于发现潜在的内存泄露 objgraph.show_growth() # 生产一张有关objs的引用图,看出看出对象为什么不释放,后面会利用这个API来查内存泄露 objgraph.show_backrefs(objgraph.by_type('OBJ')[0], max_depth=10, filename='obj.png') # 找到一条指向obj对象的最短路径,且路径的头部节点需要满足predicate函数 (返回值为True) objgraph.show_chain(objgraph.find_backref_chain( objgraph.by_type('OBJ')[0], objgraph.is_proper_module), filename='short_chain.png')