Python tracemalloc.take_snapshot() Examples
The following are 30
code examples of tracemalloc.take_snapshot().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tracemalloc
, or try the search function
.
Example #1
Source File: test_tracemalloc.py From android_universal with MIT License | 6 votes |
def test_snapshot(self): obj, source = allocate_bytes(123) # take a snapshot snapshot = tracemalloc.take_snapshot() # write on disk snapshot.dump(support.TESTFN) self.addCleanup(support.unlink, support.TESTFN) # load from disk snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) self.assertEqual(snapshot2.traces, snapshot.traces) # tracemalloc must be tracing memory allocations to take a snapshot tracemalloc.stop() with self.assertRaises(RuntimeError) as cm: tracemalloc.take_snapshot() self.assertEqual(str(cm.exception), "the tracemalloc module must be tracing memory " "allocations to take a snapshot")
Example #2
Source File: test_tracemalloc.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_snapshot(self): obj, source = allocate_bytes(123) # take a snapshot snapshot = tracemalloc.take_snapshot() # write on disk snapshot.dump(support.TESTFN) self.addCleanup(support.unlink, support.TESTFN) # load from disk snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) self.assertEqual(snapshot2.traces, snapshot.traces) # tracemalloc must be tracing memory allocations to take a snapshot tracemalloc.stop() with self.assertRaises(RuntimeError) as cm: tracemalloc.take_snapshot() self.assertEqual(str(cm.exception), "the tracemalloc module must be tracing memory " "allocations to take a snapshot")
Example #3
Source File: tools.py From sisyphus with Mozilla Public License 2.0 | 6 votes |
def snapshot(self): snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno') total = sum(stat.size for stat in top_stats) if abs(self.last_total - total) < self.min_change: return self.last_total = total self.log_stream.write("Top %s lines at %s\n" % (self.limit, time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))) for index, stat in enumerate(top_stats[:self.limit], 1): frame = stat.traceback[0] # replace "/path/to/module/file.py" with "module/file.py" filename = os.sep.join(frame.filename.split(os.sep)[-2:]) self.log_stream.write("#%s: %s:%s: %.1f KiB\n" % (index, filename, frame.lineno, stat.size / 1024)) line = linecache.getline(frame.filename, frame.lineno).strip() if line: self.log_stream.write(' %s\n' % line) other = top_stats[self.limit:] if other: size = sum(stat.size for stat in other) self.log_stream.write("%s other: %.1f KiB\n" % (len(other), size / 1024)) self.log_stream.write("Total allocated size: %.1f KiB\n\n" % (total / 1024)) self.log_stream.flush()
Example #4
Source File: test_tracemalloc.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_create_snapshot(self): raw_traces = [(5, (('a.py', 2),))] with contextlib.ExitStack() as stack: stack.enter_context(patch.object(tracemalloc, 'is_tracing', return_value=True)) stack.enter_context(patch.object(tracemalloc, 'get_traceback_limit', return_value=5)) stack.enter_context(patch.object(tracemalloc, '_get_traces', return_value=raw_traces)) snapshot = tracemalloc.take_snapshot() self.assertEqual(snapshot.traceback_limit, 5) self.assertEqual(len(snapshot.traces), 1) trace = snapshot.traces[0] self.assertEqual(trace.size, 5) self.assertEqual(len(trace.traceback), 1) self.assertEqual(trace.traceback[0].filename, 'a.py') self.assertEqual(trace.traceback[0].lineno, 2)
Example #5
Source File: benchmark.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test(self): timings = [] memory_usage = [] tracemalloc.start() for i in range(self.repeat): before_memory = tracemalloc.take_snapshot() start_time = time.time() self.bench() end_time = time.time() after_memory = tracemalloc.take_snapshot() timings.append(end_time - start_time) memory_usage.append(sum([t.size for t in after_memory.compare_to(before_memory, 'filename')])) print("time min:", min(timings), "max:", max(timings), "avg:", sum(timings) / len(timings)) # NOQA print("memory min:", min(memory_usage), "max:", max(memory_usage), "avg:", sum(memory_usage) / len(memory_usage)) # NOQA
Example #6
Source File: test_tracemalloc.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_create_snapshot(self): raw_traces = [(5, (('a.py', 2),))] with contextlib.ExitStack() as stack: stack.enter_context(patch.object(tracemalloc, 'is_tracing', return_value=True)) stack.enter_context(patch.object(tracemalloc, 'get_traceback_limit', return_value=5)) stack.enter_context(patch.object(tracemalloc, '_get_traces', return_value=raw_traces)) snapshot = tracemalloc.take_snapshot() self.assertEqual(snapshot.traceback_limit, 5) self.assertEqual(len(snapshot.traces), 1) trace = snapshot.traces[0] self.assertEqual(trace.size, 5) self.assertEqual(len(trace.traceback), 1) self.assertEqual(trace.traceback[0].filename, 'a.py') self.assertEqual(trace.traceback[0].lineno, 2)
Example #7
Source File: print_progress.py From SparseSC with MIT License | 6 votes |
def print_memory_snapshot(extra_str=None): import os import tracemalloc log_file = os.getenv("SparseSC_log_file") #None if non-existant snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno') if log_file is not None: log_file = open(log_file, "a") if extra_str is not None: print(extra_str, file=log_file) limit=10 print("[ Top 10 ] ", file=log_file) for stat in top_stats[:limit]: print(stat, file=log_file) other = top_stats[limit:] if other: size = sum(stat.size for stat in other) print("%s other: %.1f KiB" % (len(other), size / 1024), file=log_file) total = sum(stat.size for stat in top_stats) print("Total allocated size: %.1f KiB" % (total / 1024), file=log_file) #if old_snapshot is not None: # diff_stats = snapshot.compare_to(old_snapshot, 'lineno') if log_file is not None: log_file.close()
Example #8
Source File: test_tracemalloc.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_snapshot(self): obj, source = allocate_bytes(123) # take a snapshot snapshot = tracemalloc.take_snapshot() # write on disk snapshot.dump(support.TESTFN) self.addCleanup(support.unlink, support.TESTFN) # load from disk snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) self.assertEqual(snapshot2.traces, snapshot.traces) # tracemalloc must be tracing memory allocations to take a snapshot tracemalloc.stop() with self.assertRaises(RuntimeError) as cm: tracemalloc.take_snapshot() self.assertEqual(str(cm.exception), "the tracemalloc module must be tracing memory " "allocations to take a snapshot")
Example #9
Source File: test_zipkin.py From aiozipkin with Apache License 2.0 | 6 votes |
def test_leak_in_transport(zipkin_url, client, loop): tracemalloc.start() endpoint = az.create_endpoint('simple_service') tracer = await az.create(zipkin_url, endpoint, sample_rate=1, send_interval=0.0001, loop=loop) await asyncio.sleep(5) gc.collect() snapshot1 = tracemalloc.take_snapshot() await asyncio.sleep(10) gc.collect() snapshot2 = tracemalloc.take_snapshot() top_stats = snapshot2.compare_to(snapshot1, 'lineno') count = sum(s.count for s in top_stats) await tracer.close() assert count < 400 # in case of leak this number is around 901452
Example #10
Source File: check_memory.py From quickjs with MIT License | 6 votes |
def main(): print("Warming up (to discount regex cache etc.)") run() tracemalloc.start(25) gc.collect() snapshot1 = tracemalloc.take_snapshot() run() gc.collect() snapshot2 = tracemalloc.take_snapshot() top_stats = snapshot2.compare_to(snapshot1, 'traceback') print("Objects not released") print("====================") for stat in top_stats: if "tracemalloc.py" in str(stat) or stat.size_diff == 0: continue print(stat) for line in stat.traceback.format(): print(" ", line) print("\nquickjs should not show up above.")
Example #11
Source File: test_tracemalloc.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_create_snapshot(self): raw_traces = [(5, (('a.py', 2),))] with contextlib.ExitStack() as stack: stack.enter_context(patch.object(tracemalloc, 'is_tracing', return_value=True)) stack.enter_context(patch.object(tracemalloc, 'get_traceback_limit', return_value=5)) stack.enter_context(patch.object(tracemalloc, '_get_traces', return_value=raw_traces)) snapshot = tracemalloc.take_snapshot() self.assertEqual(snapshot.traceback_limit, 5) self.assertEqual(len(snapshot.traces), 1) trace = snapshot.traces[0] self.assertEqual(trace.size, 5) self.assertEqual(len(trace.traceback), 1) self.assertEqual(trace.traceback[0].filename, 'a.py') self.assertEqual(trace.traceback[0].lineno, 2)
Example #12
Source File: test_tracemalloc.py From android_universal with MIT License | 6 votes |
def test_create_snapshot(self): raw_traces = [(0, 5, (('a.py', 2),))] with contextlib.ExitStack() as stack: stack.enter_context(patch.object(tracemalloc, 'is_tracing', return_value=True)) stack.enter_context(patch.object(tracemalloc, 'get_traceback_limit', return_value=5)) stack.enter_context(patch.object(tracemalloc, '_get_traces', return_value=raw_traces)) snapshot = tracemalloc.take_snapshot() self.assertEqual(snapshot.traceback_limit, 5) self.assertEqual(len(snapshot.traces), 1) trace = snapshot.traces[0] self.assertEqual(trace.size, 5) self.assertEqual(len(trace.traceback), 1) self.assertEqual(trace.traceback[0].filename, 'a.py') self.assertEqual(trace.traceback[0].lineno, 2)
Example #13
Source File: test_tracemalloc.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_snapshot(self): obj, source = allocate_bytes(123) # take a snapshot snapshot = tracemalloc.take_snapshot() # write on disk snapshot.dump(support.TESTFN) self.addCleanup(support.unlink, support.TESTFN) # load from disk snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) self.assertEqual(snapshot2.traces, snapshot.traces) # tracemalloc must be tracing memory allocations to take a snapshot tracemalloc.stop() with self.assertRaises(RuntimeError) as cm: tracemalloc.take_snapshot() self.assertEqual(str(cm.exception), "the tracemalloc module must be tracing memory " "allocations to take a snapshot")
Example #14
Source File: memory_test.py From pycorrector with Apache License 2.0 | 6 votes |
def test_trace(): import tracemalloc tracemalloc.start(10) time1 = tracemalloc.take_snapshot() import pycorrector c = pycorrector.correct('少先队员因该为老人让坐') print(c) time2 = tracemalloc.take_snapshot() stats = time2.compare_to(time1, 'lineno') print('*' * 32) for stat in stats[:3]: print(stat) stats = time2.compare_to(time1, 'traceback') print('*' * 32) for stat in stats[:3]: print(stat.traceback.format())
Example #15
Source File: debug.py From zulip with Apache License 2.0 | 6 votes |
def tracemalloc_dump() -> None: if not tracemalloc.is_tracing(): logger.warning("pid %s: tracemalloc off, nothing to dump", os.getpid()) return # Despite our name for it, `timezone_now` always deals in UTC. basename = "snap.{}.{}".format(os.getpid(), timezone_now().strftime("%F-%T")) path = os.path.join(settings.TRACEMALLOC_DUMP_DIR, basename) os.makedirs(settings.TRACEMALLOC_DUMP_DIR, exist_ok=True) gc.collect() tracemalloc.take_snapshot().dump(path) with open(f'/proc/{os.getpid()}/stat', 'rb') as f: procstat = f.read().split() rss_pages = int(procstat[23]) logger.info("tracemalloc dump: tracing %s MiB (%s MiB peak), using %s MiB; rss %s MiB; dumped %s", tracemalloc.get_traced_memory()[0] // 1048576, tracemalloc.get_traced_memory()[1] // 1048576, tracemalloc.get_tracemalloc_memory() // 1048576, rss_pages // 256, basename)
Example #16
Source File: mem_test_thread.py From Auto-PyTorch with Apache License 2.0 | 6 votes |
def memory_monitor(command_queue: Queue, poll_interval=1): tracemalloc.start() old_max = 0 snapshot = None while True: try: command_queue.get(timeout=poll_interval) if snapshot is not None: print(datetime.now()) display_top(snapshot) return except Empty: max_rss = getrusage(RUSAGE_SELF).ru_maxrss if max_rss > old_max: old_max = max_rss snapshot = tracemalloc.take_snapshot() display_top(snapshot, limit=1) print(datetime.now(), 'max RSS', old_max)
Example #17
Source File: chimay_red.py From Chimay-Red with MIT License | 6 votes |
def profile_main(): """ :return: """ log.info("Profiling: ENABLED") # Enable memory usage profiling at the line level tracemalloc.start() # Enable CPU usage/function call timing/rate at the function level # Automatigically dumps profile to `filename` for further analysis cProfile.run("main()", filename=(CWD + "/chimay-red.cprof")) # Take snapshot of traced malloc profile snapshot = tracemalloc.take_snapshot() # Print snapshot statistics filtering for only `tracefiles` display_top(snapshot, limit=20, modpaths=TRACEFILES) return 0
Example #18
Source File: test_tracemalloc.py From android_universal with MIT License | 5 votes |
def test_snapshot_save_attr(self): # take a snapshot with a new attribute snapshot = tracemalloc.take_snapshot() snapshot.test_attr = "new" snapshot.dump(support.TESTFN) self.addCleanup(support.unlink, support.TESTFN) # load() should recreate the attribute snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) self.assertEqual(snapshot2.test_attr, "new")
Example #19
Source File: test_tracemalloc.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_snapshot_save_attr(self): # take a snapshot with a new attribute snapshot = tracemalloc.take_snapshot() snapshot.test_attr = "new" snapshot.dump(support.TESTFN) self.addCleanup(support.unlink, support.TESTFN) # load() should recreates the attribute snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) self.assertEqual(snapshot2.test_attr, "new")
Example #20
Source File: conftest.py From aio-pika with Apache License 2.0 | 5 votes |
def memory_tracer(): tracemalloc.start() tracemalloc.clear_traces() filters = ( tracemalloc.Filter(True, aiormq.__file__), tracemalloc.Filter(True, pamqp.__file__), tracemalloc.Filter(True, aio_pika.__file__), ) snapshot_before = tracemalloc.take_snapshot().filter_traces(filters) try: yield with suppress(Exception): gc.collect() snapshot_after = tracemalloc.take_snapshot().filter_traces(filters) top_stats = snapshot_after.compare_to( snapshot_before, "lineno", cumulative=True, ) assert not top_stats finally: tracemalloc.stop()
Example #21
Source File: test_tracemalloc.py From android_universal with MIT License | 5 votes |
def get_traced_memory(self): # Get the traced size in the domain snapshot = tracemalloc.take_snapshot() domain_filter = tracemalloc.DomainFilter(True, self.domain) snapshot = snapshot.filter_traces([domain_filter]) return sum(trace.size for trace in snapshot.traces)
Example #22
Source File: test_memoryfs.py From pyfilesystem2 with MIT License | 5 votes |
def test_close_mem_free(self): """Ensure all file memory is freed when calling close(). Prevents regression against issue #308. """ trace_filters = [tracemalloc.Filter(True, "*/memoryfs.py")] tracemalloc.start() before = tracemalloc.take_snapshot().filter_traces(trace_filters) self._create_many_files() after_create = tracemalloc.take_snapshot().filter_traces(trace_filters) self.fs.close() after_close = tracemalloc.take_snapshot().filter_traces(trace_filters) tracemalloc.stop() [diff_create] = after_create.compare_to( before, key_type="filename", cumulative=True ) self.assertGreater( diff_create.size_diff, 0, "Memory usage didn't increase after creating files; diff is %0.2f KiB." % (diff_create.size_diff / 1024.0), ) [diff_close] = after_close.compare_to( after_create, key_type="filename", cumulative=True ) self.assertLess( diff_close.size_diff, 0, "Memory usage increased after closing the file system; diff is %0.2f KiB." % (diff_close.size_diff / 1024.0), )
Example #23
Source File: test_tracemalloc.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_snapshot_save_attr(self): # take a snapshot with a new attribute snapshot = tracemalloc.take_snapshot() snapshot.test_attr = "new" snapshot.dump(support.TESTFN) self.addCleanup(support.unlink, support.TESTFN) # load() should recreate the attribute snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) self.assertEqual(snapshot2.test_attr, "new")
Example #24
Source File: allocation_profiler.py From stackimpact-python with BSD 3-Clause "New" or "Revised" License | 5 votes |
def stop_profiler(self): self.agent.log('Deactivating memory allocation profiler.') with self.profile_lock: if self.overhead_monitor: self.overhead_monitor.cancel() self.overhead_monitor = None if tracemalloc.is_tracing(): snapshot = tracemalloc.take_snapshot() self.agent.log('Allocation profiler memory overhead {0} bytes'.format(tracemalloc.get_tracemalloc_memory())) tracemalloc.stop() self.process_snapshot(snapshot, time.time() - self.start_ts)
Example #25
Source File: tools.py From rssant with BSD 3-Clause "New" or "Revised" License | 5 votes |
def top(key_type='lineno', limit=10): snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics(key_type) print_top_stats(top_stats, limit=limit)
Example #26
Source File: tools.py From rssant with BSD 3-Clause "New" or "Revised" License | 5 votes |
def top_diff(key_type='lineno', limit=10): if _INIT_MEMORY_SNAPSHOT is None: print('tracemalloc not enabled') return snapshot2 = tracemalloc.take_snapshot() top_stats = snapshot2.compare_to(_INIT_MEMORY_SNAPSHOT, key_type) print_top_stats(top_stats, limit=limit)
Example #27
Source File: tools.py From rssant with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setup(): global _INIT_MEMORY_SNAPSHOT if tracemalloc.is_tracing(): _INIT_MEMORY_SNAPSHOT = tracemalloc.take_snapshot() server = BackdoorServer() server.start() return server
Example #28
Source File: test_tracemalloc.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_snapshot_save_attr(self): # take a snapshot with a new attribute snapshot = tracemalloc.take_snapshot() snapshot.test_attr = "new" snapshot.dump(support.TESTFN) self.addCleanup(support.unlink, support.TESTFN) # load() should recreates the attribute snapshot2 = tracemalloc.Snapshot.load(support.TESTFN) self.assertEqual(snapshot2.test_attr, "new")
Example #29
Source File: test_performance.py From python-netflow-v9-softflowd with MIT License | 5 votes |
def test_compare_memory(self): """ Test memory usage of two collector runs with IPFIX and NetFlow v9 packets respectively. Then compare the two memory snapshots to make sure the libraries do not cross each other. TODO: more features could be tested, e.g. too big of a difference if one version is optimized better :return: """ pkts, t1, t2 = send_recv_packets(generate_packets(NUM_PACKETS_PERFORMANCE, 10)) self.assertEqual(len(pkts), NUM_PACKETS_PERFORMANCE) snapshot_ipfix = tracemalloc.take_snapshot() del pkts tracemalloc.clear_traces() pkts, t1, t2 = send_recv_packets(generate_packets(NUM_PACKETS_PERFORMANCE, 9)) self.assertEqual(len(pkts), NUM_PACKETS_PERFORMANCE) snapshot_v9 = tracemalloc.take_snapshot() del pkts stats = snapshot_v9.compare_to(snapshot_ipfix, "lineno") for stat in stats: if stat.traceback[0].filename.endswith("netflow/ipfix.py"): self.assertEqual(stat.count, 0) self.assertEqual(stat.size, 0) stats = snapshot_ipfix.compare_to(snapshot_v9, "lineno") for stat in stats: if stat.traceback[0].filename.endswith("netflow/v9.py"): self.assertEqual(stat.count, 0) self.assertEqual(stat.size, 0)
Example #30
Source File: test_performance.py From python-netflow-v9-softflowd with MIT License | 5 votes |
def _memory_of_version(self, version, store_packets=500) -> tracemalloc.Snapshot: """ Create memory snapshot of collector run with packets of version :version: :param version: :return: """ if not tracemalloc.is_tracing(): raise RuntimeError pkts, t1, t2 = send_recv_packets(generate_packets(NUM_PACKETS_PERFORMANCE, version), store_packets=store_packets) self.assertEqual(len(pkts), NUM_PACKETS_PERFORMANCE) snapshot = tracemalloc.take_snapshot() del pkts return snapshot