To install memory_profiler:
1 |
sudo easy_install -U memory_profiler |
- Profile function/script:
Add following line in script to import memory profiler:
1from memory_profiler import profile
Decorate the function you would like to profile with @profile
Example:
123456789101112131415#test.pyfrom memory_profiler import profile@profiledef test_func():numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]addition = 0for num in numbers:addition += numreturn additionif __name__ == '__main__':test_func()
Run python script to get memory usage line by line.
1python test.py
Another method to profile function is , decorate function with @profile and then run script using following command:
1python -m memory_profiler test.py
2. Profile web2py application/(External scripts):
Start webpy server using following command :
1mprof run python web2py.py -p 8000 -a g0987
Now open application from browser and open page you want to profile. Then stop web2py server. To view result run command ‘mprof plot’. But to plot result you need package ‘matplotlib’.
To install matplotlib, first install required packages for matplotlib:
1sudo apt-get build-dep python-matplotlib
Now install matplotlib:
1sudo pip install matplotlib
Now run following command to get memory profile graph (Memory Usage vs Time):
1mprof plot
The available commands for mprof :- mprof run: running an executable, recording memory usage
- mprof plot: plotting one the recorded memory usage (by default, the last one)
- mprof list: listing all recorded memory usage files.
- mprof clean: delete recorded memory usage files.
- mprof rm : delete particular memory usage file.
Recent Comments