Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Irixo edited this pageDec 26, 2020 ·1 revision

memory_profiler exposes a number of functions to be used in third-partycode.

memory_usage(proc=-1, interval=.1, timeout=None) returns the memory usageover a time interval. The first argument,proc represents whatshould be monitored. This can either be the PID of a process (notnecessarily a Python program), a string containing some python code tobe evaluated or a tuple(f, args, kw) containing a function and itsarguments to be evaluated asf(*args, **kw). For example,

>>>from memory_profilerimport memory_usage>>> mem_usage= memory_usage(-1,interval=.2,timeout=1)>>>print(mem_usage)    [7.296875, 7.296875, 7.296875, 7.296875, 7.296875]

Here I've told memory_profiler to get the memory consumption of thecurrent process over a period of 1 second with a time interval of 0.2seconds. As PID I've given it -1, which is a special number (PIDs areusually positive) that means current process, that is, I'm getting thememory usage of the current Python interpreter. Thus I'm gettingaround 7MB of memory usage from a plain python interpreter. If I trythe same thing on IPython (console) I get 29MB, and if I try the samething on the IPython notebook it scales up to 44MB.

If you'd like to get the memory consumption of a Python function, thenyou should specify the function and its arguments in the tuple(f,args, kw). For example:

>>> # define a simple function>>> def f(a, n=100):    ...     import time    ...     time.sleep(2)    ...     b = [a] * n    ...     time.sleep(1)    ...     return b    ...>>> from memory_profiler import memory_usage>>> memory_usage((f, (1,), {'n' : int(1e6)}))

This will execute the codef(1, n=int(1e6)) and return the memoryconsumption during this execution.

Clone this wiki locally


[8]ページ先頭

©2009-2026 Movatter.jp