New in version 3.4.
The tracemalloc module is a debug tool to trace memory blocks allocated by Python. It provides the following information: 4.8 MiB data (bytecode and constants) from modules and that the collections module allocated 244 KiB to build namedtuple types.
See Snapshot.statistics() for more options..2 MiB of module data (bytecode and constants), and that this is 4.4 KiB. The traceback is where the importlib loaded data most recently: on the import pdb line of the doctest module. The traceback may change if a new module is loaded.
Code to display the 10 lines allocating the most memory with a pretty output, ignoring <frozen importlib._bootstrap> and <unknown> files:
import linecache import os import tracemalloc def display_top(snapshot, group_by='lineno', limit=10): snapshot = snapshot.filter_traces(( tracemalloc.Filter(False, "<frozen importlib._bootstrap>"), tracemalloc.Filter(False, "<unknown>"), )) top_stats = snapshot.statistics(group_by).
Clear traces of memory blocks allocated by Python. the maximum number of frames stored in the traceback of a trace.
The tracemalloc module must be tracing memory allocations to get the limit, otherwise an exception is raised.
The limit is set by the start() function.
Get the current size and peak size of memory blocks traced by the tracemalloc module as a tuple: (current: int, peak: int).
Get the memory usage in bytes of the tracemalloc module used to store traces of memory blocks. Return an int.
True if the tracemalloc module is tracing Python memory allocations, False otherwise.
See also start() and stop() functions. tracing Python memory allocations: uninstall hooks on Python memory allocators. Also clears all previously collected traces of memory blocks allocated by Python.
Call take_snapshot() function to take a snapshot of traces before clearing them.
See also start(), is_tracing() and clear_traces() functions. on traces of memory blocks.
See the fnmatch.fnmatch() function for the syntax of filename_pattern. The '.pyc' and '.pyo' file extensions are replaced with '.py'.
Examples:
If inclusive is True (include), only trace memory blocks allocated in a file with a name matching filename_pattern at line number lineno.
If inclusive is False (exclude), ignore memory blocks allocated in a file with a name matching filename_pattern at line number lineno.
Line number (int) of the filter. If lineno is None, the filter matches any line number.
Filename pattern of the filter (str).
If all_frames is True, all frames of the traceback are checked. If all_frames is False, only the most recent frame is checked.
This attribute has no effect if the traceback limit is 1. See the get_traceback_limit() function and Snapshot.traceback_limit attribute.
Frame of a traceback.
The Traceback class is a sequence of Frame instances.
Filename (str).
Line number (int).
Snapshot of traces of memory blocks allocated by Python.
The take_snapshot() function creates a snapshot instance..
Write the snapshot into a file.
Use load() to reload the snapshot. a snapshot from a'.
The result is sorted from the biggest to the smallest by: Statistic.size, Statistic.count and then by Statistic.traceback.
Maximum number of frames stored in the traceback of traces: result of the get_traceback_limit() when the snapshot was taken.
Traces of all memory blocks allocated by Python: sequence of Trace instances.
The sequence has an undefined order. Use the Snapshot.statistics() method to get a sorted list of statistics.
Statistic on memory allocations.
Snapshot.statistics() returns a list of Statistic instances.
See also the StatisticDiff class.
Number of memory blocks (int).
Total size of memory blocks in bytes (int).
Traceback where the memory block was allocated, Traceback instance.
Statistic difference on memory allocations between an old and a new Snapshot instance.
Snapshot.compare_to() returns a list of StatisticDiff instances. See also the Statistic class.
Number of memory blocks in the new snapshot (int): 0 if the memory blocks have been released in the new snapshot.
Difference of number of memory blocks between the old and the new snapshots (int): 0 if the memory blocks have been allocated in the new snapshot.
Total size of memory blocks in bytes in the new snapshot (int): 0 if the memory blocks have been released in the new snapshot.
Difference of total size of memory blocks in bytes between the old and the new snapshots (int): 0 if the memory blocks have been allocated in the new snapshot.
Traceback where the memory blocks were allocated, Traceback instance.
Trace of a memory block.
The Snapshot.traces attribute is a sequence of Trace instances.
Size of the memory block in bytes (int).
Traceback where the memory block was allocated, Traceback instance..
Format the traceback as a list of lines with newlines. Use the linecache module to retrieve lines from the source code. If limit is set, only format the limit most recent frames.
Similar to the traceback.format_tb() function, except that format() does not include newlines.
Example:
print("Traceback (most recent call first):") for line in traceback: print(line)
Output:
Traceback (most recent call first): File "test.py", line 9 obj = Object() File "test.py", line 12 tb = tracemalloc.get_object_traceback(f())