To install memory_profiler:
sudo easy_install -U memory_profiler
- Profile function/script:
Add following line in script to import memory profiler:
from memory_profiler import profile
Decorate the function you would like to profile with @profile
#test.py from memory_profiler import profile @profile def test_func(): numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] addition = 0 for num in numbers: addition += num return addition if __name__ == '__main__': test_func()
Run python script to get memory usage line by line.
Another method to profile function is , decorate function with @profile and then run script using following command:
python -m memory_profiler test.py
2. Profile web2py application/(External scripts):
Start webpy server using following command :
mprof 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:
sudo apt-get build-dep python-matplotlib
Now install matplotlib:
sudo pip install matplotlib
Now run following command to get memory profile graph (Memory Usage vs Time):
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.
The post Memory profiling in Python using memory_profiler appeared first on Gaurav Vichare.