Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Memory profiling in Python using memory_profiler

To install memory_profiler:

sudo easy_install -U memory_profiler

    1. 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
      from memory_profiler import 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__':

      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

      2. Profile web2py application/(External scripts):
      Start webpy server using following command :
      mprof run python -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):
      mprof 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.

The post Memory profiling in Python using memory_profiler appeared first on Gaurav Vichare.

This post first appeared on Gaurav Vichare -, please read the originial post: here

Share the post

Memory profiling in Python using memory_profiler


Subscribe to Gaurav Vichare -

Get updates delivered right to your inbox!

Thank you for your subscription