2013-09-02 18:35:54 -04:00
|
|
|
|
PEP: 454
|
|
|
|
|
Title: Add a new tracemalloc module to trace Python memory allocations
|
|
|
|
|
Version: $Revision$
|
|
|
|
|
Last-Modified: $Date$
|
2019-10-17 20:48:46 -04:00
|
|
|
|
Author: Victor Stinner <vstinner@python.org>
|
2013-11-21 16:55:10 -05:00
|
|
|
|
BDFL-Delegate: Charles-François Natali <cf.natali@gmail.com>
|
2013-11-23 19:14:08 -05:00
|
|
|
|
Status: Final
|
2013-09-02 18:35:54 -04:00
|
|
|
|
Type: Standards Track
|
|
|
|
|
Content-Type: text/x-rst
|
2021-02-09 11:54:26 -05:00
|
|
|
|
Created: 03-Sep-2013
|
2013-09-02 18:35:54 -04:00
|
|
|
|
Python-Version: 3.4
|
2013-11-21 16:55:10 -05:00
|
|
|
|
Resolution: https://mail.python.org/pipermail/python-dev/2013-November/130491.html
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
|
========
|
|
|
|
|
|
2013-10-08 09:54:15 -04:00
|
|
|
|
This PEP proposes to add a new ``tracemalloc`` module to trace memory
|
|
|
|
|
blocks allocated by Python.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
|
=========
|
|
|
|
|
|
2013-10-22 07:57:53 -04:00
|
|
|
|
Classic generic tools like Valgrind can get the C traceback where a
|
|
|
|
|
memory block was allocated. Using such tools to analyze Python memory
|
|
|
|
|
allocations does not help because most memory blocks are allocated in
|
|
|
|
|
the same C function, in ``PyMem_Malloc()`` for example. Moreover, Python
|
2013-10-30 06:24:34 -04:00
|
|
|
|
has an allocator for small objects called "pymalloc" which keeps free
|
2013-10-22 07:57:53 -04:00
|
|
|
|
blocks for efficiency. This is not well handled by these tools.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
|
2013-09-03 20:02:50 -04:00
|
|
|
|
There are debug tools dedicated to the Python language like ``Heapy``
|
2013-10-30 06:24:34 -04:00
|
|
|
|
``Pympler`` and ``Meliae`` which lists all alive objects using the
|
|
|
|
|
garbage collector module (functions like ``gc.get_objects()``,
|
2013-10-22 07:57:53 -04:00
|
|
|
|
``gc.get_referrers()`` and ``gc.get_referents()``), compute their size
|
|
|
|
|
(ex: using ``sys.getsizeof()``) and group objects by type. These tools
|
|
|
|
|
provide a better estimation of the memory usage of an application. They
|
|
|
|
|
are useful when most memory leaks are instances of the same type and
|
|
|
|
|
this type is only instantiated in a few functions. Problems arise when
|
|
|
|
|
the object type is very common like ``str`` or ``tuple``, and it is hard
|
|
|
|
|
to identify where these objects are instantiated.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
|
2013-10-08 09:54:15 -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
|
|
|
|
|
========
|
|
|
|
|
|
2013-10-08 09:54:15 -04:00
|
|
|
|
Using the customized allocation API from PEP 445, it becomes easy to
|
|
|
|
|
set up a hook on Python memory allocators. A hook can inspect Python
|
2013-10-22 11:04:24 -04:00
|
|
|
|
internals to retrieve Python tracebacks. The idea of getting the current
|
|
|
|
|
traceback comes from the faulthandler module. The faulthandler dumps
|
|
|
|
|
the traceback of all Python threads on a crash, here is the idea is to
|
|
|
|
|
get the traceback of the current Python thread when a memory block is
|
|
|
|
|
allocated by Python.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
|
2013-10-27 13:26:47 -04:00
|
|
|
|
This PEP proposes to add a new ``tracemalloc`` module, a debug tool
|
2013-10-18 07:54:17 -04:00
|
|
|
|
to trace memory blocks allocated by Python. The module provides the
|
2013-09-03 19:19:30 -04:00
|
|
|
|
following information:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-10-27 13:26:47 -04:00
|
|
|
|
* Traceback where an object was allocated
|
2013-10-03 10:45:04 -04:00
|
|
|
|
* Statistics on allocated memory blocks per filename and per line
|
|
|
|
|
number: total size, number and average size of allocated memory blocks
|
2013-10-23 14:03:33 -04:00
|
|
|
|
* Computed differences between two snapshots to detect memory leaks
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-11-03 07:50:52 -05:00
|
|
|
|
The API of the tracemalloc module is similar to the API of the faulthandler
|
|
|
|
|
module: ``enable()`` / ``start()``, ``disable()`` / ``stop()`` and
|
|
|
|
|
``is_enabled()`` / ``is_tracing()`` functions, an environment variable
|
|
|
|
|
(``PYTHONFAULTHANDLER`` and ``PYTHONTRACEMALLOC``), and a ``-X`` command line
|
|
|
|
|
option (``-X faulthandler`` and ``-X tracemalloc``). See the `documentation of
|
|
|
|
|
the faulthandler module <http://docs.python.org/3/library/faulthandler.html>`_.
|
2013-09-05 17:23:35 -04:00
|
|
|
|
|
2013-10-22 07:57:53 -04:00
|
|
|
|
The idea of tracing memory allocations is not new. It was first
|
|
|
|
|
implemented in the PySizer project in 2005. PySizer was implemented
|
|
|
|
|
differently: the traceback was stored in frame objects and some Python
|
|
|
|
|
types were linked the trace with the name of object type. PySizer patch
|
2016-05-03 06:52:22 -04:00
|
|
|
|
on CPython adds an overhead on performances and memory footprint, even if
|
2021-09-17 14:18:24 -04:00
|
|
|
|
the PySizer was not used. tracemalloc attaches a traceback to the
|
2013-10-22 07:57:53 -04:00
|
|
|
|
underlying layer, to memory blocks, and has no overhead when the module
|
2013-11-03 07:50:52 -05:00
|
|
|
|
is not tracing memory allocations.
|
2013-10-22 07:57:53 -04:00
|
|
|
|
|
2013-09-05 17:26:55 -04:00
|
|
|
|
The tracemalloc module has been written for CPython. Other
|
2013-10-08 09:54:15 -04:00
|
|
|
|
implementations of Python may not be able to provide it.
|
2013-09-05 17:26:55 -04:00
|
|
|
|
|
2013-09-03 19:19:30 -04:00
|
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
|
API
|
|
|
|
|
===
|
|
|
|
|
|
2013-10-23 14:12:42 -04:00
|
|
|
|
To trace most memory blocks allocated by Python, the module should be
|
2013-11-03 07:50:52 -05:00
|
|
|
|
started as early as possible by setting the ``PYTHONTRACEMALLOC``
|
2013-10-23 14:12:42 -04:00
|
|
|
|
environment variable to ``1``, or by using ``-X tracemalloc`` command
|
2013-11-03 07:50:52 -05:00
|
|
|
|
line option. The ``tracemalloc.start()`` function can be called at
|
2013-10-23 14:12:42 -04:00
|
|
|
|
runtime to start tracing Python memory allocations.
|
|
|
|
|
|
|
|
|
|
By default, a trace of an allocated memory block only stores the most
|
|
|
|
|
recent frame (1 frame). To store 25 frames at startup: set the
|
|
|
|
|
``PYTHONTRACEMALLOC`` environment variable to ``25``, or use the ``-X
|
|
|
|
|
tracemalloc=25`` command line option. The ``set_traceback_limit()``
|
|
|
|
|
function can be used at runtime to set the limit.
|
|
|
|
|
|
|
|
|
|
|
2013-11-08 21:24:36 -05:00
|
|
|
|
Functions
|
|
|
|
|
---------
|
2013-09-08 09:58:01 -04:00
|
|
|
|
|
2013-10-30 06:24:34 -04:00
|
|
|
|
``clear_traces()`` function:
|
2013-10-23 14:03:33 -04:00
|
|
|
|
|
2013-10-27 12:22:57 -04:00
|
|
|
|
Clear traces of memory blocks allocated by Python.
|
2013-09-16 19:38:43 -04:00
|
|
|
|
|
2013-11-03 07:50:52 -05:00
|
|
|
|
See also ``stop()``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
|
|
2013-11-08 21:24:36 -05:00
|
|
|
|
``get_object_traceback(obj)`` function:
|
|
|
|
|
|
|
|
|
|
Get the traceback where the Python object *obj* was allocated.
|
|
|
|
|
Return a ``Traceback`` instance, or ``None`` if the ``tracemalloc``
|
|
|
|
|
module is not tracing memory allocations or did not trace the
|
|
|
|
|
allocation of the object.
|
|
|
|
|
|
|
|
|
|
See also ``gc.get_referrers()`` and ``sys.getsizeof()`` functions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
``get_traceback_limit()`` function:
|
|
|
|
|
|
|
|
|
|
Get the maximum number of frames stored in the traceback of a trace.
|
|
|
|
|
|
2013-11-25 05:53:03 -05:00
|
|
|
|
The ``tracemalloc`` module must be tracing memory allocations to get
|
|
|
|
|
the limit, otherwise an exception is raised.
|
2013-11-08 21:24:36 -05:00
|
|
|
|
|
2013-11-25 05:53:03 -05:00
|
|
|
|
The limit is set by the ``start()`` function.
|
2013-11-08 21:24:36 -05:00
|
|
|
|
|
|
|
|
|
|
2013-10-18 07:54:17 -04:00
|
|
|
|
``get_traced_memory()`` function:
|
|
|
|
|
|
|
|
|
|
Get the current size and maximum size of memory blocks traced by the
|
|
|
|
|
``tracemalloc`` module as a tuple: ``(size: int, max_size: int)``.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
``get_tracemalloc_memory()`` function:
|
|
|
|
|
|
2013-10-27 19:20:26 -04:00
|
|
|
|
Get the memory usage in bytes of the ``tracemalloc`` module used to
|
|
|
|
|
store traces of memory blocks. Return an ``int``.
|
2013-09-05 17:15:45 -04:00
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
|
|
2013-11-03 07:50:52 -05:00
|
|
|
|
``is_tracing()`` function:
|
2013-11-01 07:05:37 -04:00
|
|
|
|
|
2013-11-03 07:50:52 -05:00
|
|
|
|
``True`` if the ``tracemalloc`` module is tracing Python memory
|
|
|
|
|
allocations, ``False`` otherwise.
|
2013-11-01 07:05:37 -04:00
|
|
|
|
|
2013-11-03 07:50:52 -05:00
|
|
|
|
See also ``start()`` and ``stop()`` functions.
|
2013-11-01 07:05:37 -04:00
|
|
|
|
|
|
|
|
|
|
2013-11-25 05:53:03 -05:00
|
|
|
|
``start(nframe: int=1)`` function:
|
2013-11-08 21:24:36 -05:00
|
|
|
|
|
2013-11-25 05:53:03 -05:00
|
|
|
|
Start tracing Python memory allocations: install hooks on Python
|
|
|
|
|
memory allocators. Collected tracebacks of traces will be limited to
|
|
|
|
|
*nframe* frames. By default, a trace of a memory block only stores
|
|
|
|
|
the most recent frame: the limit is ``1``. *nframe* must be greater
|
|
|
|
|
or equal to ``1``.
|
2013-11-08 21:24:36 -05:00
|
|
|
|
|
|
|
|
|
Storing more than ``1`` frame is only useful to compute statistics
|
|
|
|
|
grouped by ``'traceback'`` or to compute cumulative statistics: see
|
2013-11-25 05:53:03 -05:00
|
|
|
|
the ``Snapshot.compare_to()`` and ``Snapshot.statistics()`` methods.
|
2013-11-08 21:24:36 -05:00
|
|
|
|
|
2013-11-25 05:53:03 -05:00
|
|
|
|
Storing more frames increases the memory and CPU overhead of the
|
|
|
|
|
``tracemalloc`` module. Use the ``get_tracemalloc_memory()``
|
|
|
|
|
function to measure how much memory is used by the ``tracemalloc``
|
|
|
|
|
module.
|
2013-11-08 21:24:36 -05:00
|
|
|
|
|
|
|
|
|
The ``PYTHONTRACEMALLOC`` environment variable
|
|
|
|
|
(``PYTHONTRACEMALLOC=NFRAME``) and the ``-X`` ``tracemalloc=NFRAME``
|
2013-11-25 05:53:03 -05:00
|
|
|
|
command line option can be used to start tracing at startup.
|
2013-11-05 16:04:26 -05:00
|
|
|
|
|
2013-11-25 05:53:03 -05:00
|
|
|
|
See also ``stop()``, ``is_tracing()`` and ``get_traceback_limit()``
|
|
|
|
|
functions.
|
2013-11-05 16:04:26 -05:00
|
|
|
|
|
|
|
|
|
|
2013-11-01 07:05:37 -04:00
|
|
|
|
``stop()`` function:
|
|
|
|
|
|
2013-11-08 21:13:58 -05:00
|
|
|
|
Stop tracing Python memory allocations: uninstall hooks on Python
|
|
|
|
|
memory allocators. Clear also traces of memory blocks allocated by
|
|
|
|
|
Python
|
2013-11-01 07:05:37 -04:00
|
|
|
|
|
2013-11-08 12:06:37 -05:00
|
|
|
|
Call ``take_snapshot()`` function to take a snapshot of traces
|
2013-11-03 07:50:52 -05:00
|
|
|
|
before clearing them.
|
2013-11-01 07:05:37 -04:00
|
|
|
|
|
2013-11-03 07:50:52 -05:00
|
|
|
|
See also ``start()`` and ``is_tracing()`` functions.
|
2013-09-08 09:15:26 -04:00
|
|
|
|
|
|
|
|
|
|
2013-10-30 20:23:31 -04:00
|
|
|
|
``take_snapshot()`` function:
|
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
Take a snapshot of traces of memory blocks allocated by Python.
|
|
|
|
|
Return a new ``Snapshot`` instance.
|
|
|
|
|
|
|
|
|
|
The snapshot does not include memory blocks allocated before the
|
2013-11-08 21:13:58 -05:00
|
|
|
|
``tracemalloc`` module started to trace memory allocations.
|
2013-11-08 10:25:13 -05:00
|
|
|
|
|
2013-11-08 12:06:37 -05:00
|
|
|
|
Tracebacks of traces are limited to ``get_traceback_limit()``
|
2013-11-25 05:53:03 -05:00
|
|
|
|
frames. Use the *nframe* parameter of the ``start()`` function to
|
|
|
|
|
store more frames.
|
2013-10-30 20:23:31 -04:00
|
|
|
|
|
2013-11-04 02:50:35 -05:00
|
|
|
|
The ``tracemalloc`` module must be tracing memory allocations to
|
2016-05-03 05:03:16 -04:00
|
|
|
|
take a snapshot, see the ``start()`` function.
|
2013-10-30 20:23:31 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
See also the ``get_object_traceback()`` function.
|
2013-10-30 20:23:31 -04:00
|
|
|
|
|
|
|
|
|
|
2013-10-03 10:00:07 -04:00
|
|
|
|
Filter
|
|
|
|
|
------
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-10-30 19:13:57 -04:00
|
|
|
|
``Filter(inclusive: bool, filename_pattern: str, lineno: int=None, all_frames: bool=False)`` class:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-11-08 21:13:58 -05:00
|
|
|
|
Filter on traces of memory blocks.
|
|
|
|
|
|
|
|
|
|
See the ``fnmatch.fnmatch()`` function for the syntax of
|
|
|
|
|
*filename_pattern*. The ``'.pyc'`` and ``'.pyo'`` file extensions
|
|
|
|
|
are replaced with ``'.py'``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-11-08 21:13:58 -05:00
|
|
|
|
Examples:
|
2013-10-03 10:00:07 -04:00
|
|
|
|
|
2013-11-08 21:13:58 -05:00
|
|
|
|
* ``Filter(True, subprocess.__file__)`` only includes traces of the
|
|
|
|
|
``subprocess`` module
|
2013-11-08 21:24:36 -05:00
|
|
|
|
* ``Filter(False, tracemalloc.__file__)`` excludes traces of the
|
|
|
|
|
``tracemalloc`` module
|
|
|
|
|
* ``Filter(False, "<unknown>")`` excludes empty tracebacks
|
2013-10-27 12:22:57 -04:00
|
|
|
|
|
2013-10-29 21:07:37 -04:00
|
|
|
|
``inclusive`` attribute:
|
2013-10-03 10:00:07 -04:00
|
|
|
|
|
2013-10-29 21:07:37 -04:00
|
|
|
|
If *inclusive* is ``True`` (include), only trace memory blocks
|
|
|
|
|
allocated in a file with a name matching ``filename_pattern`` at
|
|
|
|
|
line number ``lineno``.
|
2013-09-16 19:38:43 -04:00
|
|
|
|
|
2013-10-29 21:07:37 -04:00
|
|
|
|
If *inclusive* is ``False`` (exclude), ignore memory blocks
|
|
|
|
|
allocated in a file with a name matching ``filename_pattern`` at
|
|
|
|
|
line number ``lineno``.
|
2013-09-16 19:38:43 -04:00
|
|
|
|
|
|
|
|
|
``lineno`` attribute:
|
|
|
|
|
|
2013-10-27 12:22:57 -04:00
|
|
|
|
Line number (``int``) of the filter. If *lineno* is ``None``, the
|
|
|
|
|
filter matches any line number.
|
2013-10-03 10:00:07 -04:00
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
``filename_pattern`` attribute:
|
2013-10-03 10:00:07 -04:00
|
|
|
|
|
2013-11-08 12:06:37 -05:00
|
|
|
|
Filename pattern of the filter (``str``).
|
2013-09-16 19:38:43 -04:00
|
|
|
|
|
2013-10-30 19:13:57 -04:00
|
|
|
|
``all_frames`` attribute:
|
2013-09-16 19:38:43 -04:00
|
|
|
|
|
2013-10-30 19:13:57 -04:00
|
|
|
|
If *all_frames* is ``True``, all frames of the traceback are
|
|
|
|
|
checked. If *all_frames* is ``False``, only the most recent frame is
|
|
|
|
|
checked.
|
2013-09-16 19:38:43 -04:00
|
|
|
|
|
2013-10-03 10:45:04 -04:00
|
|
|
|
This attribute is ignored if the traceback limit is less than ``2``.
|
2013-11-08 12:06:37 -05:00
|
|
|
|
See the ``get_traceback_limit()`` function and
|
|
|
|
|
``Snapshot.traceback_limit`` attribute.
|
2013-09-16 19:38:43 -04:00
|
|
|
|
|
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
Frame
|
|
|
|
|
-----
|
|
|
|
|
|
|
|
|
|
``Frame`` class:
|
|
|
|
|
|
|
|
|
|
Frame of a traceback.
|
|
|
|
|
|
|
|
|
|
The ``Traceback`` class is a sequence of ``Frame`` instances.
|
|
|
|
|
|
|
|
|
|
``filename`` attribute:
|
|
|
|
|
|
|
|
|
|
Filename (``str``).
|
|
|
|
|
|
|
|
|
|
``lineno`` attribute:
|
|
|
|
|
|
|
|
|
|
Line number (``int``).
|
|
|
|
|
|
|
|
|
|
|
2013-10-03 10:00:07 -04:00
|
|
|
|
Snapshot
|
|
|
|
|
--------
|
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
``Snapshot`` class:
|
2013-10-03 10:00:07 -04:00
|
|
|
|
|
2013-10-27 12:22:57 -04:00
|
|
|
|
Snapshot of traces of memory blocks allocated by Python.
|
2013-10-03 10:00:07 -04:00
|
|
|
|
|
2013-11-08 12:06:37 -05:00
|
|
|
|
The ``take_snapshot()`` function creates a snapshot instance.
|
2013-10-27 19:20:26 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
``compare_to(old_snapshot: Snapshot, group_by: str, cumulative: bool=False)`` method:
|
|
|
|
|
|
2013-11-08 12:06:37 -05:00
|
|
|
|
Compute the differences with an old snapshot. Get statistics as a
|
|
|
|
|
sorted list of ``StatisticDiff`` instances grouped by *group_by*.
|
2013-11-08 10:25:13 -05:00
|
|
|
|
|
|
|
|
|
See the ``statistics()`` method for *group_by* and *cumulative*
|
|
|
|
|
parameters.
|
|
|
|
|
|
|
|
|
|
The result is sorted from the biggest to the smallest by: absolute
|
|
|
|
|
value of ``StatisticDiff.size_diff``, ``StatisticDiff.size``,
|
|
|
|
|
absolute value of ``StatisticDiff.count_diff``, ``Statistic.count``
|
|
|
|
|
and then by ``StatisticDiff.traceback``.
|
|
|
|
|
|
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
``dump(filename)`` method:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
Write the snapshot into a file.
|
|
|
|
|
|
|
|
|
|
Use ``load()`` to reload the snapshot.
|
2013-09-03 07:18:48 -04:00
|
|
|
|
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-11-12 07:38:36 -05:00
|
|
|
|
``filter_traces(filters)`` method:
|
|
|
|
|
|
|
|
|
|
Create a new ``Snapshot`` instance with a filtered ``traces``
|
|
|
|
|
sequence, *filters* is a list of ``Filter`` instances. If *filters*
|
|
|
|
|
is an empty list, return a new ``Snapshot`` instance with a copy of
|
|
|
|
|
the traces.
|
|
|
|
|
|
|
|
|
|
All inclusive filters are applied at once, a trace is ignored if no
|
|
|
|
|
inclusive filters match it. A trace is ignored if at least one
|
2021-09-17 14:18:24 -04:00
|
|
|
|
exclusive filter matches it.
|
2013-11-12 07:38:36 -05:00
|
|
|
|
|
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
``load(filename)`` classmethod:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-10-03 10:45:04 -04:00
|
|
|
|
Load a snapshot from a file.
|
2013-10-03 10:00:07 -04:00
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
See also ``dump()``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
``statistics(group_by: str, cumulative: bool=False)`` method:
|
2013-10-29 21:07:37 -04:00
|
|
|
|
|
2013-11-08 12:06:37 -05:00
|
|
|
|
Get statistics as a sorted list of ``Statistic`` instances grouped
|
2013-11-05 16:04:26 -05:00
|
|
|
|
by *group_by*:
|
2013-10-18 07:54:17 -04:00
|
|
|
|
|
2013-11-05 16:04:26 -05:00
|
|
|
|
===================== ========================
|
|
|
|
|
group_by description
|
|
|
|
|
===================== ========================
|
|
|
|
|
``'filename'`` filename
|
|
|
|
|
``'lineno'`` filename and line number
|
|
|
|
|
``'traceback'`` traceback
|
|
|
|
|
===================== ========================
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-10-03 10:45:04 -04:00
|
|
|
|
If *cumulative* is ``True``, cumulate size and count of memory
|
|
|
|
|
blocks of all frames of the traceback of a trace, not only the most
|
2013-11-08 12:06:37 -05:00
|
|
|
|
recent frame. The cumulative mode can only be used with *group_by*
|
|
|
|
|
equals to ``'filename'`` and ``'lineno'`` and ``traceback_limit``
|
|
|
|
|
greater than ``1``.
|
2013-10-29 21:07:37 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
The result is sorted from the biggest to the smallest by:
|
|
|
|
|
``Statistic.size``, ``Statistic.count`` and then by
|
|
|
|
|
``Statistic.traceback``.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
|
``traceback_limit`` attribute:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-11-08 12:06:37 -05:00
|
|
|
|
Maximum number of frames stored in the traceback of ``traces``:
|
|
|
|
|
result of the ``get_traceback_limit()`` when the snapshot was taken.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
|
``traces`` attribute:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
Traces of all memory blocks allocated by Python: sequence of
|
|
|
|
|
``Trace`` instances.
|
|
|
|
|
|
|
|
|
|
The sequence has an undefined order. Use the
|
|
|
|
|
``Snapshot.statistics()`` method to get a sorted list of statistics.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
Statistic
|
2013-10-03 10:00:07 -04:00
|
|
|
|
---------
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
``Statistic`` class:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
Statistic on memory allocations.
|
|
|
|
|
|
2013-10-29 21:07:37 -04:00
|
|
|
|
``Snapshot.statistics()`` returns a list of ``Statistic`` instances.
|
2013-10-23 14:03:33 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
See also the ``StatisticDiff`` class.
|
|
|
|
|
|
|
|
|
|
``count`` attribute:
|
|
|
|
|
|
|
|
|
|
Number of memory blocks (``int``).
|
|
|
|
|
|
|
|
|
|
``size`` attribute:
|
|
|
|
|
|
|
|
|
|
Total size of memory blocks in bytes (``int``).
|
|
|
|
|
|
2013-11-05 16:04:26 -05:00
|
|
|
|
``traceback`` attribute:
|
2013-10-23 14:03:33 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
Traceback where the memory block was allocated, ``Traceback``
|
|
|
|
|
instance.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StatisticDiff
|
|
|
|
|
-------------
|
|
|
|
|
|
|
|
|
|
``StatisticDiff`` class:
|
|
|
|
|
|
|
|
|
|
Statistic difference on memory allocations between an old and a new
|
|
|
|
|
``Snapshot`` instance.
|
|
|
|
|
|
|
|
|
|
``Snapshot.compare_to()`` returns a list of ``StatisticDiff``
|
|
|
|
|
instances. See also the ``Statistic`` class.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
``count`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
Number of memory blocks in the new snapshot (``int``): ``0`` if the
|
|
|
|
|
memory blocks have been released in the new snapshot.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
``count_diff`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
Difference of number of memory blocks between the old and the new
|
|
|
|
|
snapshots (``int``): ``0`` if the memory blocks have been allocated
|
|
|
|
|
in the new snapshot.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
``size`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
Total size of memory blocks in bytes in the new snapshot (``int``):
|
|
|
|
|
``0`` if the memory blocks have been released in the new snapshot.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
2013-10-23 14:03:33 -04:00
|
|
|
|
``size_diff`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
2013-11-08 10:25:13 -05:00
|
|
|
|
Difference of total size of memory blocks in bytes between the old
|
|
|
|
|
and the new snapshots (``int``): ``0`` if the memory blocks have
|
|
|
|
|
been allocated in the new snapshot.
|
|
|
|
|
|
|
|
|
|
``traceback`` attribute:
|
|
|
|
|
|
|
|
|
|
Traceback where the memory blocks were allocated, ``Traceback``
|
|
|
|
|
instance.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Trace
|
|
|
|
|
-----
|
|
|
|
|
|
|
|
|
|
``Trace`` class:
|
|
|
|
|
|
|
|
|
|
Trace of a memory block.
|
|
|
|
|
|
|
|
|
|
The ``Snapshot.traces`` attribute is a sequence of ``Trace``
|
|
|
|
|
instances.
|
|
|
|
|
|
|
|
|
|
``size`` attribute:
|
|
|
|
|
|
|
|
|
|
Size of the memory block in bytes (``int``).
|
|
|
|
|
|
|
|
|
|
``traceback`` attribute:
|
|
|
|
|
|
|
|
|
|
Traceback where the memory block was allocated, ``Traceback``
|
|
|
|
|
instance.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Traceback
|
|
|
|
|
---------
|
|
|
|
|
|
|
|
|
|
``Traceback`` class:
|
|
|
|
|
|
|
|
|
|
Sequence of ``Frame`` instances sorted from the most recent frame to
|
|
|
|
|
the oldest frame.
|
|
|
|
|
|
2013-11-10 17:54:37 -05:00
|
|
|
|
A traceback contains at least ``1`` frame. If the ``tracemalloc`` module
|
|
|
|
|
failed to get a frame, the filename ``"<unknown>"`` at line number ``0`` is
|
|
|
|
|
used.
|
2013-11-08 10:25:13 -05:00
|
|
|
|
|
|
|
|
|
When a snapshot is taken, tracebacks of traces are limited to
|
|
|
|
|
``get_traceback_limit()`` frames. See the ``take_snapshot()``
|
|
|
|
|
function.
|
|
|
|
|
|
|
|
|
|
The ``Trace.traceback`` attribute is an instance of ``Traceback``
|
|
|
|
|
instance.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
2013-09-03 07:18:48 -04:00
|
|
|
|
|
2013-10-30 21:24:25 -04:00
|
|
|
|
Rejected Alternatives
|
|
|
|
|
=====================
|
|
|
|
|
|
|
|
|
|
Log calls to the memory allocator
|
|
|
|
|
---------------------------------
|
|
|
|
|
|
|
|
|
|
A different approach is to log calls to ``malloc()``, ``realloc()`` and
|
|
|
|
|
``free()`` functions. Calls can be logged into a file or send to another
|
|
|
|
|
computer through the network. Example of a log entry: name of the
|
|
|
|
|
function, size of the memory block, address of the memory block, Python
|
|
|
|
|
traceback where the allocation occurred, timestamp.
|
|
|
|
|
|
|
|
|
|
Logs cannot be used directly, getting the current status of the memory
|
|
|
|
|
requires to parse previous logs. For example, it is not possible to get
|
|
|
|
|
directly the traceback of a Python object, like
|
|
|
|
|
``get_object_traceback(obj)`` does with traces.
|
|
|
|
|
|
|
|
|
|
Python uses objects with a very short lifetime and so makes an extensive
|
|
|
|
|
use of memory allocators. It has an allocator optimized for small
|
|
|
|
|
objects (less than 512 bytes) with a short lifetime. For example, the
|
|
|
|
|
Python test suites calls ``malloc()``, ``realloc()`` or ``free()``
|
|
|
|
|
270,000 times per second in average. If the size of log entry is 32
|
|
|
|
|
bytes, logging produces 8.2 MB per second or 29.0 GB per hour.
|
|
|
|
|
|
|
|
|
|
The alternative was rejected because it is less efficient and has less
|
|
|
|
|
features. Parsing logs in a different process or a different computer is
|
|
|
|
|
slower than maintaining traces on allocated memory blocks in the same
|
|
|
|
|
process.
|
|
|
|
|
|
|
|
|
|
|
2013-10-21 20:30:12 -04:00
|
|
|
|
Prior Work
|
|
|
|
|
==========
|
|
|
|
|
|
|
|
|
|
* `Python Memory Validator
|
|
|
|
|
<http://www.softwareverify.com/python/memory/index.html>`_ (2005-2013):
|
|
|
|
|
commercial Python memory validator developed by Software Verification.
|
|
|
|
|
It uses the Python Reflection API.
|
|
|
|
|
* `PySizer <http://pysizer.8325.org/>`_: Google Summer of Code 2005 project by
|
|
|
|
|
Nick Smallbone.
|
|
|
|
|
* `Heapy
|
|
|
|
|
<http://guppy-pe.sourceforge.net/>`_ (2006-2013):
|
|
|
|
|
part of the Guppy-PE project written by Sverker Nilsson.
|
|
|
|
|
* Draft PEP: `Support Tracking Low-Level Memory Usage in CPython
|
|
|
|
|
<http://svn.python.org/projects/python/branches/bcannon-sandboxing/PEP.txt>`_
|
|
|
|
|
(Brett Canon, 2006)
|
|
|
|
|
* Muppy: project developed in 2008 by Robert Schuppenies.
|
|
|
|
|
* `asizeof <http://code.activestate.com/recipes/546530/>`_:
|
|
|
|
|
a pure Python module to estimate the size of objects by Jean
|
|
|
|
|
Brouwers (2008).
|
|
|
|
|
* `Heapmonitor <http://www.scons.org/wiki/LudwigHaehne/HeapMonitor>`_:
|
|
|
|
|
It provides facilities to size individual objects and can track all objects
|
|
|
|
|
of certain classes. It was developed in 2008 by Ludwig Haehne.
|
|
|
|
|
* `Pympler <http://code.google.com/p/pympler/>`_ (2008-2011):
|
|
|
|
|
project based on asizeof, muppy and HeapMonitor
|
|
|
|
|
* `objgraph <http://mg.pov.lt/objgraph/>`_ (2008-2012)
|
|
|
|
|
* `Dozer <https://pypi.python.org/pypi/Dozer>`_: WSGI Middleware version
|
|
|
|
|
of the CherryPy memory leak debugger, written by Marius Gedminas (2008-2013)
|
|
|
|
|
* `Meliae
|
|
|
|
|
<https://pypi.python.org/pypi/meliae>`_:
|
|
|
|
|
Python Memory Usage Analyzer developed by John A Meinel since 2009
|
2013-10-26 03:43:14 -04:00
|
|
|
|
* `gdb-heap <https://fedorahosted.org/gdb-heap/>`_: gdb script written in
|
2021-09-17 14:18:24 -04:00
|
|
|
|
Python by Dave Malcolm (2010-2011) to analyze the usage of the heap memory
|
2013-10-21 20:30:12 -04:00
|
|
|
|
* `memory_profiler <https://pypi.python.org/pypi/memory_profiler>`_:
|
|
|
|
|
written by Fabian Pedregosa (2011-2013)
|
2013-10-26 03:43:14 -04:00
|
|
|
|
* `caulk <https://github.com/smartfile/caulk/>`_: written by Ben Timby in 2012
|
2013-10-21 20:30:12 -04:00
|
|
|
|
|
|
|
|
|
See also `Pympler Related Work
|
|
|
|
|
<http://pythonhosted.org/Pympler/related.html>`_.
|
|
|
|
|
|
|
|
|
|
|
2013-09-02 18:35:54 -04:00
|
|
|
|
Links
|
|
|
|
|
=====
|
|
|
|
|
|
2013-09-04 08:01:56 -04:00
|
|
|
|
tracemalloc:
|
2013-09-02 18:41:20 -04:00
|
|
|
|
|
|
|
|
|
* `#18874: Add a new tracemalloc module to trace Python
|
2013-09-02 18:35:54 -04:00
|
|
|
|
memory allocations <http://bugs.python.org/issue18874>`_
|
2013-09-04 08:01:56 -04:00
|
|
|
|
* `pytracemalloc on PyPI
|
|
|
|
|
<https://pypi.python.org/pypi/pytracemalloc>`_
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-10-27 19:20:26 -04:00
|
|
|
|
|
2013-09-02 18:35:54 -04:00
|
|
|
|
Copyright
|
|
|
|
|
=========
|
|
|
|
|
|
2013-10-08 09:54:15 -04:00
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
2013-10-08 09:54:15 -04:00
|
|
|
|
|
|
|
|
|
..
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
sentence-end-double-space: t
|
|
|
|
|
fill-column: 70
|
|
|
|
|
coding: utf-8
|
|
|
|
|
End:
|