它是一个依赖 psutil 模块的纯 Python 模块。 安装 pip install -U memory_profiler 参数注解 frommemory_profilerimportprofile@profiledefmy_func():a=[1]*(10**6)b=[2]*(2*10**7)delbreturnaif__name__=='__main__':my_func() Line # Mem usage Increment Line Contents === 3 @profile 4 ...
pip install memory_profiler#Load its magic function %load_ext memory_profiler from memory_profiler import profile memory_profiler可以完成以下的工作: 1、查找一行的内存消耗 我们只需要在代码的前面加上魔法函数 %memit %memit x = 10+5 #Output peak memory: 54.01 MiB, increment: 0.27 MiB 这里,峰值...
$python -m memory_profiler del3.py+++++Filename: del3.py Line#Mem usage Increment Line Contents=== 2 10.293 MiB 0.000MiB @profile3defmy_func():4 17.934 MiB 7.641 MiB a = [1] * (10 ** 6)5 170.523 MiB 152.590 MiB b = [2] * (2 * 10 ** 7)6 170.527 MiB 0.004 MiB time....
from memory_profiler import profile @profile(precision=4, stream=open('mem.log','w+')) # 也可不使用参数 def func(x): # process 1. 2. 3. 4. stream:设置输出到指定文件,不指定则打印到标准输出; 结果说明 Line # Mem usage Increment Line Contents === 3 @profile 4 5.97 MB 0.00 MB def...
如果没有from memory_profiler import profile这句代码,执行终端命令如下 python -m memory_profiler test.py结果 Line # Mem usage Increment Occurences Line Contents === 10 53.8 MiB 53.8 MiB 1 @profile 11 def test(): 12 56.8 MiB 3.0 MiB 1 a=np.full(shape=(600, 700), fill_value=99.0) 13...
python -m memory_profiler script.py 输出关键指标: Increment:每行代码内存增量 Memory usage:进程总内存占用 4.Py-Spy:无侵入式“X光机” 优势:无需修改代码,直接分析运行中进程。 安装:pip install py-spy 命令:# 生成CPU火焰图 py-spy top --pid 12345 ...
python-mmemory_profiler your_script.py 1. 你应该会看到类似如下的输出: Line # Mem usage Increment Occurrences Line Contents === 4 14.2 MiB 14.2 MiB 1 @profile 5 def memory_leak_example(): 6 14.2 MiB 0.0 MiB 1 a = [] 7 14.2 MiB 0.0 MiB 1 for i ...
from memory_profiler import profile @profile(precision=4,stream=open('memory_profiler.log','w+')) # @profile def test1(): c=0 for item in xrange(100000): c+=1 print c # 直接运行即可 结果 Filename: memory_profiler_test.py Line # Mem usage Increment Line Contents === 5 21.492 MiB...
#python -m memory_profiler example.py Filename: example.py Line # Mem usage Increment Line Contents === 1 10.539 MiB 0.000 MiB @profile 2 def my_func(): 3 18.172 MiB 7.633 MiB a = [1] * (10 ** 6) 4 170.762 MiB 152.590 MiB b = [2] * (2 * 10 ** 7) 5 18.172 MiB -15...
pip install -U memory_profiler 然后用profile修饰想要查看的函数名:如: @profiledefmy_func(): a = [1] * (10**6) b = [2] * (2*10**7)delbreturnaif__name__ =='__main__': my_func() AI代码助手复制代码 输出结果: Line # Mem usage Increment Line Contents ...