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$
|
|
|
|
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-16 19:38:43 -04:00
|
|
|
Add a new ``tracemalloc`` module to trace memory blocks allocated by Python.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2013-09-03 20:02:50 -04:00
|
|
|
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
|
|
|
|
2013-09-03 20:02:50 -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.
|
2013-09-03 20:02:50 -04:00
|
|
|
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
|
2013-09-03 20:02:50 -04:00
|
|
|
``tuple``, and it is hard to identify where these objects are
|
|
|
|
instancied.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-03 20:02:50 -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-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
* Compute the differences between two snapshots to detect memory leaks
|
|
|
|
* Statistics on allocated memory blocks per filename and per line number:
|
|
|
|
total size, number and average size of allocated memory blocks
|
|
|
|
* For each allocated memory block: its size and the traceback where the block
|
|
|
|
was allocated
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-05 17:29:40 -04:00
|
|
|
The API of the tracemalloc module is similar to the API of the
|
|
|
|
faulthandler module: ``enable()``, ``disable()`` and ``is_enabled()``
|
|
|
|
functions, an environment variable (``PYTHONFAULTHANDLER`` and
|
|
|
|
``PYTHONTRACEMALLOC``), a ``-X`` command line option (``-X
|
|
|
|
faulthandler`` and ``-X tracemalloc``). See the
|
|
|
|
`documentation of the faulthandler module
|
|
|
|
<http://docs.python.org/dev/library/faulthandler.html>`_.
|
2013-09-05 17:23:35 -04:00
|
|
|
|
2013-09-05 17:26:55 -04:00
|
|
|
The tracemalloc module has been written for CPython. Other
|
|
|
|
implementations of Python may not provide it.
|
|
|
|
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
API
|
|
|
|
===
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
To trace most memory blocks allocated by Python, the module should be
|
|
|
|
enabled as early as possible by calling ``tracemalloc.enable()``
|
|
|
|
function, by setting the ``PYTHONTRACEMALLOC`` environment variable to
|
|
|
|
``1``, or by using ``-X tracemalloc`` command line option.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
By default, the ``Trace.traceback`` attribute only stores one ``Frame``
|
|
|
|
instance per allocated memory block. Use ``set_traceback_limit()`` to
|
|
|
|
store more frames.
|
2013-09-05 17:15:45 -04:00
|
|
|
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
Functions
|
|
|
|
---------
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``add_filter(filter)`` function:
|
2013-09-08 09:58:01 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Add a new filter on Python memory allocations, *filter* is a
|
|
|
|
``Filter`` instance.
|
2013-09-08 09:58:01 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
All inclusive filters are applied at once, a memory allocation is
|
|
|
|
only ignored if no inclusive filter match its trace. A memory
|
|
|
|
allocation is ignored if at least one exclusive filter matchs its
|
|
|
|
trace.
|
|
|
|
|
|
|
|
The new filter is not applied on already collected traces. Use
|
|
|
|
``clear_traces()`` to ensure that all traces match the new filter.
|
|
|
|
|
|
|
|
|
|
|
|
``add_include_filter(filename: str, lineno: int=None, traceback: bool=False)`` function:
|
|
|
|
|
|
|
|
Add an inclusive filter: helper for ``add_filter()`` creating a
|
|
|
|
``Filter`` instance with ``include`` attribute set to ``True``.
|
|
|
|
|
|
|
|
Example: ``tracemalloc.add_include_filter(tracemalloc.__file__)``
|
|
|
|
only includes memory blocks allocated by the ``tracemalloc`` module.
|
|
|
|
|
|
|
|
|
|
|
|
``add_exclude_filter(filename: str, lineno: int=None, traceback: bool=False)`` function:
|
|
|
|
|
|
|
|
Add an exclusive filter: helper for ``add_filter()`` creating a
|
|
|
|
``Filter`` instance with ``include`` attribute set to ``False``.
|
|
|
|
|
|
|
|
Example: ``tracemalloc.add_exclude_filter(tracemalloc.__file__)``
|
|
|
|
ignores memory blocks allocated by the ``tracemalloc`` module.
|
2013-09-08 09:58:01 -04:00
|
|
|
|
|
|
|
|
2013-09-08 09:15:26 -04:00
|
|
|
``clear_filters()`` function:
|
|
|
|
|
|
|
|
Reset the filter list.
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
|
2013-09-05 17:15:45 -04:00
|
|
|
``clear_traces()`` function:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Clear all traces and statistics on Python memory allocations, and
|
|
|
|
reset the ``get_traced_memory()`` counter.
|
|
|
|
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-03 07:18:48 -04:00
|
|
|
``disable()`` function:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
Stop tracing Python memory allocations and stop the timer started by
|
|
|
|
``start_timer()``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
See also ``enable()`` and ``is_enabled()`` functions.
|
|
|
|
|
|
|
|
|
2013-09-05 17:15:45 -04:00
|
|
|
``enable()`` function:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-05 17:15:45 -04:00
|
|
|
Start tracing Python memory allocations.
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
See also ``disable()`` and ``is_enabled()`` functions.
|
|
|
|
|
|
|
|
|
2013-09-08 09:15:26 -04:00
|
|
|
``get_filters()`` function:
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Get the filters on Python memory allocations as list of ``Filter``
|
|
|
|
instances.
|
2013-09-08 09:15:26 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``get_traceback_limit()`` function:
|
2013-09-08 09:58:01 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Get the maximum number of ``Frame`` instances stored in the
|
|
|
|
``traceback`` attribute of a ``Trace`` instance.
|
|
|
|
|
|
|
|
Use ``set_traceback_limit()`` to change the limit.
|
2013-09-05 17:15:45 -04:00
|
|
|
|
2013-09-02 18:35:54 -04:00
|
|
|
|
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-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
|
2013-09-03 19:19:30 -04:00
|
|
|
``get_object_trace(obj)`` function:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Get the trace of a Python object *obj* as a ``Trace`` instance.
|
|
|
|
|
|
|
|
The function only returns the trace of the memory block directly
|
|
|
|
holding to object. The ``size`` attribute of the trace is smaller
|
|
|
|
than the total size of the object if the object is composed of more
|
|
|
|
than one memory block.
|
|
|
|
|
|
|
|
Return ``None`` if the ``tracemalloc`` module did not trace the
|
|
|
|
allocation of the object.
|
|
|
|
|
|
|
|
See also ``gc.get_referrers()`` and ``sys.getsizeof()`` functions.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
2013-09-03 07:18:48 -04:00
|
|
|
``get_process_memory()`` function:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
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:35:54 -04:00
|
|
|
|
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:35:54 -04:00
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
Return ``None`` if the platform is not supported.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
2013-09-03 19:19:30 -04:00
|
|
|
``get_stats()`` function:
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Get statistics on traced Python memory blocks as a dictionary
|
|
|
|
``{filename (str): {line_number (int): stats}}`` where *stats* in a
|
|
|
|
``TraceStats`` instance, *filename* and *line_number* can be
|
|
|
|
``None``.
|
|
|
|
|
|
|
|
Return an empty dictionary if the ``tracemalloc`` module is
|
|
|
|
disabled.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``get_traced_memory()`` function:
|
|
|
|
|
|
|
|
Get the total size of all traced memory blocks allocated by Python.
|
|
|
|
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-08 09:58:01 -04:00
|
|
|
``get_tracemalloc_size()`` function:
|
|
|
|
|
|
|
|
Get the memory usage in bytes of the ``tracemalloc`` module.
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
|
2013-09-03 19:19:30 -04:00
|
|
|
``get_traces(obj)`` function:
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Get all traces of Python memory allocations as a dictionary
|
|
|
|
``{address (int): trace}`` where *trace* is a ``Trace`` instance.
|
2013-09-05 17:15:45 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Return an empty dictionary if the ``tracemalloc`` module is
|
|
|
|
disabled.
|
2013-09-05 17:15:45 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``is_enabled()`` function:
|
2013-09-05 17:15:45 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``True`` if the ``tracemalloc`` module is tracing Python memory
|
|
|
|
allocations, ``False`` otherwise.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
See also ``enable()`` and ``disable()`` functions.
|
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:35:54 -04:00
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
Start a timer calling ``func(*args, **kwargs)`` every *delay*
|
2013-09-16 19:38:43 -04:00
|
|
|
seconds. Enable the ``tracemalloc`` module if it is disabled. 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. The timer has a resolution of 1 second.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If the ``start_timer()`` function is called twice, previous
|
|
|
|
parameters are replaced. Call the ``stop_timer()`` function to stop
|
|
|
|
the timer.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
The ``DisplayTopTask.start()`` and ``TakeSnapshot.start()`` methods
|
|
|
|
use the ``start_timer()`` function to run regulary a task.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``set_traceback_limit(limit: int)`` function:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Set the maximum number of ``Frame`` instances stored in the
|
|
|
|
``traceback`` attribute of a ``Trace`` instance. Clear all traces
|
|
|
|
and statistics on Python memory allocations if the ``tracemalloc``
|
|
|
|
module is enabled,
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Storing the traceback of each memory allocation has an important
|
|
|
|
overhead on the memory usage. Example with the Python test suite:
|
|
|
|
tracing all memory allocations increases the memory usage by
|
|
|
|
``+50%`` when storing only 1 frame and ``+150%`` when storing 10
|
|
|
|
frames. Use ``get_tracemalloc_size()`` to measure the overhead and
|
|
|
|
``add_filter()`` to select which memory allocations are traced.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Use ``get_traceback_limit()`` to get the current limit.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``stop_timer()`` function:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Stop the timer started by ``start_timer()``.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
DisplayTop class
|
|
|
|
----------------
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``DisplayTop()`` class:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Display the top of allocated memory blocks.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``display_snapshot(snapshot, count=10, group_by="filename_lineno", cumulative=False, file=None)`` method:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Display a snapshot of memory blocks allocated by Python, *snapshot*
|
|
|
|
is a ``Snapshot`` instance.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``display_top_diff(top_diff, count=10, file=None)`` method:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Display differences between two ``GroupedStats`` instances,
|
|
|
|
*top_diff* is a ``StatsDiff`` instance.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``display_top_stats(top_stats, count=10, file=None)`` method:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Display the top of allocated memory blocks grouped by the
|
|
|
|
``group_by`` attribute of *top_stats*, *top_stats* is a
|
|
|
|
``GroupedStats`` instance.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``color`` attribute:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If ``True``, always use colors. If ``False``, never use colors. The
|
|
|
|
default value is ``None``: use colors if the *file* parameter is a
|
|
|
|
TTY device.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``compare_with_previous`` attribute:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If ``True`` (default value), compare with the previous snapshot. If
|
|
|
|
``False``, compare with the first snapshot.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``filename_parts`` attribute:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Number of displayed filename parts (int, default: ``3``). Extra
|
|
|
|
parts are replaced with ``'...'``.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``show_average`` attribute:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If ``True`` (default value), display the average size of memory blocks.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``show_count`` attribute:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If ``True`` (default value), display the number of allocated memory
|
|
|
|
blocks.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``show_size`` attribute:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If ``True`` (default value), display the size of memory blocks.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
DisplayTopTask class
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
``DisplayTopTask(count=10, group_by="filename_lineno", cumulative=False, file=sys.stdout, user_data_callback=None)`` class:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Task taking temporary snapshots and displaying the top *count*
|
|
|
|
memory allocations grouped by *group_by*.
|
|
|
|
|
|
|
|
Call the ``start()`` method to start the task.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
``display()`` method:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Take a snapshot and display the top *count* biggest allocated memory
|
|
|
|
blocks grouped by *group_by* using the ``display_top`` attribute.
|
|
|
|
|
|
|
|
Return the snapshot, a ``Snapshot`` instance.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
``start(delay: int)`` method:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Start a task using the ``start_timer()`` function calling the
|
|
|
|
``display()`` method every *delay* seconds.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
``stop()`` method:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Stop the task started by the ``start()`` method using the
|
|
|
|
``stop_timer()`` function.
|
|
|
|
|
|
|
|
``count`` attribute:
|
|
|
|
|
|
|
|
Maximum number of displayed memory blocks.
|
|
|
|
|
|
|
|
``cumulative`` attribute:
|
|
|
|
|
|
|
|
If ``True``, cumulate size and count of memory blocks of all frames
|
|
|
|
of each ``Trace`` instance, not only the most recent frame. The
|
|
|
|
default value is ``False``.
|
|
|
|
|
|
|
|
The option is ignored if the traceback limit is ``1``, see the
|
|
|
|
``get_traceback_limit()`` function.
|
|
|
|
|
|
|
|
``display_top`` attribute:
|
|
|
|
|
|
|
|
Instance of ``DisplayTop``.
|
|
|
|
|
|
|
|
``file`` attribute:
|
|
|
|
|
|
|
|
The top is written into *file*.
|
|
|
|
|
|
|
|
``group_by`` attribute:
|
|
|
|
|
|
|
|
Determine how memory allocations are grouped: see
|
|
|
|
``Snapshot.top_by`` for the available values.
|
|
|
|
|
|
|
|
``user_data_callback`` attribute:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Optional callback collecting user data (callable, default:
|
|
|
|
``None``). See ``Snapshot.create()``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Filter class
|
|
|
|
------------
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``Filter(include: bool, pattern: str, lineno: int=None, traceback: bool=False)`` class:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Filter to select which memory allocations are traced. Filters can be
|
|
|
|
used to reduce the memory usage of the ``tracemalloc`` module, which
|
|
|
|
can be read using ``get_tracemalloc_size()``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``match_trace(trace)`` method:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Return ``True`` if the ``Trace`` instance must be kept according to
|
|
|
|
the filter, ``False`` otherwise.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``match(filename: str, lineno: int)`` method:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Return ``True`` if the filename and line number must be kept
|
|
|
|
according to the filter, ``False`` otherwise.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``match_filename(filename: str)`` method:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Return ``True`` if the filename must be kept according to the
|
|
|
|
filter, ``False`` otherwise.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``match_lineno(lineno: int)`` method:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Return ``True`` if the line number must be kept according to the
|
|
|
|
filter, ``False`` otherwise.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``include`` attribute:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If *include* is ``True``, only trace memory blocks allocated in a
|
|
|
|
file with a name matching filename ``pattern`` at line number
|
|
|
|
``lineno``. If *include* is ``False``, ignore memory blocks
|
|
|
|
allocated in a file with a name matching filename :attr`pattern` at
|
|
|
|
line number ``lineno``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``pattern`` attribute:
|
|
|
|
|
|
|
|
The filename *pattern* can contain one or many ``*`` joker
|
|
|
|
characters which match any substring, including an empty string. The
|
|
|
|
``.pyc`` and ``.pyo`` suffixes are replaced with ``.py``. On
|
|
|
|
Windows, the comparison is case insensitive and the alternative
|
|
|
|
separator ``/`` is replaced with the standard separator ``\``.
|
|
|
|
|
|
|
|
``lineno`` attribute:
|
|
|
|
|
|
|
|
Line number (``int``). If is is ``None`` or lesser than ``1``, it
|
|
|
|
matches any line number.
|
|
|
|
|
|
|
|
``traceback`` attribute:
|
|
|
|
|
|
|
|
If *traceback* is ``True``, all frames of the ``traceback``
|
|
|
|
attribute of ``Trace`` instances are checked. If *traceback* is
|
|
|
|
``False``, only the most recent frame is checked.
|
|
|
|
|
|
|
|
This attribute only has an effect on the ``match_trace()`` method
|
|
|
|
and only if the traceback limit is greater than ``1``. See the
|
|
|
|
``get_traceback_limit()`` function.
|
|
|
|
|
|
|
|
|
|
|
|
Frame class
|
|
|
|
-----------
|
|
|
|
|
|
|
|
``Frame`` class:
|
|
|
|
|
|
|
|
Trace of a Python frame, used by ``Trace.traceback`` attribute.
|
|
|
|
|
|
|
|
``filename`` attribute:
|
|
|
|
|
|
|
|
Python filename, ``None`` if unknown.
|
|
|
|
|
|
|
|
``lineno`` attribute:
|
|
|
|
|
|
|
|
Python line number, ``None`` if unknown.
|
|
|
|
|
|
|
|
|
|
|
|
GroupedStats class
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``GroupedStats(stats: dict, group_by: str, cumulative=False, timestamp=None, process_memory=None, tracemalloc_size=None)`` class:
|
|
|
|
|
|
|
|
Top of allocated memory blocks grouped by on *group_by* as a
|
|
|
|
dictionary.
|
|
|
|
|
|
|
|
The ``Snapshot.top_by()`` method creates a ``GroupedStats`` instance.
|
|
|
|
|
|
|
|
``compare_to(old_stats: GroupedStats=None)`` method:
|
|
|
|
|
|
|
|
Compare to an older ``GroupedStats`` instance. Return a
|
|
|
|
``StatsDiff`` instance.
|
|
|
|
|
|
|
|
``cumulative`` attribute:
|
|
|
|
|
|
|
|
If ``True``, cumulate size and count of memory blocks of all frames
|
|
|
|
of ``Trace``, not only the most recent frame.
|
|
|
|
|
|
|
|
``group_by`` attribute:
|
|
|
|
|
|
|
|
Determine how memory allocations were grouped. The type of ``stats``
|
|
|
|
keys depends on *group_by*:
|
|
|
|
|
|
|
|
===================== ======================== ==============
|
|
|
|
group_by description key type
|
|
|
|
===================== ======================== ==============
|
|
|
|
``'filename'`` filename ``str``
|
|
|
|
``'filename_lineno'`` filename and line number ``(str, str)``
|
|
|
|
``'address'`` memory block address ``int``
|
|
|
|
===================== ======================== ==============
|
|
|
|
|
|
|
|
See the *group_by* parameter of the ``Snapshot.top_by()`` method.
|
|
|
|
|
|
|
|
``stats`` attribute:
|
|
|
|
|
|
|
|
Dictionary ``{key: stats}`` where the *key* type depends on the
|
|
|
|
``group_by`` attribute and *stats* type is ``TraceStats``.
|
|
|
|
|
|
|
|
``process_memory`` attribute:
|
|
|
|
|
|
|
|
Result of the ``get_process_memory()`` function, can be ``None``.
|
|
|
|
|
|
|
|
``timestamp`` attribute:
|
|
|
|
|
|
|
|
Creation date and time of the snapshot, ``datetime.datetime``
|
|
|
|
instance.
|
|
|
|
|
|
|
|
``tracemalloc_size`` attribute:
|
|
|
|
|
|
|
|
The memory usage in bytes of the ``tracemalloc`` module, result of
|
|
|
|
the ``get_tracemalloc_size()`` function.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
2013-09-02 18:41:20 -04:00
|
|
|
Snapshot class
|
|
|
|
--------------
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``Snapshot`` class:
|
2013-09-02 18:53:13 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Snapshot of memory blocks allocated by Python.
|
2013-09-03 07:18:48 -04:00
|
|
|
|
|
|
|
Use ``TakeSnapshot`` to take regulary snapshots.
|
2013-09-02 18:53:13 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``apply_filters(filters)`` method:
|
|
|
|
|
|
|
|
Apply a list filters on the ``traces`` and ``stats`` dictionaries,
|
|
|
|
*filters* is a list of ``Filter`` instances.
|
|
|
|
|
|
|
|
``create(\*, with_traces=False, with_stats=True, user_data_callback=None)`` classmethod:
|
|
|
|
|
|
|
|
Take a snapshot of traces and/or statistics of allocated memory
|
|
|
|
blocks.
|
2013-09-02 18:53:13 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If *with_traces* is ``True``, ``get_traces()`` is called and its
|
|
|
|
result is stored in the ``traces`` attribute. This attribute
|
|
|
|
contains more information than ``stats`` and uses more memory and
|
|
|
|
more disk space. If *with_traces* is ``False``, ``traces`` is set to
|
|
|
|
``None``.
|
2013-09-02 18:53:13 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If *with_stats* is ``True``, ``get_stats()`` is called and its
|
|
|
|
result is stored in the ``Snapshot.stats`` attribute. If
|
|
|
|
*with_stats* is ``False``, ``Snapshot.stats`` is set to ``None``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
*with_traces* and *with_stats* cannot be ``False`` at the same time.
|
2013-09-03 07:18:48 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
*user_data_callback* is an optional callable object. Its result
|
|
|
|
should be serializable by the ``pickle`` module, or
|
|
|
|
``Snapshot.write()`` would fail. If *user_data_callback* is set, it
|
|
|
|
is called and the result is stored in the ``Snapshot.user_data``
|
|
|
|
attribute. Otherwise, ``Snapshot.user_data`` is set to ``None``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
The ``tracemalloc`` module must be enabled to take a snapshot. See
|
|
|
|
the ``enable()`` function.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
``load(filename)`` classmethod:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-02 18:53:13 -04:00
|
|
|
Load a snapshot from a file.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``top_by(group_by: str, cumulative: bool=False)`` method:
|
2013-09-02 18:41:20 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Compute top statistics grouped by *group_by* as a ``GroupedStats``
|
|
|
|
instance:
|
2013-09-02 18:41:20 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
===================== ======================== ==============
|
|
|
|
group_by description key type
|
|
|
|
===================== ======================== ==============
|
|
|
|
``'filename'`` filename ``str``
|
|
|
|
``'filename_lineno'`` filename and line number ``(str, str)``
|
|
|
|
``'address'`` memory block address ``int``
|
|
|
|
===================== ======================== ==============
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
If *cumulative* is ``True``, cumulate size and count of memory
|
|
|
|
blocks of all frames of each ``Trace`` instance, not only the most
|
|
|
|
recent frame. The *cumulative* parameter is ignored if *group_by* is
|
|
|
|
``'address'`` or if the traceback limit is ``1``. See the
|
|
|
|
``traceback_limit`` attribute.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``write(filename)`` method:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Write the snapshot into a file.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``pid`` attribute:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Identifier of the process which created the snapshot, result of
|
|
|
|
``os.getpid()``.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``process_memory`` attribute:
|
2013-09-08 09:58:01 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Memory usage of the current process, result of the
|
|
|
|
``get_process_memory()`` function. It can be ``None``.
|
2013-09-08 09:58:01 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``stats`` attribute:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Statistics on traced Python memory, result of the ``get_stats()``
|
|
|
|
function, if ``create()`` was called with *with_stats* equals to
|
|
|
|
``True``, ``None`` otherwise.
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``tracemalloc_size`` attribute:
|
2013-09-03 19:19:30 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
The memory usage in bytes of the ``tracemalloc`` module, result of
|
|
|
|
the ``get_tracemalloc_size()`` function.
|
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-09-16 19:38:43 -04:00
|
|
|
The maximum number of frames stored in the ``traceback`` attribute
|
|
|
|
of a ``Trace``, result of the ``get_traceback_limit()`` function.
|
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-09-16 19:38:43 -04:00
|
|
|
Traces of Python memory allocations, result of the ``get_traces()``
|
|
|
|
function, if ``create()`` was called with *with_traces* equals to
|
|
|
|
``True``, ``None`` otherwise.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
The ``traceback`` attribute of each ``Trace`` instance is limited to
|
|
|
|
``traceback_limit`` frames.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``timestamp`` attribute:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Creation date and time of the snapshot, ``datetime.datetime``
|
|
|
|
instance.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``user_data`` attribute:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Result of *user_data_callback* called in ``Snapshot.create()``
|
|
|
|
(default: ``None``).
|
2013-09-02 18:35:54 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
StatsDiff class
|
|
|
|
---------------
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``StatsDiff(differences, old_stats, new_stats)`` class:
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Differences between two ``GroupedStats`` instances. By default, the
|
|
|
|
``differences`` list is unsorted: call ``sort()`` to sort it.
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
The ``GroupedStats.compare_to()`` method creates a ``StatsDiff``
|
|
|
|
instance.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``sort()`` method:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Sort the ``differences`` list from the biggest allocation to the
|
|
|
|
smallest. Sort by *size_diff*, *size*, *count_diff*, *count* and
|
|
|
|
then by *key*.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``differences`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Differences between ``old_stats`` and ``new_stats`` as a list of
|
|
|
|
``(size_diff, size, count_diff, count, key)`` tuples. *size_diff*,
|
|
|
|
*size*, *count_diff* and *count* are ``int``. The key type depends
|
|
|
|
on the ``group_by`` attribute of ``new_stats``:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
===================== ======================== ==============
|
|
|
|
group_by description key type
|
|
|
|
===================== ======================== ==============
|
|
|
|
``'filename'`` filename ``str``
|
|
|
|
``'filename_lineno'`` filename and line number ``(str, str)``
|
|
|
|
``'address'`` memory block address ``int``
|
|
|
|
===================== ======================== ==============
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
See the ``group_by`` attribute of the ``GroupedStats`` class.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``old_stats`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Old ``GroupedStats`` instance, can be ``None``.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``new_stats`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
New ``GroupedStats`` instance.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-03 07:18:48 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Trace class
|
|
|
|
-----------
|
2013-09-02 18:35:54 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``Trace`` class:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Debug information of a memory block allocated by Python.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``size`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Size in bytes of the memory block.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``traceback`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Traceback where the memory block was allocated as a list of
|
|
|
|
``Frame`` instances, most recent first.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
The list can be empty or incomplete if the ``tracemalloc`` module
|
|
|
|
was unable to retrieve the full traceback.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
The traceback is limited to ``get_traceback_limit()`` frames. Use
|
|
|
|
``set_traceback_limit()`` to store more frames.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
TraceStats class
|
|
|
|
----------------
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``TraceStats`` class:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Statistics on Python memory allocations.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``size`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Total size in bytes of allocated memory blocks.
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
``count`` attribute:
|
2013-09-04 07:19:17 -04:00
|
|
|
|
2013-09-16 19:38:43 -04:00
|
|
|
Number of allocated memory blocks.
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
|