Python Glossary Русский

Python 3.4+tracemallocterm

tracemalloc.Snapshot

snapshots

A snapshot of allocation traces: it groups them into statistics, compares with another snapshot, filters, and saves to a file.

Syntax

snapshot.statistics(key_type, cumulative=False); snapshot.compare_to(old, key_type)

The main methods: statistics(key_type) groups by 'filename', 'lineno' or 'traceback'; compare_to(old, key_type) diffs against an older snapshot; filter_traces(filters) returns a NEW snapshot and leaves the original unchanged. A wrong key_type ('line') raises ValueError. dump()/load() store the snapshot with pickle — load() trusts the file, so never load snapshots from strangers.

Examples

import tracemalloc
tracemalloc.start()
snap = tracemalloc.take_snapshot()
try:
    snap.statistics('line')
except ValueError as e:
    print(e)  # → unknown key_type: 'line'
snap.dump('snap.bin')
again = tracemalloc.Snapshot.load('snap.bin')
print(len(again.traces) == len(snap.traces))  # → True
tracemalloc.stop()

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy