Return the memory usage of a process or piece of code Parameters ---------- proc : {int, string, tuple, subprocess.Popen}, optional The process to monitor. Can be given by an integer/string representing a PID, by a Popen object or by a tuple representing a P
(proc=-1, interval=.1, timeout=None, timestamps=False,
include_children=False, multiprocess=False, max_usage=False,
retval=False, stream=None, backend=None, max_iterations=None)
| 267 | |
| 268 | |
| 269 | def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False, |
| 270 | include_children=False, multiprocess=False, max_usage=False, |
| 271 | retval=False, stream=None, backend=None, max_iterations=None): |
| 272 | """ |
| 273 | Return the memory usage of a process or piece of code |
| 274 | |
| 275 | Parameters |
| 276 | ---------- |
| 277 | proc : {int, string, tuple, subprocess.Popen}, optional |
| 278 | The process to monitor. Can be given by an integer/string |
| 279 | representing a PID, by a Popen object or by a tuple |
| 280 | representing a Python function. The tuple contains three |
| 281 | values (f, args, kw) and specifies to run the function |
| 282 | f(*args, **kw). |
| 283 | Set to -1 (default) for current process. |
| 284 | |
| 285 | interval : float, optional |
| 286 | Interval at which measurements are collected. |
| 287 | |
| 288 | timeout : float, optional |
| 289 | Maximum amount of time (in seconds) to wait before returning. |
| 290 | |
| 291 | max_usage : bool, optional |
| 292 | Only return the maximum memory usage (default False) |
| 293 | |
| 294 | retval : bool, optional |
| 295 | For profiling python functions. Save the return value of the profiled |
| 296 | function. Return value of memory_usage becomes a tuple: |
| 297 | (mem_usage, retval) |
| 298 | |
| 299 | timestamps : bool, optional |
| 300 | if True, timestamps of memory usage measurement are collected as well. |
| 301 | |
| 302 | include_children : bool, optional |
| 303 | if True, sum the memory of all forked processes as well |
| 304 | |
| 305 | multiprocess : bool, optional |
| 306 | if True, track the memory usage of all forked processes. |
| 307 | |
| 308 | stream : File |
| 309 | if stream is a File opened with write access, then results are written |
| 310 | to this file instead of stored in memory and returned at the end of |
| 311 | the subprocess. Useful for long-running processes. |
| 312 | Implies timestamps=True. |
| 313 | |
| 314 | backend : str, optional |
| 315 | Current supported backends: 'psutil', 'psutil_pss', 'psutil_uss', 'posix', 'tracemalloc' |
| 316 | If `backend=None` the default is "psutil" which measures RSS aka "Resident Set Size". |
| 317 | For more information on "psutil_pss" (measuring PSS) and "psutil_uss" please refer to: |
| 318 | https://psutil.readthedocs.io/en/latest/index.html?highlight=memory_info#psutil.Process.memory_full_info |
| 319 | |
| 320 | max_iterations : int |
| 321 | Limits the number of iterations (calls to the process being monitored). Relevant |
| 322 | when the process is a python function. |
| 323 | |
| 324 | Returns |
| 325 | ------- |
| 326 | mem_usage : list of floating-point values |