pymemtrace
provides tools for tracking and understanding Python memory usage at different levels, at different
granularities and with different runtime costs.
Full documentation: https://pymemtrace.readthedocs.io
The tools provided by pymemtrace
:
process
is a very lightweight way of logging the total memory usage at regular time intervals. It can plot memory over time with plotting programs such asgnuplot
. See some process examplescPyMemTrace
is a memory tracer written in C that can report total memory usage for every function call/return for both C and Python sections. See some cPyMemTrace examples and a technical note on cPyMemTrace.- DTrace: Here are a number of D scripts that can trace the low level
malloc()
andfree()
system calls and report how much memory was allocated and by whom. See some DTrace examples and a technical note on DTrace. trace_malloc
is a convenience wrapper around the Python standard library tracemalloc module. This can report Python memory usage by module and line compensating for the cost oftracemalloc
. This can take memory snapshots before and after code blocks and show the change on memory caused by that code. See some trace_malloc examplesdebug_malloc_stats
is a wrapper around thesys._debugmallocstats
function that can take snapshots of memory before and after code execution and report the significant differences of the Python small object allocator. See some debug_malloc_stats examples
Each tool can be characterised by:
- Memory Granularity: In how much detail is a memory change is observed.
An example of coarse memory granularity is measuring the
Resident Set Size which is normally in chunks of 4096 bytes.
An example of fine memory granularity is recording every
malloc()
andfree()
. - Execution Granularity: In how much code detail is the memory change observed. An example of coarse execution granularity is measuring the memory usage every second. An example of fine execution granularity is recording the memory usage every Python line.
- Memory Cost: How much extra memory the tool needs.
- Execution Cost: How much the execution time is increased.
Clearly there are trade-offs between these depending on the problem you are trying to solve.
Tool | Memory Granularity | Execution Granularity | Memory Cost | Execution Cost |
---|---|---|---|---|
process |
RSS (total Python and C memory). | Regular time intervals. | Near zero. | Near zero. |
cPyMemTrace |
RSS (total Python and C memory). | Per Python line, Python function and C function call. | Near zero. | x10 to x20. |
DTrace | Every malloc() and free() . |
Per function call and return. | Minimal. | x90 to x100. |
trace_malloc |
Every Python object. | Per Python line, per function call. | Significant but compensated. | x900 for small objects, x6 for large objects. |
debug_malloc_stats |
Python memory pool. | Snapshots the CPython memory pool either side of a block of code. | Minimal. | x2000+ for small objects, x12 for large objects. |
Python memory tracing.
- Free software: MIT license
- Documentation: https://pymemtrace.readthedocs.io.
- Project: https://github.com/paulross/pymemtrace.
Phil Smith (AHL) with whom a casual lunch time chat lead to the creation of an earlier, but quite different
implementation, of cPyMemTrace
in pure Python.
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.