55 Memory Profiler
66=================
77
8+
9+ **Note: ** This package is no longer actively maintained. If you'd like to volunteer to maintain it, please drop me a line at f@bianp.net
10+
811This is a python module for monitoring memory consumption of a process
912as well as line-by-line analysis of memory consumption for python
1013programs. It is a pure python module which depends on the `psutil
@@ -23,7 +26,7 @@ The package is also available on `conda-forge
2326
2427To install from source, download the package, extract and type::
2528
26- $ python setup.py install
29+ $ pip install .
2730
2831
2932=======
@@ -64,14 +67,14 @@ this would result in::
6467
6568Output will follow::
6669
67- Line # Mem usage Increment Line Contents
68- ==============================================
69- 3 @profile
70- 4 5.97 MB 0.00 MB def my_func():
71- 5 13.61 MB 7.64 MB a = [1] * (10 ** 6)
72- 6 166.20 MB 152.59 MB b = [2] * (2 * 10 ** 7)
73- 7 13.61 MB -152.59 MB del b
74- 8 13.61 MB 0.00 MB return a
70+ Line # Mem usage Increment Occurrences Line Contents
71+ ============================================================
72+ 3 38.816 MiB 38.816 MiB 1 @profile
73+ 4 def my_func():
74+ 5 46.492 MiB 7.676 MiB 1 a = [1] * (10 ** 6)
75+ 6 199.117 MiB 152.625 MiB 1 b = [2] * (2 * 10 ** 7)
76+ 7 46.629 MiB -152.488 MiB 1 del b
77+ 8 46.629 MiB 0.000 MiB 1 return a
7578
7679
7780The first column represents the line number of the code that has been
@@ -179,7 +182,7 @@ track the usage of child processes: sum the memory of all children to the
179182parent's usage and track each child individual.
180183
181184To create a report that combines memory usage of all the children and the
182- parent, use the ``include_children `` flag in either the ``profile `` decorator or
185+ parent, use the ``include-children `` flag in either the ``profile `` decorator or
183186as a command line argument to ``mprof ``::
184187
185188 mprof run --include-children <script>
@@ -197,7 +200,7 @@ This will create a plot using matplotlib similar to this:
197200 :target: https://github.com/pythonprofilers/memory_profiler/pull/134
198201 :height: 350px
199202
200- You can combine both the ``include_children `` and ``multiprocess `` flags to show
203+ You can combine both the ``include-children `` and ``multiprocess `` flags to show
201204the total memory of the program as well as each child individually. If using
202205the API directly, note that the return from ``memory_usage `` will include the
203206child memory in a nested list along with the main process memory.
@@ -214,6 +217,21 @@ You can also hide the function timestamps using the ``n`` flag, such as
214217
215218 mprof plot -n
216219
220+ Trend lines and its numeric slope can be plotted using the ``s `` flag, such as
221+
222+ mprof plot -s
223+
224+ .. image :: ./images/trend_slope.png
225+ :height: 350px
226+
227+ The intended usage of the -s switch is to check the labels' numerical slope over a significant time period for :
228+
229+ - ``>0 `` it might mean a memory leak.
230+ - ``~0 `` if 0 or near 0, the memory usage may be considered stable.
231+ - ``<0 `` to be interpreted depending on the expected process memory usage patterns, also might mean that the sampling period is too small.
232+
233+ The trend lines are for ilustrative purposes and are plotted as (very) small dashed lines.
234+
217235
218236Setting debugger breakpoints
219237=============================
@@ -392,6 +410,25 @@ file ~/.ipython/ipy_user_conf.py to add the following lines::
392410 import memory_profiler
393411 memory_profiler.load_ipython_extension(ip)
394412
413+ ===============================
414+ Memory tracking backends
415+ ===============================
416+ `memory_profiler ` supports different memory tracking backends including: 'psutil', 'psutil_pss', 'psutil_uss', 'posix', 'tracemalloc'.
417+ If no specific backend is specified the default is to use "psutil" which measures RSS aka "Resident Set Size".
418+ In some cases (particularly when tracking child processes) RSS may overestimate memory usage (see `example/example_psutil_memory_full_info.py ` for an example).
419+ For more information on "psutil_pss" (measuring PSS) and "psutil_uss" please refer to:
420+ https://psutil.readthedocs.io/en/latest/index.html?highlight=memory_info#psutil.Process.memory_full_info
421+
422+ Currently, the backend can be set via the CLI
423+
424+ $ python -m memory_profiler --backend psutil my_script.py
425+
426+ and is exposed by the API
427+
428+ >>> from memory_profiler import memory_usage
429+ >>> mem_usage = memory_usage(- 1 , interval = .2 , timeout = 1 , backend = " psutil" )
430+
431+
395432============================
396433 Frequently Asked Questions
397434============================
@@ -409,7 +446,6 @@ file ~/.ipython/ipy_user_conf.py to add the following lines::
409446 `psutil <http://pypi.python.org/pypi/psutil >`_ module.
410447
411448
412-
413449===========================
414450 Support, bugs & wish list
415451===========================
@@ -419,9 +455,9 @@ Send issues, proposals, etc. to `github's issue tracker
419455<https://github.com/pythonprofilers/memory_profiler/issues> `_ .
420456
421457If you've got questions regarding development, you can email me
422- directly at fabian@fseoane .net
458+ directly at f@bianp .net
423459
424- .. image :: http://fseoane .net/static/tux_memory_small.png
460+ .. image :: http://fa.bianp .net/static/tux_memory_small.png
425461
426462
427463=============
@@ -471,6 +507,8 @@ cleanup.
471507
472508`Juan Luis Cano <https://github.com/Juanlu001 >`_ modernized the infrastructure and helped with various things.
473509
510+ `Martin Becker <https://github.com/mgbckr >`_ added PSS and USS tracking via the psutil backend.
511+
474512=========
475513 License
476514=========
0 commit comments