Python Glossary Русский

Python 3.4+tracemallocfunction

tracemalloc.get_traced_memory()

memory measurements

Returns a tuple (current, peak) of the traced memory size in bytes.

Syntax

current, peak = tracemalloc.get_traced_memory()

Only memory allocated through Python's allocators after start() is counted, not the whole process: the numbers will not match the RSS in a task manager, and memory that C extensions take around the Python allocator is not visible at all. The peak catches short spikes — a temporary list that is already gone no longer shows in the current value but stays in the peak.

Examples

import tracemalloc
tracemalloc.start()
tmp = [0] * 100_000
del tmp
current, peak = tracemalloc.get_traced_memory()
print(current, peak)  # → ? числа зависят от версии и машины
print(peak >= 800_000, current < peak)  # → True True
tracemalloc.stop()

See also

Open in the full glossarydocs.python.orgReport an inaccuracy