Skip to main content

Python memory tracing.

Project description

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

pymemtrace Tools

The tools provided by pymemtrace are:

process_tree

A command line tool that shows the total memory usage of a process and its child processes at regular time intervals. It can log this data to a JSON file for later analysis. See some process_tree examples

process

A very lightweight way of logging the total memory usage of a single process at regular time intervals. It can plot memory over time with plotting programs such as gnuplot. See some process examples

cPyMemTrace

This module, written in C, provides real time logging of Python and C actions:

  • pymemtrace.cPyMemTrace.Profile is a memory tracer written in C that can report total memory usage for every function call/return for both C and Python sections. This is more suitable for logging C code, for example Python’s C extensions.

  • pymemtrace.cPyMemTrace.Trace is a memory tracer written in C that can report total memory usage for every function call/return/line for Python sections. This is more suitable for logging pure Python code.

  • pymemtrace.cPyMemTrace.ReferenceTracing can report every object allocation and de-allocation with Reference Tracing. This is quite invasive but the API allows this to filter out most of the noise or target specific types of interest. (Python 3.13+ only).

See some cPyMemTrace examples and a technical note on cPyMemTrace.

DTrace

There are a number of D scripts that can trace the low level malloc() and free() system calls and report how much memory was allocated and by whom. See some DTrace examples and a technical note on DTrace.

trace_malloc

A convenience wrapper around the Python standard library tracemalloc module. This can report Python memory usage by module and line compensating for the cost of tracemalloc. This can take memory snapshots before and after code blocks and show the change on memory caused by that code. See some trace_malloc examples

debug_malloc_stats

Awrapper around the sys._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

Tool Characteristics

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 (RSS) which is normally in chunks of 4096 bytes. An example of fine memory granularity is recording every malloc() and free().

  • 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.

Firstly granularity:

Tool Granularity

Tool

Memory Granularity

Execution Granularity

process_tree

RSS.

Regular time intervals.

process

RSS.

Regular time intervals.

cPyMemTrace.Profile

RSS.

Per Python line, Python function and return. C function call and return.

cPyMemTrace.Trace

RSS.

Per Python line, Python function and return. Python Opcode and exception.

cPyMemTrace.ReferenceTracing

RSS.

Every object allocation/de-allocation.

DTrace

Every malloc() and free().

Per function call and return.

trace_malloc

Every Python object.

Per Python line, per function call.

debug_malloc_stats

Python memory pool.

Snapshots the CPython memory pool either side of a block of code.

Secondly cost:

Tool Cost

Tool

Memory Cost

Execution Cost

process_tree

Near zero.

Near zero.

process

Near zero.

Near zero.

cPyMemTrace.Profile

Near zero.

10x to 40x.

cPyMemTrace.Trace

Near zero.

20x to 60x.

cPyMemTrace.ReferenceTracing

Near zero.

2x to 80x.

DTrace

Minimal.

90x to 100x.

trace_malloc

Significant but compensated.

900x for small objects, 6x for large objects.

debug_malloc_stats

Minimal.

+2000x for small objects, 12x for large objects.

Installation

To install pymemtrace, run this command in your terminal:

$ pip install pymemtrace

Licence

Python memory tracing.

Credits

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.

History

0.6.1 (2026-06-24)

  • Fixes: #5 (macros setting for strdup()).

  • Some debug improvements.

  • Added magic numbers to opaque data structures.

  • Add gnuplot output of live object count.

  • Documentation improvements.

  • Supported Python versions are: 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14, 3.15

  • Development Status :: 5 - Production/Stable

0.6.0 (2026-05-19)

  • Add a hash table of live type counts to Reference Tracing. This can be accessed from Python as a dictionary.

  • Fix Reference tracing example for build_all.sh.

  • Add module level functions to write to the Profile/Trace/Reference Trace log files.

  • Add some asserts and clean up test_cpyreftraceexample.py.

  • Add Reference Tracing minimal example in C, a Python module and tests.

  • Add Doxygen documentation.

  • Reorganise debug code.

  • Add basic example of Reference Tracing in C to debug code.

  • Supported Python versions are: 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14, 3.15

  • Development Status :: 5 - Production/Stable

0.5.0 (2026-05-12)

  • Add two console entry points in the distro: pymemtrace_ref_trace_analyse and pymemtrace_dtrace_log_analyse

  • Make option names for ref_trace_analyse.py more intuitive.

  • Fix issues with initialising the datatime capsule.

  • Minor change to the Reference Tracing log file format.

  • ref_trace_analyse.py can now merge log files.

  • Add the ability to get reference counts from arbitrary addresses.

  • Added documentation.

  • Supported Python versions are: 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14, 3.15

  • Development Status :: 5 - Production/Stable

0.4.1 (2026-05-03)

  • Add example of using Reference Tracing to detect memory leaks.

0.4.0 (2026-04-18)

  • cPyMemTrace:
    • User filtering of Reference Tracing events to include/exclude specific events.

    • Reference Tracing runs the Garbage Collector on exit to make the log more accurate.

    • Much more reliable processing of Reference Tracing events.

    • Fix the issue with Reference Tracing where handling type “frame” and “code” was causing pytest and CPython runtime assert failures.

    • Write profile/trace/reference tracing context switches to the appropriate log file.

    • Add cPyMemTrace.ReferenceTracingSimple as an example. Includes documentation and test code.

    • Add suspend() and resume() methods for Reference Tracing.

    • Document pytest issues with Reference Tracing (now historical information).

  • General documentation improvements, now around 140 PDF A4 pages.

  • Add Doxygen documentation of the C source.

  • Supported Python versions are: 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14, 3.15

  • Development Status :: 5 - Production/Stable

0.3.1 (2026-03-23)

  • pymemtrace:
    • Add decorator for pymemtrace.process.

    • pymemtrace.process can now summarise JSON in the log to stdout.

  • cPyMemTrace:
    • Add decorators for Python functions for Profile, Trace and ReferenceTracing.

    • Fix a SIGSEGV when bad keyword arguments were passed to cPyMemTrace.ReferenceTracing.

0.3.0 (2026-03-19)

  • Add process-tree.py for logging a process and its children.

  • cPyMemTrace:
    • Add Reference Tracing (Python 3.13+) that can record every object allocation or de-allocation.

    • Add an option to log to a specific file.

    • Add an API write_message_to_log() to inject text into the log file.

    • Better structure of the log file format.

    • Define the log file format.

    • Add debug exploration code with debug_cPyMemtrace().

    • Fix stacking pop() issue with trace/profile functions with linked list of tTraceFileWrapperLinkedList.

  • Add support for Python 3.14

  • Remove support for Python 3.7

  • Supported Python versions are: 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14

  • Development Status :: 5 - Production/Stable

0.2.0 (2024-11-17)

  • cPyMemTrace:
    • Add P/T, stack depth and python version to log file name, example: "20241107_195847_62264_P_0_PY3.13.0b3.log"

    • Add stacking of trace/profile functions with linked list of tTraceFileWrapperLinkedList.

    • Add an option to log to a specific file.

    • Add an API write_to_log() to inject text into the log file.

    • Add an optional message to the log file in cPyMemTrace.

    • Add Python API to get log file being written to by cPyMemTrace.

    • Bug fixes in cPyMemTrace.c

    • Safety fix for file path name lengths.

    • Fix for log files where '#' was being concatenated.

0.1.7 (2024-09-12)

  • Minor fix for a single test.

0.1.6 (2024-09-11)

  • Add support for Python versions 3.12, 3.13. Now supports Python versions 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13.

0.1.5 (2023-06-21)

  • Add support for Python versions 3.10, 3.11. Now supports Python versions 3.7, 3.8, 3.9, 3.10, 3.11.

0.1.4 (2022-03-19)

  • Fix Linux build.

0.1.3 (2022-03-17)

  • Fix some tests.

0.1.2 (2022-03-17)

  • Fix source distribution that had missing headers.

0.1.1 (2020-11-17)

  • Add cPyMemTrace the C level profiler.

  • Add DTrace scripts for low level tracing.

  • Add debug_malloc_stats the wrapper around sys._debugmallocstats.

  • Add process.py from the TotalDepth project.

  • Add redirect_stdout for debug_malloc_stats.

  • Add trace_malloc, a wrapper around the tracemalloc module.

  • Includes extensive documentation and performance measurement.

  • First release on PyPI.

0.1.0 (2017-12-04)

  • Initial idea and implementation, never released.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymemtrace-0.6.1.tar.gz (8.5 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pymemtrace-0.6.1-cp315-cp315-macosx_10_15_universal2.whl (182.5 kB view details)

Uploaded CPython 3.15macOS 10.15+ universal2 (ARM64, x86-64)

pymemtrace-0.6.1-cp314-cp314-macosx_10_15_universal2.whl (182.5 kB view details)

Uploaded CPython 3.14macOS 10.15+ universal2 (ARM64, x86-64)

pymemtrace-0.6.1-cp313-cp313-macosx_10_13_universal2.whl (182.1 kB view details)

Uploaded CPython 3.13macOS 10.13+ universal2 (ARM64, x86-64)

pymemtrace-0.6.1-cp312-cp312-macosx_10_13_universal2.whl (158.0 kB view details)

Uploaded CPython 3.12macOS 10.13+ universal2 (ARM64, x86-64)

pymemtrace-0.6.1-cp311-cp311-macosx_10_9_universal2.whl (157.7 kB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

pymemtrace-0.6.1-cp310-cp310-macosx_10_9_universal2.whl (157.6 kB view details)

Uploaded CPython 3.10macOS 10.9+ universal2 (ARM64, x86-64)

pymemtrace-0.6.1-cp39-cp39-macosx_10_9_universal2.whl (157.6 kB view details)

Uploaded CPython 3.9macOS 10.9+ universal2 (ARM64, x86-64)

pymemtrace-0.6.1-cp38-cp38-macosx_11_0_universal2.whl (157.4 kB view details)

Uploaded CPython 3.8macOS 11.0+ universal2 (ARM64, x86-64)

File details

Details for the file pymemtrace-0.6.1.tar.gz.

File metadata

  • Download URL: pymemtrace-0.6.1.tar.gz
  • Upload date:
  • Size: 8.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for pymemtrace-0.6.1.tar.gz
Algorithm Hash digest
SHA256 0fe58836e05d08b5f98221e969aa0eb1e4bd1547143f18f0b7e72dd8822aee4f
MD5 4eae41e0fb7387a13a85558c73d5b7ce
BLAKE2b-256 654b2d0cced7d06f31ea77b20d7cbc8530152804093c37e2593aaa899a38c183

See more details on using hashes here.

File details

Details for the file pymemtrace-0.6.1-cp315-cp315-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for pymemtrace-0.6.1-cp315-cp315-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 cd657886b3adc4626684e17c4cd397b1463a5fb584ee0b79a0dbb1118e644a9e
MD5 31c158f9b00b5c6fe6a0aa1ddfd5614c
BLAKE2b-256 12005bcb68ba6e0ea5418a3443f959e260a751c9f27ea5b671b257376c539c56

See more details on using hashes here.

File details

Details for the file pymemtrace-0.6.1-cp314-cp314-macosx_10_15_universal2.whl.

File metadata

File hashes

Hashes for pymemtrace-0.6.1-cp314-cp314-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 068d92ad044bbefbae79592d8b89a19c9a1f57fc99ac2146eca53d32280afb8d
MD5 1047c9e375b0857afcade2e00eef6dba
BLAKE2b-256 f3c805537e6ef61f8c66f2e3fd0871ba2533f17161f23c7ade28c9e23d97ed8b

See more details on using hashes here.

File details

Details for the file pymemtrace-0.6.1-cp313-cp313-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for pymemtrace-0.6.1-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 fe900ba370070a94ab2cb85c236641ee1e6c7a620866827d054bc98b6f009bcc
MD5 2d03a050387fbf02e3870dab09abedca
BLAKE2b-256 d0082d1c9c747aaf3eee9c8bc5e5ad2e25c2e76ce3d75d5ee3fb842017ad6201

See more details on using hashes here.

File details

Details for the file pymemtrace-0.6.1-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for pymemtrace-0.6.1-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 04cee30827420cc2e95289565d85cac7df8f1c0f0cfb926bb5731b529e196426
MD5 8c984accac332ddf6127eedda6c3e480
BLAKE2b-256 ad82fa2c8b9bd3a87e8ac6fb08645467dbf4fe176697c37015fc1940aeb4bf7b

See more details on using hashes here.

File details

Details for the file pymemtrace-0.6.1-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pymemtrace-0.6.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 2fdb487e3d72709efa3e1a4d923391835cbfcc7a690044387ccd3d5046a37a49
MD5 b08ebee80814611eb1c5a0fef7138e71
BLAKE2b-256 9414225b1e7d702ee78e370cab23f92cd1bb144d1aef672dfe76254be08ffb29

See more details on using hashes here.

File details

Details for the file pymemtrace-0.6.1-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pymemtrace-0.6.1-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 0ad926aa0fd7924cfdc2d3bb03ca4c8ad8607037a3eaf779d1df54d29014ef3b
MD5 b8bfd4a987bb74291710904ec1580fdf
BLAKE2b-256 4fc87f0d1423b9b660d4f1c2ce7b703dee15def399fe5bec835d1cc0fa428050

See more details on using hashes here.

File details

Details for the file pymemtrace-0.6.1-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pymemtrace-0.6.1-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 13ab9963b703ea263545afc90fabc9de0251c77f235bb3de48b69e5256a38808
MD5 e38b3a9826ddb74eee32b4cae31fa08d
BLAKE2b-256 6a37aefa4faffed94767594576df2408209e7bbea93dbf69db18cb9179bf87a9

See more details on using hashes here.

File details

Details for the file pymemtrace-0.6.1-cp38-cp38-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for pymemtrace-0.6.1-cp38-cp38-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 5e019502ef08bd80e432d9fc5e44659ead049e09c1c6794e6c35b586e821d761
MD5 51e81d7e039888ccf2d9c8086a746182
BLAKE2b-256 2ce029d042adecbb6bb12267181a76f2a3fc5edc80770be8a4a8711ae1bf1e9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page