【发布时间】:2019-07-08 12:44:18
【问题描述】:
我的问题描述
我正在尝试解决 python 3.6 程序中的内存泄漏问题。
为此,我正在测试 tracemalloc,它允许我比较内存快照并打印出“回溯”。
根据docs,应该将回溯中的最大帧数设置为tracemalloc.start()的第一个参数。
但是,在我的最小测试设置(下面的代码)中,我使用参数 25 启动 tracemalloc,但我在回溯中只得到 1 帧,我期望 2:
我得到的输出
me@my_machine:/tmp$ python ./test_tm.py
Entry: /tmp/test_tm_utils.py:2: size=3533 KiB (+3533 KiB), count=99746 (+99746), average=36 B
Traceback:
/tmp/test_tm_utils.py:2
我期望的输出
我希望有 两行行,像这样:
Entry: /tmp/test_tm_utils.py:2: size=3533 KiB (+3533 KiB), count=99746 (+99746), average=36 B
Traceback:
/tmp/test_tm_utils.py:2
/tmp/test_tm.py:10
^^^^^^^^^^^^^^^^^^
最小代码示例
_/tmp/test_tm.py_中的主程序:
import tracemalloc
tracemalloc.start(25)
import test_tm_utils
if __name__ == '__main__':
s1 = tracemalloc.take_snapshot()
test_tm_utils.myfun()
s2 = tracemalloc.take_snapshot()
diff = s2.compare_to(s1, 'lineno')
for entry in diff[:1]:
print('\nEntry: {}'.format(entry))
print('Traceback:')
for line in entry.traceback:
print(' {}'.format(line))
还有test_tm_utils.py中的内存泄漏函数:
def myfun(lst=list()):
lst.append([i for i in range(100000)])
【问题讨论】: