python-peps/pep-0454.txt

466 lines
12 KiB
Plaintext
Raw Normal View History

PEP: 454
Title: Add a new tracemalloc module to trace Python memory allocations
Version: $Revision$
Last-Modified: $Date$
Author: Victor Stinner <victor.stinner@gmail.com>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 3-September-2013
Python-Version: 3.4
Abstract
========
2013-09-02 18:53:13 -04:00
Add a new ``tracemalloc`` module to trace Python memory allocations.
Rationale
=========
2013-09-03 19:19:30 -04:00
Common debug tools tracing memory allocations read the C filename and
number. Using such tool to analyze Python memory allocations does not
help because most memory block are allocated in the same C function,
in ``PyMem_Malloc()`` for example.
2013-09-03 19:19:30 -04:00
There are debug tools dedicated to the Python language like ``Heapy``
2013-09-03 19:19:30 -04:00
and ``PySizer``. These projects analyze objects type and/or content.
These tools are useful when most memory leaks are instances of the
same type and this type is only instancied in a few functions. The
2013-09-03 19:19:30 -04:00
problem is when the object type is very common like ``str`` or
``tuple``, and it is hard to identify where these objects are
instancied.
2013-09-03 19:19:30 -04:00
Finding reference cycles is also a difficult problem. There are
different tools to draw a diagram of all references. These tools cannot
be used on large applications with thousands of objects because the
diagram is too huge to be analyzed manually.
2013-09-03 19:19:30 -04:00
Proposal
========
Using the PEP 445, it becomes easy to setup an hook on Python memory
allocators. The hook can inspect the current Python frame to get the
Python filename and line number.
This PEP proposes to add a new ``tracemalloc`` module. It is a debug
tool to trace memory allocations made by Python. The module provides the
following information:
2013-09-03 07:18:48 -04:00
* Statistics on Python memory allocations per Python filename and line
number: size, number, and average size of allocations
* Compute differences between two snapshots of Python memory allocations
* Location of a Python memory allocation: size in bytes, Python filename
and line number
2013-09-03 19:19:30 -04:00
2013-09-02 18:53:13 -04:00
API
===
To trace the most Python memory allocations, the module should be
enabled as early as possible in your application by calling
``tracemalloc.enable()`` function, by setting the ``PYTHONTRACEMALLOC``
environment variable to ``1``, or by using ``-X tracemalloc`` command
line option.
2013-09-05 17:15:45 -04:00
By default, tracemalloc only stores one ``frame`` instance per memory
allocation. Use ``tracemalloc.set_number_frame()`` to store more frames.
Functions
---------
2013-09-05 17:15:45 -04:00
``clear_traces()`` function:
2013-09-05 17:15:45 -04:00
Clear all traces and statistics of memory allocations.
2013-09-03 07:18:48 -04:00
``disable()`` function:
2013-09-02 18:53:13 -04:00
Stop tracing Python memory allocations and stop the timer started by
``start_timer()``.
2013-09-05 17:15:45 -04:00
``enable()`` function:
2013-09-05 17:15:45 -04:00
Start tracing Python memory allocations.
``get_number_frame()`` function:
Get the maximum number of frames stored in a trace of a memory
allocation.
2013-09-03 19:19:30 -04:00
``get_object_address(obj)`` function:
Get the address of the memory block of the specified Python object.
2013-09-03 19:19:30 -04:00
``get_object_trace(obj)`` function:
2013-09-03 19:19:30 -04:00
Get the trace of a Python object *obj* as a ``trace`` instance.
2013-09-03 19:19:30 -04:00
Return ``None`` if the tracemalloc module did not save the location
when the object was allocated, for example if the module was
disabled.
2013-09-03 07:18:48 -04:00
``get_process_memory()`` function:
2013-09-02 18:53:13 -04:00
Get the memory usage of the current process as a meminfo namedtuple
with two attributes:
2013-09-02 18:53:13 -04:00
* ``rss``: Resident Set Size in bytes
* ``vms``: size of the virtual memory in bytes
2013-09-02 18:53:13 -04:00
Return ``None`` if the platform is not supported.
2013-09-02 18:53:13 -04:00
Use the ``psutil`` module if available.
2013-09-03 19:19:30 -04:00
``get_stats()`` function:
Get statistics on Python memory allocations per Python filename and
per Python line number.
Return a dictionary
``{filename: str -> {line_number: int -> stats: line_stat}}``
where *stats* in a ``line_stat`` instance. *filename* and
*line_number* can be ``None``.
Return an empty dictionary if the tracemalloc module is disabled.
``get_traces(obj)`` function:
2013-09-05 17:15:45 -04:00
Get all traces of a Python memory allocations.
Return a dictionary ``{pointer: int -> trace}`` where *trace*
is a ``trace`` instance.
Return an empty dictionary if the ``tracemalloc`` module is disabled.
``is_enabled()`` function:
Get the status of the module: ``True`` if it is enabled, ``False``
otherwise.
``set_number_frame(nframe: int)`` function:
2013-09-03 19:19:30 -04:00
2013-09-05 17:15:45 -04:00
Set the maximum number of frames stored in a trace of a memory
allocation.
2013-09-03 19:19:30 -04:00
2013-09-05 17:15:45 -04:00
All traces and statistics of memory allocations are cleared.
2013-09-03 19:19:30 -04:00
2013-09-03 07:18:48 -04:00
``start_timer(delay: int, func: callable, args: tuple=(), kwargs: dict={})`` function:
2013-09-02 18:53:13 -04:00
Start a timer calling ``func(*args, **kwargs)`` every *delay*
seconds.
2013-09-02 18:53:13 -04:00
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
called exactly after *delay* seconds if no Python memory allocation
occurred.
2013-09-02 18:53:13 -04:00
If ``start_timer()`` is called twice, previous parameters are
replaced. The timer has a resolution of 1 second.
2013-09-03 07:18:48 -04:00
``start_timer()`` is used by ``DisplayTop`` and ``TakeSnapshot`` to
2013-09-02 18:53:13 -04:00
run regulary a task.
2013-09-03 07:18:48 -04:00
``stop_timer()`` function:
2013-09-02 18:53:13 -04:00
Stop the timer started by ``start_timer()``.
frame class
-----------
``frame`` class:
Trace of a Python frame.
``filename`` attribute (``str``):
Python filename, ``None`` if unknown.
``lineno`` attribute (``int``):
Python line number, ``None`` if unknown.
2013-09-03 19:19:30 -04:00
trace class
-----------
``trace`` class:
This class represents debug information of an allocated memory block.
``size`` attribute (``int``):
2013-09-03 19:19:30 -04:00
Size in bytes of the memory block.
``frames`` attribute (``list``):
2013-09-03 19:19:30 -04:00
Traceback where the memory block was allocated as a list of
``frame`` instances (most recent first).
2013-09-03 19:19:30 -04:00
The list can be empty or incomplete if the tracemalloc module was
unable to retrieve the full traceback.
2013-09-03 19:19:30 -04:00
For efficiency, the traceback is truncated to 10 frames.
2013-09-03 19:19:30 -04:00
line_stat class
----------------
``line_stat`` class:
Statistics on Python memory allocations of a specific line number.
``size`` attribute (``int``):
2013-09-03 19:19:30 -04:00
Total size in bytes of all memory blocks allocated on the line.
``count`` attribute (``int``):
2013-09-03 19:19:30 -04:00
Number of memory blocks allocated on the line.
2013-09-02 18:41:20 -04:00
DisplayTop class
----------------
2013-09-03 19:19:30 -04:00
``DisplayTop(count: int=10, file=sys.stdout)`` class:
2013-09-03 07:18:48 -04:00
Display the list of the *count* biggest memory allocations into
*file*.
2013-09-02 18:53:13 -04:00
``display()`` method:
2013-09-03 07:18:48 -04:00
Display the top once.
2013-09-02 18:53:13 -04:00
``start(delay: int)`` method:
2013-09-03 07:18:48 -04:00
Start a task using ``tracemalloc`` timer to display the top every
*delay* seconds.
2013-09-02 18:53:13 -04:00
``stop()`` method:
2013-09-02 18:53:13 -04:00
Stop the task started by the ``DisplayTop.start()`` method
``color`` attribute (``bool``, default: ``file.isatty()``):
If ``True``, ``display()`` uses color.
``compare_with_previous`` attribute (``bool``, default: ``True``):
If ``True``, ``display()`` compares with the
2013-09-03 19:19:30 -04:00
previous snapshot. If ``False``, compare with the first snapshot.
``filename_parts`` attribute (``int``, default: ``3``):
Number of displayed filename parts. Extra parts are replaced
with ``"..."``.
``group_per_file`` attribute (``bool``, default: ``False``):
2013-09-03 19:19:30 -04:00
If ``True``, group memory allocations per Python filename. If
``False``, group allocation per Python line number.
``show_average`` attribute (``bool``, default: ``True``):
If ``True``, ``display()`` shows the average size
2013-09-03 19:19:30 -04:00
of allocations.
``show_count`` attribute (``bool``, default: ``True``):
If ``True``, ``display()`` shows the number of
2013-09-03 19:19:30 -04:00
allocations.
``show_size`` attribute (``bool``, default: ``True``):
If ``True``, ``display()`` shows the size of
2013-09-03 19:19:30 -04:00
allocations.
``user_data_callback`` attribute (``callable``, default: ``None``):
Optional callback collecting user data. See ``Snapshot.create()``.
2013-09-02 18:41:20 -04:00
Snapshot class
--------------
2013-09-02 18:53:13 -04:00
``Snapshot()`` class:
2013-09-03 07:18:48 -04:00
Snapshot of Python memory allocations.
Use ``TakeSnapshot`` to take regulary snapshots.
2013-09-02 18:53:13 -04:00
``create(user_data_callback=None)`` method:
2013-09-03 07:18:48 -04:00
Take a snapshot. If *user_data_callback* is specified, it must be a
2013-09-03 19:19:30 -04:00
callable object returning a list of
``(title: str, format: str, value: int)``.
*format* must be ``'size'``. The list must always have the same
2013-09-02 18:53:13 -04:00
length and the same order to be able to compute differences between
values.
Example: ``[('Video memory', 'size', 234902)]``.
2013-09-03 07:18:48 -04:00
``filter_filenames(patterns: list, include: bool)`` method:
Remove filenames not matching any pattern of *patterns* if *include*
is ``True``, or remove filenames matching a pattern of *patterns* if
*include* is ``False`` (exclude).
2013-09-03 07:18:48 -04:00
See ``fnmatch.fnmatch()`` for the syntax of a pattern.
2013-09-02 18:53:13 -04:00
``load(filename)`` classmethod:
2013-09-02 18:53:13 -04:00
Load a snapshot from a file.
2013-09-03 19:19:30 -04:00
``write(filename)`` method:
2013-09-02 18:41:20 -04:00
2013-09-03 19:19:30 -04:00
Write the snapshot into a file.
2013-09-02 18:41:20 -04:00
``pid`` attribute (``int``):
Identifier of the process which created the snapshot.
2013-09-03 19:19:30 -04:00
``process_memory`` attribute:
Result of the ``get_process_memory()`` function, can be ``None``.
``stats`` attribute (``dict``):
Result of the ``get_stats()`` function.
``timestamp`` attribute (``datetime.datetime``):
Creation date and time of the snapshot.
2013-09-03 19:19:30 -04:00
``user_data`` attribute (``list``, default: ``None``):
2013-09-03 19:19:30 -04:00
Optional list of user data, result of *user_data_callback* in
``Snapshot.create()``.
2013-09-02 18:41:20 -04:00
TakeSnapshot class
------------------
2013-09-02 18:53:13 -04:00
``TakeSnapshot`` class:
2013-09-03 07:18:48 -04:00
Task taking snapshots of Python memory allocations: write them into
files. By default, snapshots are written in the current directory.
2013-09-02 18:53:13 -04:00
``start(delay: int)`` method:
2013-09-02 18:53:13 -04:00
Start a task taking a snapshot every delay seconds.
2013-09-02 18:53:13 -04:00
``stop()`` method:
2013-09-02 18:53:13 -04:00
Stop the task started by the ``TakeSnapshot.start()`` method.
2013-09-02 18:53:13 -04:00
``take_snapshot()`` method:
2013-09-02 18:53:13 -04:00
Take a snapshot.
``filename_template`` attribute (``str``,
default: ``'tracemalloc-$counter.pickle'``):
Template used to create a filename. The following variables can be
used in the template:
2013-09-02 18:53:13 -04:00
* ``$pid``: identifier of the current process
* ``$timestamp``: current date and time
* ``$counter``: counter starting at 1 and incremented at each snapshot
``user_data_callback`` attribute (``callable``, default: ``None``):
Optional callback collecting user data. See ``Snapshot.create()``.
Command line options
====================
The ``python -m tracemalloc`` command can be used to analyze and compare
snapshots. The command takes a list of snapshot filenames and has the
following options.
``-g``, ``--group-per-file``
Group allocations per filename, instead of grouping per line number.
``-n NTRACES``, ``--number NTRACES``
Number of traces displayed per top (default: 10).
``--first``
Compare with the first snapshot, instead of comparing with the
previous snapshot.
``--include PATTERN``
2013-09-03 07:18:48 -04:00
Only include filenames matching pattern *PATTERN*. The option can be
specified multiple times.
See ``fnmatch.fnmatch()`` for the syntax of patterns.
``--exclude PATTERN``
Exclude filenames matching pattern *PATTERN*. The option can be
specified multiple times.
See ``fnmatch.fnmatch()`` for the syntax of patterns.
``-S``, ``--hide-size``
Hide the size of allocations.
``-C``, ``--hide-count``
Hide the number of allocations.
``-A``, ``--hide-average``
Hide the average size of allocations.
``-P PARTS``, ``--filename-parts=PARTS``
Number of displayed filename parts (default: 3).
``--color``
Force usage of colors even if ``sys.stdout`` is not a TTY device.
``--no-color``
Disable colors if ``sys.stdout`` is a TTY device.
Links
=====
tracemalloc:
2013-09-02 18:41:20 -04:00
* `#18874: Add a new tracemalloc module to trace Python
memory allocations <http://bugs.python.org/issue18874>`_
* `pytracemalloc on PyPI
<https://pypi.python.org/pypi/pytracemalloc>`_
Similar projects:
* `Meliae: Python Memory Usage Analyzer
<https://pypi.python.org/pypi/meliae>`_
* `Guppy-PE: umbrella package combining Heapy and GSL
<http://guppy-pe.sourceforge.net/>`_
* `PySizer <http://pysizer.8325.org/>`_: developed for Python 2.4
* `memory_profiler <https://pypi.python.org/pypi/memory_profiler>`_
* `pympler <http://code.google.com/p/pympler/>`_
* `Dozer <https://pypi.python.org/pypi/Dozer>`_: WSGI Middleware version of
the CherryPy memory leak debugger
* `objgraph <http://mg.pov.lt/objgraph/>`_
* `caulk <https://github.com/smartfile/caulk/>`_
Copyright
=========
This document has been placed into the public domain.