# memory_profiler **Repository Path**: lhtgitee1/memory_profiler ## Basic Information - **Project Name**: memory_profiler - **Description**: Monitor Memory usage of Python code - **Primary Language**: Unknown - **License**: BSD-3-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-20 - **Last Updated**: 2024-05-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README .. image:: https://travis-ci.org/pythonprofilers/memory_profiler.svg?branch=master :target: https://travis-ci.org/pythonprofilers/memory_profiler ================= Memory Profiler ================= This is a python module for monitoring memory consumption of a process as well as line-by-line analysis of memory consumption for python programs. It is a pure python module which depends on the `psutil `_ module. ============== Installation ============== Install via pip:: $ pip install -U memory_profiler The package is also available on `conda-forge `_. To install from source, download the package, extract and type:: $ python setup.py install ======= Usage ======= line-by-line memory usage ========================= The line-by-line memory usage mode is used much in the same way of the `line_profiler `_: first decorate the function you would like to profile with ``@profile`` and then run the script with a special script (in this case with specific arguments to the Python interpreter). In the following example, we create a simple function ``my_func`` that allocates lists ``a``, ``b`` and then deletes ``b``:: @profile def my_func(): a = [1] * (10 ** 6) b = [2] * (2 * 10 ** 7) del b return a if __name__ == '__main__': my_func() Execute the code passing the option ``-m memory_profiler`` to the python interpreter to load the memory_profiler module and print to stdout the line-by-line analysis. If the file name was example.py, this would result in:: $ python -m memory_profiler example.py Output will follow:: Line # Mem usage Increment Line Contents ============================================== 3 @profile 4 5.97 MB 0.00 MB def my_func(): 5 13.61 MB 7.64 MB a = [1] * (10 ** 6) 6 166.20 MB 152.59 MB b = [2] * (2 * 10 ** 7) 7 13.61 MB -152.59 MB del b 8 13.61 MB 0.00 MB return a The first column represents the line number of the code that has been profiled, the second column (*Mem usage*) the memory usage of the Python interpreter after that line has been executed. The third column (*Increment*) represents the difference in memory of the current line with respect to the last one. The last column (*Line Contents*) prints the code that has been profiled. Decorator ========= A function decorator is also available. Use as follows:: from memory_profiler import profile @profile def my_func(): a = [1] * (10 ** 6) b = [2] * (2 * 10 ** 7) del b return a In this case the script can be run without specifying ``-m memory_profiler`` in the command line. In function decorator, you can specify the precision as an argument to the decorator function. Use as follows:: from memory_profiler import profile @profile(precision=4) def my_func(): a = [1] * (10 ** 6) b = [2] * (2 * 10 ** 7) del b return a If a python script with decorator ``@profile`` is called using ``-m memory_profiler`` in the command line, the ``precision`` parameter is ignored. Time-based memory usage ========================== Sometimes it is useful to have full memory usage reports as a function of time (not line-by-line) of external processes (be it Python scripts or not). In this case the executable ``mprof`` might be useful. Use it like:: mprof run mprof plot The first line run the executable and record memory usage along time, in a file written in the current directory. Once it's done, a graph plot can be obtained using the second line. The recorded file contains a timestamps, that allows for several profiles to be kept at the same time. Help on each `mprof` subcommand can be obtained with the `-h` flag, e.g. `mprof run -h`. In the case of a Python script, using the previous command does not give you any information on which function is executed at a given time. Depending on the case, it can be difficult to identify the part of the code that is causing the highest memory usage. Adding the `profile` decorator to a function and running the Python script with mprof run