mprof python内存分析器

ami*_*mar 6 python memory-profiling

我在系统上安装了 memory_profiler 并运行了这个程序

!/usr/bin/env python
from memory_profiler import profile

@profile(precision=4)
def my_func():
    a = [1] * (10 ** 6)
    b = [2] * (2 * 10 ** 7)
   del b
   return a

if __name__ == '__main__':
   my_func()
Run Code Online (Sandbox Code Playgroud)

运行 python fp.py 后显示

Filename: fp.py

Line #    Mem usage    Increment   Line Contents
================================================
     4  12.6992 MiB   0.0000 MiB   @profile(precision=4)
     5                             def my_func():
     6  20.3516 MiB   7.6523 MiB       a = [1] * (10 ** 6)
     7 172.8945 MiB 152.5430 MiB       b = [2] * (2 * 10 ** 7)
     8  20.5273 MiB -152.3672 MiB       del b
     9  20.5273 MiB   0.0000 MiB       return a
Run Code Online (Sandbox Code Playgroud)

但是在运行 mprof run fp.py 时它会返回

No command 'mprof' found, did you mean:
 Command 'sprof' from package 'libc-dev-bin' (main)
 Command 'mlprof' from package 'mlton-tools' (universe)
 Command 'pprof' from package 'tau' (universe)
 Command 'prof' from package 'profphd' (universe)
 Command 'gprof' from package 'binutils' (main)
 Command 'gprof' from package 'binutils-multiarch' (main)
mprof: command not found
Run Code Online (Sandbox Code Playgroud)

我应该如何绘制内存使用图还有其他库吗?

小智 0

您必须在终端上运行mprof run filename.py,mprof plot命令。

如需参考,请访问此页面使用 Memory_Profiler 对 Python 程序进行性能分析