{ "info": { "author": "Fabian Pedregosa", "author_email": "f@bianp.net", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "Intended Audience :: Science/Research", "License :: OSI Approved :: BSD License", "Operating System :: POSIX", "Operating System :: Unix", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.2", "Programming Language :: Python :: 3.3", "Topic :: Software Development" ], "description": ".. image:: https://travis-ci.org/pythonprofilers/memory_profiler.svg?branch=master\n :target: https://travis-ci.org/pythonprofilers/memory_profiler\n\n=================\n Memory Profiler\n=================\n\nThis is a python module for monitoring memory consumption of a process\nas well as line-by-line analysis of memory consumption for python\nprograms. It is a pure python module which depends on the `psutil\n`_ module.\n\n\n==============\n Installation\n==============\nInstall via pip::\n\n $ pip install -U memory_profiler\n\nThe package is also available on `conda-forge\n`_.\n\nTo install from source, download the package, extract and type::\n\n $ python setup.py install\n\n\n=======\n Usage\n=======\n\n\nline-by-line memory usage\n=========================\n\nThe line-by-line memory usage mode is used much in the same way of the\n`line_profiler `_: first\ndecorate the function you would like to profile with ``@profile`` and\nthen run the script with a special script (in this case with specific\narguments to the Python interpreter).\n\nIn the following example, we create a simple function ``my_func`` that\nallocates lists ``a``, ``b`` and then deletes ``b``::\n\n\n @profile\n def my_func():\n a = [1] * (10 ** 6)\n b = [2] * (2 * 10 ** 7)\n del b\n return a\n\n if __name__ == '__main__':\n my_func()\n\n\nExecute the code passing the option ``-m memory_profiler`` to the\npython interpreter to load the memory_profiler module and print to\nstdout the line-by-line analysis. If the file name was example.py,\nthis would result in::\n\n $ python -m memory_profiler example.py\n\nOutput will follow::\n\n Line # Mem usage Increment Line Contents\n ==============================================\n 3 @profile\n 4 5.97 MB 0.00 MB def my_func():\n 5 13.61 MB 7.64 MB a = [1] * (10 ** 6)\n 6 166.20 MB 152.59 MB b = [2] * (2 * 10 ** 7)\n 7 13.61 MB -152.59 MB del b\n 8 13.61 MB 0.00 MB return a\n\n\nThe first column represents the line number of the code that has been\nprofiled, the second column (*Mem usage*) the memory usage of the\nPython interpreter after that line has been executed. The third column\n(*Increment*) represents the difference in memory of the current line\nwith respect to the last one. The last column (*Line Contents*) prints\nthe code that has been profiled.\n\nDecorator\n=========\nA function decorator is also available. Use as follows::\n\n from memory_profiler import profile\n\n @profile\n def my_func():\n a = [1] * (10 ** 6)\n b = [2] * (2 * 10 ** 7)\n del b\n return a\n\nIn this case the script can be run without specifying ``-m\nmemory_profiler`` in the command line.\n\nIn function decorator, you can specify the precision as an argument to the\ndecorator function. Use as follows::\n\n from memory_profiler import profile\n\n @profile(precision=4)\n def my_func():\n a = [1] * (10 ** 6)\n b = [2] * (2 * 10 ** 7)\n del b\n return a\n\nIf a python script with decorator ``@profile`` is called using ``-m\nmemory_profiler`` in the command line, the ``precision`` parameter is ignored.\n\nTime-based memory usage\n==========================\nSometimes it is useful to have full memory usage reports as a function of\ntime (not line-by-line) of external processes (be it Python scripts or not).\nIn this case the executable ``mprof`` might be useful. Use it like::\n\n mprof run \n mprof plot\n\nThe first line run the executable and record memory usage along time,\nin a file written in the current directory.\nOnce it's done, a graph plot can be obtained using the second line.\nThe recorded file contains a timestamps, that allows for several\nprofiles to be kept at the same time.\n\nHelp on each `mprof` subcommand can be obtained with the `-h` flag,\ne.g. `mprof run -h`.\n\nIn the case of a Python script, using the previous command does not\ngive you any information on which function is executed at a given\ntime. Depending on the case, it can be difficult to identify the part\nof the code that is causing the highest memory usage.\n\nAdding the `profile` decorator to a function and running the Python\nscript with\n\n mprof run