Memory profiling in Python using memory_profiler

To install memory_profiler:

    1. Profile function/script:
      Add following line in script to import memory profiler:

      Decorate the function you would like to profile with @profile

      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:

      2. Profile web2py application/(External scripts):
      Start webpy server using following command :

      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:

      Now 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.
About Gaurav Vichare

Hi , I am Gaurav : author of this blog , Software engineer and a Photoshop lover . You can connect with me on Facebook , cirlce me on Google or follow on Twitter