我想寻找内存泄漏。对于来自 memory_profiler 的 --pdb-mmem 标志将非常有帮助。
分析此脚本 test.py:
import time
from memory_profiler import profile
@profile
def my_func():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
if __name__ == '__main__':
my_func()
和:python -m memory_profiler --pdb-mmem=10 test.py
结果是:
Line # Mem usage Increment Occurences Line Contents
============================================================
4 33.6 MiB 33.6 MiB 1 @profile
5 def my_func():
6 41.2 MiB 7.6 MiB 1 a = [1] * (10 ** 6)
7 193.9 MiB 152.7 MiB 1 b = [2] * (2 * 10 ** 7)
8 41.4 MiB -152.6 MiB 1 del b
9 41.4 MiB 0.0 MiB 1 return a
但不会在第 7 行停止,如下所述:
--pdb-mmem MAXMEM step into the debugger when memory exceeds MAXMEM
我究竟做错了什么?