Python 3.4+tracemallocterm
tracemalloc.Filter
filters
A trace filter by file name (fnmatch pattern) and line number: it keeps or excludes allocations in a snapshot.
Syntax
tracemalloc.Filter(inclusive, filename_pattern, lineno=None, all_frames=False, domain=None)
The first argument, inclusive: True keeps only matching traces, False drops them. Several filters combine like this: a trace passes if it matches at least one inclusive filter and no exclusive one. Typical noise is Filter(False, '<frozen importlib._bootstrap>') and Filter(False, tracemalloc.__file__). By default only the most recent frame is checked; all_frames=True looks at the whole trace. Filter and DomainFilter share the undocumented base BaseFilter; you never need it directly.
Examples
import tracemalloc
tracemalloc.start()
data = [bytes(50) for _ in range(100)]
snap = tracemalloc.take_snapshot()
mine = snap.filter_traces([tracemalloc.Filter(True, __file__)])
print(all(t.traceback[0].filename == __file__ for t in mine.traces)) # → True
print(len(mine.traces) <= len(snap.traces)) # → True — исходный снимок не изменился
tracemalloc.stop()
See also
Open in the full glossarydocs.python.orgReport an inaccuracy