File size: 1,523 Bytes
2abfccb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import linecache
import tracemalloc
from tracemalloc import Snapshot
def format_size(size):
return tracemalloc._format_size(size, False)
def display_top(snapshot: Snapshot, limit=10, buffer=None, key_type='lineno'):
if buffer:
def write(msg):
buffer.write(msg)
buffer.write('\n')
else:
def write(msg):
print(msg)
stats = snapshot.statistics(key_type)
for index, stat in enumerate(stats[:limit], 1):
frame = stat.traceback[0]
line = linecache.getline(frame.filename, frame.lineno).strip()
msg = f'#{index}:\t{frame.filename}:{frame.lineno}: {stat.count} blocks, {format_size(stat.size)}\n\t{line}'
write(msg)
other = stats[limit:]
if other:
other_size = sum(stat.size for stat in other)
other_blocks = sum(stat.count for stat in other)
write(
f'Other:\t{len(other)} items, {other_blocks} blocks, {format_size(other_size)}')
total_size = sum(stat.size for stat in stats)
total_blocks = sum(stat.count for stat in stats)
write(
f'Total:\t{len(stats)} items, {total_blocks} blocks, {format_size(total_size)}')
def start():
tracemalloc.start()
def stop():
tracemalloc.stop()
def take_snapshot():
return tracemalloc.take_snapshot()
def filter_traces(snapshot, pattern):
return snapshot.filter_traces((
tracemalloc.Filter(True, pattern),
))
Snapshot.display_top = display_top
Snapshot.filter_traces = filter_traces
|