Python 3.9+tracemallocfunction
tracemalloc.reset_peak()
memory measurements
Resets the peak of traced memory to the current size, so the peak of a single code section can be measured.
Syntax
tracemalloc.reset_peak()
Without reset_peak() the peak accumulates since start(), and measuring a second function reports the first one's peak if that one was hungrier. Unlike clear_traces(), traces are not erased: the current value and get_object_traceback() keep working. Added in 3.9.
Examples
import tracemalloc
tracemalloc.start()
big = [0] * 200_000
del big
small = [0] * 1_000
_, peak_before = tracemalloc.get_traced_memory()
tracemalloc.reset_peak()
small2 = [0] * 1_000
_, peak_after = tracemalloc.get_traced_memory()
print(peak_before > 1_000_000, peak_after < 1_000_000) # → True True
tracemalloc.stop()
See also
Open in the full glossarydocs.python.orgReport an inaccuracy