pythonperformancememoryprocess-monitoring

How can I measure Memory Performance of a Function?


Is there a way to measure the memory a function uses in Python? The Peak Of memory Usage, or like a Memory Vs Execution Time?

I could insert some bytecode, and check every certain amount of steps the stack and take a timestamp, but this is the solution I think of, I'm sure that there is a way to find out with a standard module or something.

The problem Is that I don't even know If what I'm trying to do has a name, and If it does what are the appropriate terms for it.

The main Issue that will help me out a lot is memory monitoring. Any Idea?

(I'm attempting to do Performance test over some routines, I picture this could be with decorators, but I just need ways to measure, memory, time, and possible state of variables during execution, but not as in debugging as in " how many iterations occurred when a certain variable preserved it's value" and questions of that sort, and try to extract the history of variables in the scope of the function to work stuff out with them)


Solution

  • Yes it does exist! You can use the memory profiler, more info here.

    As an example:

    @profile
    def yourFunction():
        alpha = sqrt(2)
    

    You launch your script with:

    python -m memory_profiler main.py
    

    To get the memory footprint over time, you could use mprof from the same package:

    mprof run main.py
    mprof plot
    

    You will get (as an example) enter image description here