Skip to content

Commit 3ac2559

Browse files
authored
Merge pull request #313 from mgbckr/master
Clean-up and documentation for backend
2 parents 730171a + 23f31c5 commit 3ac2559

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ MANIFEST
66
*.egg-info
77
*.pyc
88
*~
9+
.coverage
910

1011
# Ignore mprof generated files
1112
mprofile_*.dat

README.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,25 @@ file ~/.ipython/ipy_user_conf.py to add the following lines::
407407
import memory_profiler
408408
memory_profiler.load_ipython_extension(ip)
409409

410+
===============================
411+
Memory tracking backends
412+
===============================
413+
`memory_profiler` supports different memory tracking backends including: 'psutil', 'psutil_pss', 'psutil_uss', 'posix', 'tracemalloc'.
414+
If no specific backend is specified the default is to use "psutil" which measures RSS aka “Resident Set Size”.
415+
In some cases (particularly when tracking child processes) RSS may overestimate memory usage (see `example/example_psutil_memory_full_info.py` for an example).
416+
For more information on "psutil_pss" (measuring PSS) and "psutil_uss" please refer to:
417+
https://psutil.readthedocs.io/en/latest/index.html?highlight=memory_info#psutil.Process.memory_full_info
418+
419+
Currently, the backend can be set via the CLI
420+
421+
$ python -m memory_profiler --backend psutil my_script.py
422+
423+
and is exposed by the API
424+
425+
>>> from memory_profiler import memory_usage
426+
>>> mem_usage = memory_usage(-1, interval=.2, timeout=1, backend="psutil")
427+
428+
410429
============================
411430
Frequently Asked Questions
412431
============================
@@ -424,7 +443,6 @@ file ~/.ipython/ipy_user_conf.py to add the following lines::
424443
`psutil <http://pypi.python.org/pypi/psutil>`_ module.
425444

426445

427-
428446
===========================
429447
Support, bugs & wish list
430448
===========================
@@ -486,6 +504,8 @@ cleanup.
486504

487505
`Juan Luis Cano <https://github.com/Juanlu001>`_ modernized the infrastructure and helped with various things.
488506

507+
`Martin Becker <https://github.com/mgbckr>`_ added PSS and USS tracking via the psutil backend.
508+
489509
=========
490510
License
491511
=========
File renamed without changes.

memory_profiler.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def _ps_util_full_tool(memory_metric):
152152
process = psutil.Process(pid)
153153
try:
154154
if not hasattr(process, 'memory_full_info'):
155-
raise NotImplementedError("Backend `psutil_pss` and `psutil_uss` requires psutil > 4.0.0")
155+
raise NotImplementedError("Backend `{}` requires psutil > 4.0.0".format(memory_metric))
156156

157157
meminfo_attr = 'memory_full_info'
158158
meminfo = getattr(process, meminfo_attr)()
@@ -309,6 +309,12 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
309309
the subprocess. Useful for long-running processes.
310310
Implies timestamps=True.
311311
312+
backend : str, optional
313+
Current supported backends: 'psutil', 'psutil_pss', 'psutil_uss', 'posix', 'tracemalloc'
314+
If `backend=None` the default is "psutil" which measures RSS aka “Resident Set Size”.
315+
For more information on "psutil_pss" (measuring PSS) and "psutil_uss" please refer to:
316+
https://psutil.readthedocs.io/en/latest/index.html?highlight=memory_info#psutil.Process.memory_full_info
317+
312318
max_iterations : int
313319
Limits the number of iterations (calls to the process being monitored). Relevent
314320
when the process is a python function.
@@ -1318,9 +1324,9 @@ def flush(self):
13181324
default=False, action='store_true',
13191325
help='also include memory used by child processes')
13201326
parser.add_argument('--backend', dest='backend', type=str, action='store',
1321-
choices=['tracemalloc', 'psutil', 'posix'], default='psutil',
1327+
choices=['tracemalloc', 'psutil', 'psutil_pss', 'psutil_uss', 'posix'], default='psutil',
13221328
help='backend using for getting memory info '
1323-
'(one of the {tracemalloc, psutil, posix})')
1329+
'(one of the {tracemalloc, psutil, posix, psutil_pss, psutil_uss, posix})')
13241330
parser.add_argument("program", nargs=REMAINDER,
13251331
help='python script or module followed by command line arguements to run')
13261332
args = parser.parse_args()

0 commit comments

Comments
 (0)