mprof python memory_profiler
Asked Answered
J

2

6

I installed memory_profiler on my system and ran this program

!/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()

after running python fp.py it displayed

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

but while running mprof run fp.py it returns

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

how should i plot the memory usage graph is there any other library.

Junji answered 23/3, 2017 at 13:11 Comment(2)
Did you install memory_profiler via pip?Betweenwhiles
Also, you must use this command in your system command prompt, not in a python interpreter.Razo
M
1

You've to run mprof run filename.py, mprof plot commands on terminal.

For reference, visit this page Performance analysis of your python program using Memory_Profiler

Malleus answered 1/1, 2021 at 14:59 Comment(0)
P
0

I believe it has something to do with PATH.

Depends on the linux distribution, executables from pip can be installed at different locations.

For ubuntu, it is very likely in the following locations:

  • /usr/local/bin/mprof
  • ~/.local/bin/mprof (if it was installed as user package, i.e. by pip3 install memory-profiler --user)

You can either add it into PATH (see this) or run the script with full path (i.e. /usr/local/bin/mprof run fp.py)

Pastoralist answered 19/4, 2021 at 22:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.