Следуя примеру кода tracemalloc в документе python, я могу успешно перечислить все файлы, создающие большие объекты.Тем не менее, номер строки всегда показывает 0 в моем результате (второй столбец в каждой строке):
#1: driver/kafka.py:0: 1150980: 98955.2 KiB
#2: ddtrace/tracer.py:0: 157178: 28245.5 KiB
#3: ddtrace/span.py:0: 157810: 5614.5 KiB
#4: python3.6/linecache.py:0: 13587: 1344.2 KiB
#5: ddtrace/context.py:0: 9: 1291.2 KiB
#6: message/__init__.py:0: 6431: 404.7 KiB
...
Вот моя функция display_top, такая же, как в doc:
def display_top(snapshot, key_type='filename', limit=20):
display = []
top_stats = snapshot.statistics(key_type)
display.append('Top %s lines' % limit)
for index, stat in enumerate(top_stats[:limit], 1):
frame = stat.traceback[0]
# replace '/path/to/module/file.py' with 'module/file.py'
filename = os.sep.join(frame.filename.split(os.sep)[-2:])
display.append('#%s: %s:%s: %s: %.1f KiB'
% (index, filename, frame.lineno, stat.count, stat.size / 1024))
line = linecache.getline(frame.filename, frame.lineno).strip()
logger.info("tracemalloc gauge.",
file_name_2=filename,
line_number_2=frame.lineno,
count=stat.count,
size=stat.size)
if line:
display.append(' %s' % line)
other = top_stats[limit:]
if other:
size = sum(stat.size for stat in other)
display.append('%s other: %.1f KiB' % (len(other), size / 1024))
total = sum(stat.size for stat in top_stats)
display.append('Total allocated size: %.1f KiB' % (total / 1024))
logger.info('\n'.join(display))