def get_trace(client, force_start): (started, path) = client.tracemalloc_dump() if force_start and not started: client.tracemalloc_toggle() (started, path) = client.tracemalloc_dump() if not started: raise TraceCantStart elif not started: raise TraceNotStarted return Snapshot.load(path)
def analyze(self): output = [] for i, tracedump in enumerate(self.get_input_files('*.tracemalloc')): logging.debug('Analyzing "%s" tracemalloc dump' % tracedump) try: snapshot = Snapshot.load(tracedump) snapshot = filter_traces(snapshot) except Exception, e: logging.error('Exception in PyTraceMallocSummary: "%s"' % e) continue top = format_top(snapshot) output.append(('Measurement #%s' % i, top)) trace = format_tracebacks(snapshot) output.append(('Traceback #%s' % i, trace))
print("Top %s lines" % limit) for index, stat in enumerate(top_stats[:limit], 1): frame = stat.traceback[0] print("#%s: %s:%s: %.1f KiB" % (index, frame.filename, frame.lineno, stat.size / 1024)) line = linecache.getline(frame.filename, frame.lineno).strip() if line: print(' %s' % line) other = top_stats[limit:] if other: size = sum(stat.size for stat in other) print("%s other: %.1f KiB" % (len(other), size / 1024)) total = sum(stat.size for stat in top_stats) print("Total allocated size: %.1f KiB" % (total / 1024)) tracemalloc.start(nframe) s = Snapshot.load('/Users/jx/Downloads/tracemalloc_2.log') top_stats = s.statistics('traceback') # pick the biggest memory block stat = top_stats[0] print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024)) for line in stat.traceback.format(): print(line) display_top(s, limit=1000)