EP 454: reformat
This commit is contained in:
parent
ae45d8d917
commit
618fedf642
215
pep-0454.txt
215
pep-0454.txt
|
@ -13,7 +13,7 @@ Python-Version: 3.4
|
||||||
Abstract
|
Abstract
|
||||||
========
|
========
|
||||||
|
|
||||||
Add a new tracemalloc module to trace Python memory allocations.
|
Add a new ``tracemalloc`` module to trace Python memory allocations.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,6 +29,14 @@ information:
|
||||||
* Compute delta between two "snapshots"
|
* Compute delta between two "snapshots"
|
||||||
* Location of a memory allocation: Python filename and line number
|
* Location of a memory allocation: Python filename and line number
|
||||||
|
|
||||||
|
The ``tracemalloc`` module is different than other third-party modules
|
||||||
|
like ``Heapy`` or ``PySizer``, because it is focused on the location of
|
||||||
|
a memory allocation rather that the object type or object content.
|
||||||
|
|
||||||
|
|
||||||
|
API
|
||||||
|
===
|
||||||
|
|
||||||
To trace the most Python memory allocations, the module should be
|
To trace the most Python memory allocations, the module should be
|
||||||
enabled as early as possible in your application by calling
|
enabled as early as possible in your application by calling
|
||||||
``tracemalloc.enable()`` function, by setting the ``PYTHONTRACEMALLOC``
|
``tracemalloc.enable()`` function, by setting the ``PYTHONTRACEMALLOC``
|
||||||
|
@ -36,200 +44,213 @@ environment variable to ``1``, or by using ``-X tracemalloc`` command
|
||||||
line option.
|
line option.
|
||||||
|
|
||||||
|
|
||||||
API
|
|
||||||
===
|
|
||||||
|
|
||||||
Functions
|
Functions
|
||||||
---------
|
---------
|
||||||
|
|
||||||
enable():
|
enable():
|
||||||
|
|
||||||
Start tracing Python memory allocations.
|
Start tracing Python memory allocations.
|
||||||
|
|
||||||
disable():
|
disable():
|
||||||
|
|
||||||
Stop tracing Python memory allocations and stop the timer started by
|
Stop tracing Python memory allocations and stop the timer started by
|
||||||
``start_timer()``.
|
``start_timer()``.
|
||||||
|
|
||||||
is_enabled():
|
is_enabled():
|
||||||
|
|
||||||
``True`` if the module is enabled, ``False`` otherwise.
|
Get the status of the module: ``True`` if it is enabled, ``False``
|
||||||
|
otherwise.
|
||||||
|
|
||||||
get_object_trace(obj):
|
get_object_trace(obj):
|
||||||
|
|
||||||
Get the trace of a Python object *obj* as a namedtuple with 3 attributes:
|
Get the trace of a Python object *obj* as a namedtuple with 3 attributes:
|
||||||
|
|
||||||
- ``size``: size in bytes of the object
|
- ``size``: size in bytes of the object
|
||||||
- ``filename``: name of the Python script where the object was allocated
|
- ``filename``: name of the Python script where the object was allocated
|
||||||
- ``lineno``: line number where the object was allocated
|
- ``lineno``: line number where the object was allocated
|
||||||
|
|
||||||
Return ``None`` if tracemalloc did not save the location where the object
|
Return ``None`` if tracemalloc did not save the location where the object
|
||||||
was allocated, for example if tracemalloc was disabled.
|
was allocated, for example if tracemalloc was disabled.
|
||||||
|
|
||||||
get_process_memory():
|
get_process_memory():
|
||||||
|
|
||||||
Get the memory usage of the current process as a meminfo namedtuple with
|
Get the memory usage of the current process as a meminfo namedtuple
|
||||||
two attributes:
|
with two attributes:
|
||||||
|
|
||||||
* ``rss``: Resident Set Size in bytes
|
* ``rss``: Resident Set Size in bytes
|
||||||
* ``vms``: size of the virtual memory in bytes
|
* ``vms``: size of the virtual memory in bytes
|
||||||
|
|
||||||
Return ``None`` if the platform is not supported.
|
Return ``None`` if the platform is not supported.
|
||||||
|
|
||||||
Use the ``psutil`` module if available.
|
Use the ``psutil`` module if available.
|
||||||
|
|
||||||
start_timer(delay: int, func: callable, args: tuple=(), kwargs: dict={}):
|
start_timer(delay: int, func: callable, args: tuple=(), kwargs: dict={}):
|
||||||
|
|
||||||
Start a timer calling ``func(*args, **kwargs)`` every *delay*
|
Start a timer calling ``func(*args, **kwargs)`` every *delay*
|
||||||
seconds.
|
seconds.
|
||||||
|
|
||||||
The timer is based on the Python memory allocator, it is not real
|
The timer is based on the Python memory allocator, it is not real
|
||||||
time. *func* is called after at least *delay* seconds, it is not
|
time. *func* is called after at least *delay* seconds, it is not
|
||||||
called exactly after *delay* seconds if no Python memory allocation
|
called exactly after *delay* seconds if no Python memory allocation
|
||||||
occurred.
|
occurred.
|
||||||
|
|
||||||
If ``start_timer()`` is called twice, previous parameters are
|
If ``start_timer()`` is called twice, previous parameters are
|
||||||
replaced. The timer has a resolution of 1 second.
|
replaced. The timer has a resolution of 1 second.
|
||||||
|
|
||||||
`start_timer()`` is used by ``DisplayTop`` and ``TakeSnapshot``
|
`start_timer()`` is used by ``DisplayTop`` and ``TakeSnapshot`` to
|
||||||
to run regulary a task.
|
run regulary a task.
|
||||||
|
|
||||||
stop_timer():
|
stop_timer():
|
||||||
|
|
||||||
Stop the timer started by ``start_timer()``.
|
Stop the timer started by ``start_timer()``.
|
||||||
|
|
||||||
|
|
||||||
DisplayTop class
|
DisplayTop class
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
DisplayTop(count: int, file=sys.stdout) class:
|
``DisplayTop(count: int, file=sys.stdout)`` class:
|
||||||
|
|
||||||
Display the list of the N biggest memory allocations.
|
Display the list of the N biggest memory allocations.
|
||||||
|
|
||||||
display() method:
|
``display()`` method:
|
||||||
|
|
||||||
Display the top
|
Display the top
|
||||||
|
|
||||||
start(delay: int) method:
|
``start(delay: int)`` method:
|
||||||
|
|
||||||
Start a task using tracemalloc timer to display the top every delay seconds.
|
Start a task using tracemalloc timer to display the top every delay seconds.
|
||||||
|
|
||||||
stop() method:
|
``stop()`` method:
|
||||||
|
|
||||||
Stop the task started by the ``DisplayTop.start()`` method
|
Stop the task started by the ``DisplayTop.start()`` method
|
||||||
|
|
||||||
color attribute:
|
``color`` attribute:
|
||||||
|
|
||||||
Use color if True (bool, default: stream.isatty()).
|
``display()`` uses colors if ``True`` (bool,
|
||||||
|
default: ``stream.isatty()``).
|
||||||
|
|
||||||
compare_with_previous attribute:
|
``compare_with_previous`` attribute:
|
||||||
|
|
||||||
If ``True``, compare with the previous top if ``True``. If ``False``,
|
If ``True``, ``display()`` compares with the previous top if
|
||||||
compare with the first one (bool, default: ``True``): .
|
``True``. If ``False``, compare with the first one (bool, default:
|
||||||
|
``True``): .
|
||||||
|
|
||||||
filename_parts attribute:
|
``filename_parts`` attribute:
|
||||||
|
|
||||||
Number of displayed filename parts (int, default: ``3``).
|
Number of displayed filename parts (int, default: ``3``).
|
||||||
|
|
||||||
show_average attribute:
|
``show_average`` attribute:
|
||||||
|
|
||||||
If ``True``, show the average size of allocations (bool, default: ``True``).
|
If ``True``, ``display()`` shows the average size of allocations
|
||||||
|
(bool, default: ``True``).
|
||||||
|
|
||||||
show_count attribute:
|
``show_count`` attribute:
|
||||||
|
|
||||||
If ``True``, show the number of allocations (bool, default: ``True``).
|
If ``True``, ``display()`` shows the number of allocations (bool, default: ``True``).
|
||||||
|
|
||||||
show_lineno attribute:
|
``show_lineno`` attribute:
|
||||||
|
|
||||||
If ``True``, use also the line number, not only the filename (bool, default:
|
If ``True``, use also the line number, not only the filename (bool,
|
||||||
``True``). If ``False``, only show the filename.
|
default: ``True``). If ``False``, only show the filename.
|
||||||
|
|
||||||
show_size attribute:
|
``show_size`` attribute:
|
||||||
|
|
||||||
if ``True``, show the size of allocations (bool, default: ``True``).
|
If ``True``, ``display()`` shows the size of allocations (bool,
|
||||||
|
default: ``True``).
|
||||||
|
|
||||||
user_data_callback attribute:
|
``user_data_callback`` attribute:
|
||||||
|
|
||||||
Optional callback collecting user data (callable, default:
|
Optional callback collecting user data (callable, default:
|
||||||
``None``). See ``Snapshot.create()``.
|
``None``). See ``Snapshot.create()``.
|
||||||
|
|
||||||
|
|
||||||
Snapshot class
|
Snapshot class
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
Snapshot() class:
|
``Snapshot()`` class:
|
||||||
|
|
||||||
Snapshot of Python memory allocations. Use ``TakeSnapshot`` to regulary
|
Snapshot of Python memory allocations. Use ``TakeSnapshot`` to
|
||||||
take snapshots.
|
regulary take snapshots.
|
||||||
|
|
||||||
create(user_data_callback=None) method:
|
``create(user_data_callback=None)`` method:
|
||||||
|
|
||||||
Take a snapshot. If user_data_callback is specified, it must be a callable
|
Take a snapshot. If user_data_callback is specified, it must be a
|
||||||
object returning a list of (title: str, format: str, value: int). format
|
callable object returning a list of (title: str, format: str, value:
|
||||||
must be "size". The list must always have the same length and the same order
|
int). format must be "size". The list must always have the same
|
||||||
to be able to compute differences between values.
|
length and the same order to be able to compute differences between
|
||||||
|
values.
|
||||||
|
|
||||||
Example: ``[('Video memory', 'size', 234902)]``.
|
Example: ``[('Video memory', 'size', 234902)]``.
|
||||||
|
|
||||||
filter_filenames(patterns: str|list, include: bool) method:
|
``filter_filenames(patterns: str|list, include: bool)`` method:
|
||||||
|
|
||||||
Remove filenames not matching any pattern if include is True, or remove
|
Remove filenames not matching any pattern if include is True, or
|
||||||
filenames matching a pattern if include is False (exclude). See
|
remove filenames matching a pattern if include is False (exclude).
|
||||||
fnmatch.fnmatch() for the syntax of patterns.
|
See fnmatch.fnmatch() for the syntax of patterns.
|
||||||
|
|
||||||
write(filename) method:
|
``write(filename)`` method:
|
||||||
|
|
||||||
Write the snapshot into a file.
|
Write the snapshot into a file.
|
||||||
|
|
||||||
load(filename), classmethod:
|
``load(filename)`` classmethod:
|
||||||
|
|
||||||
Load a snapshot from a file.
|
Load a snapshot from a file.
|
||||||
|
|
||||||
pid attribute:
|
``process_memory`` attribute:
|
||||||
|
|
||||||
Identifier of the process which created the snapshot (int).
|
Memory usage of the process, result of ``get_process_memory()``.
|
||||||
|
It can be ``None``.
|
||||||
|
|
||||||
stats attribute:
|
``user_data`` attribute:
|
||||||
|
|
||||||
Raw memory allocation statistics (dict).
|
Optional list of user data, result of *user_data_callback* in
|
||||||
|
``Snapshot.create()`` (default: None).
|
||||||
|
|
||||||
timestamp attribute:
|
``pid`` attribute:
|
||||||
|
|
||||||
Date and time of the creation of the snapshot (str).
|
Identifier of the process which created the snapshot (int).
|
||||||
|
|
||||||
|
``stats`` attribute:
|
||||||
|
|
||||||
|
Raw memory allocation statistics (dict).
|
||||||
|
|
||||||
|
``timestamp`` attribute:
|
||||||
|
|
||||||
|
Date and time of the creation of the snapshot (str).
|
||||||
|
|
||||||
|
|
||||||
TakeSnapshot class
|
TakeSnapshot class
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
TakeSnapshot class:
|
``TakeSnapshot`` class:
|
||||||
|
|
||||||
Task taking snapshots of Python memory allocations: write them into files.
|
Task taking snapshots of Python memory allocations: write them into files.
|
||||||
|
|
||||||
start(delay: int) method:
|
``start(delay: int)`` method:
|
||||||
|
|
||||||
Start a task taking a snapshot every delay seconds.
|
Start a task taking a snapshot every delay seconds.
|
||||||
|
|
||||||
stop() method:
|
``stop()`` method:
|
||||||
|
|
||||||
Stop the task started by the ``TakeSnapshot.start()`` method.
|
Stop the task started by the ``TakeSnapshot.start()`` method.
|
||||||
|
|
||||||
take_snapshot() method:
|
``take_snapshot()`` method:
|
||||||
|
|
||||||
Take a snapshot.
|
Take a snapshot.
|
||||||
|
|
||||||
filename_template attribute:
|
``filename_template`` attribute:
|
||||||
|
|
||||||
Template (str) to create a filename. "Variables" can be used in the
|
Template (str) to create a filename. "Variables" can be used in the
|
||||||
template:
|
template:
|
||||||
|
|
||||||
* ``$pid``: identifier of the current process
|
* ``$pid``: identifier of the current process
|
||||||
* ``$timestamp``: current date and time
|
* ``$timestamp``: current date and time
|
||||||
* ``$counter``: counter starting at 1 and incremented at each snapshot
|
* ``$counter``: counter starting at 1 and incremented at each snapshot
|
||||||
|
|
||||||
user_data_callback attribute:
|
``user_data_callback`` attribute:
|
||||||
|
|
||||||
Optional callback collecting user data (callable, default: None).
|
Optional callback collecting user data (callable, default: None).
|
||||||
See ``Snapshot.create()``.
|
See ``Snapshot.create()``.
|
||||||
|
|
||||||
|
|
||||||
Links
|
Links
|
||||||
|
|
Loading…
Reference in New Issue