scott huang
scott huang

Reputation: 2678

memory_profiler: how to plot per-function memory usage

version: 0.50.0

In the official documentation. there is a link to this blog which give me instruction of how to make per-function memory usage plot. but when I try to run the exactly code in this post.

test1.py

import time

@profile
def test1():
    n = 10000
    a = [1] * n
    time.sleep(1)
    return a

@profile
def test2():
    n = 100000
    b = [1] * n
    time.sleep(1)
    return b

if __name__ == "__main__":
    test1()
    test2()

command is:

mprof run test1.py

I get this error:

Traceback (most recent call last):

File "test.py", line 3, in @profile NameError: name 'profile' is not defined

this is very strange, because there is the quotes from official :

Warning

If your Python file imports the memory profiler from memory_profiler import profile these timestamps will not be recorded. Comment out the import, leave your functions decorated, and re-run.

So if I want to have the per-function memory usage plot, I need comment out from memory_profiler import profile, but when I comment out it, there is a error.

Upvotes: 3

Views: 3160

Answers (1)

erhesto
erhesto

Reputation: 1186

Documentation is not really up-to-date, try with mprof run --python python3 test1.py (having import commented out), it seems to work for me, but generates output to file only and does not write to stdout at all.

Upvotes: 5

Related Questions