/
/
/
1"""
2Helper to trace memory usage.
3
4https://www.red-gate.com/simple-talk/development/python/memory-profiling-in-python-with-tracemalloc/
5"""
6
7import asyncio
8import tracemalloc
9
10# ruff: noqa: D103, E741, T201
11# pylint: disable=missing-function-docstring
12
13# list to store memory snapshots
14snaps = []
15
16
17def _take_snapshot():
18 snaps.append(tracemalloc.take_snapshot())
19
20
21async def take_snapshot():
22 loop = asyncio.get_running_loop()
23 await loop.run_in_executor(None, _take_snapshot)
24
25
26def _display_stats():
27 stats = snaps[0].statistics("filename")
28 print("\n*** top 5 stats grouped by filename ***")
29 for s in stats[:5]:
30 print(s)
31
32
33async def display_stats():
34 loop = asyncio.get_running_loop()
35 await loop.run_in_executor(None, _display_stats)
36
37
38def compare():
39 first = snaps[0]
40 for snapshot in snaps[1:]:
41 stats = snapshot.compare_to(first, "lineno")
42 print("\n*** top 10 stats ***")
43 for s in stats[:10]:
44 print(s)
45
46
47def print_trace():
48 # pick the last saved snapshot, filter noise
49 snapshot = snaps[-1].filter_traces(
50 (
51 tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
52 tracemalloc.Filter(False, "<frozen importlib._bootstrap_external>"),
53 tracemalloc.Filter(False, "<unknown>"),
54 )
55 )
56 largest = snapshot.statistics("traceback")[0]
57
58 print(
59 "\n*** Trace for largest memory block - "
60 f"({largest.count} blocks, {largest.size / 1024} Kb) ***"
61 )
62 for l in largest.traceback.format():
63 print(l)
64