On this page
tracemalloc.Snapshot
Prev: tracemalloc.Sequence | Up: tracemalloc | Next: tracemalloc.start
Rel: tracemalloc.BaseFilter, tracemalloc.clear_traces, tracemalloc.DomainFilter, tracemalloc.Filter
Ref: docs.python.org — tracemalloc.Snapshot
Tags: #public #python3 #stdlib #tracemalloc
Import
import tracemalloc
from tracemalloc import Snapshot
Signature & Overview
tracemalloc.Snapshot(traces, traceback_limit)
Snapshot of traces of memory blocks allocated by Python.
Methods & Attributes
Snapshot.compare_to(self, old_snapshot, key_type, cumulative=False)— Compute the differences with an old snapshot old_snapshot. Get statistics as a sorted list of StatisticDiff instances, grouped by group_by.Snapshot.dump(self, filename)— Write the snapshot into a file.Snapshot.filter_traces(self, filters)— Create a new Snapshot instance with a filtered traces sequence, filters is a list of Filter or DomainFilter instances. If filters is an empty list, return a new Snapshot instance with a copy of the traces.Snapshot.load(filename)— Load a snapshot from a file.Snapshot.statistics(self, key_type, cumulative=False)— Group statistics by key_type. Return a sorted list of Statistic instances.
Usage
import tracemalloc
tracemalloc.start()
try:
s1 = tracemalloc.take_snapshot()
_big = [0] * 5000
s2 = tracemalloc.take_snapshot()
diff = s2.compare_to(s1, 'lineno')
assert isinstance(s1, tracemalloc.Snapshot) and len(diff) >= 1
finally:
tracemalloc.stop()
Gotchas
- Save snapshots to disk with
s.dump('snap.bin')and inspect later withSnapshot.load('snap.bin').
REPL Inspect
from tracemalloc import Snapshot
help(Snapshot)
Linked from
Related notes
- tuts/python3/stdlib_module/tracemalloc/Sequence Linked here; links to this page; shared tags: #stdlib, #tracemalloc
- tuts/python3/stdlib_module/tracemalloc/start Linked here; links to this page; shared tags: #stdlib, #tracemalloc
- tuts/python3/stdlib_module/tracemalloc Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tracemalloc/BaseFilter Linked here; shared tags: #stdlib, #tracemalloc
- tuts/python3/stdlib_module/tracemalloc/clear_traces Linked here; shared tags: #stdlib, #tracemalloc
- tuts/python3/stdlib_module/tracemalloc/DomainFilter Linked here; shared tags: #stdlib, #tracemalloc
- tuts/python3/stdlib_module/tracemalloc/Filter Linked here; shared tags: #stdlib, #tracemalloc
- tuts/python3/stdlib_module/tracemalloc/Frame shared tags: #stdlib, #tracemalloc
- tuts/python3/stdlib_module/tracemalloc/get_object_traceback shared tags: #stdlib, #tracemalloc
- tuts/python3/stdlib_module/tracemalloc/get_traceback_limit shared tags: #stdlib, #tracemalloc