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.

  • With Python 3.13+ pymemtrace.cPyMemTrace.ReferenceTracing can report every object allocation and de-allocation with Reference Tracing.

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

Tool Characteristics

Tool

Memory Granularity

Execution Granularity

Memory Cost

Execution Cost

process_tree

RSS (total Python and C memory).

Regular time intervals.

Near zero.

Near zero.

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. Per object allocation/de-allocation.

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.

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.3.1 (2026-03-23)

  • cPyMemTrace:
    • Add decorator for pymemtrace.process.

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

    • 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.3.1.tar.gz (5.6 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.3.1-cp314-cp314-macosx_10_15_universal2.whl (123.0 kB view details)

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

pymemtrace-0.3.1-cp313-cp313-macosx_10_13_universal2.whl (122.6 kB view details)

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

pymemtrace-0.3.1-cp312-cp312-macosx_10_13_universal2.whl (117.5 kB view details)

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

pymemtrace-0.3.1-cp311-cp311-macosx_10_9_universal2.whl (117.4 kB view details)

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

pymemtrace-0.3.1-cp310-cp310-macosx_10_9_universal2.whl (117.4 kB view details)

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

pymemtrace-0.3.1-cp39-cp39-macosx_10_9_universal2.whl (117.4 kB view details)

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

pymemtrace-0.3.1-cp38-cp38-macosx_11_0_universal2.whl (117.3 kB view details)

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

File details

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

File metadata

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

File hashes

Hashes for pymemtrace-0.3.1.tar.gz
Algorithm Hash digest
SHA256 0e5b5292188218834c8e4db3c2f3675191fefb7f16972fd17a674aee366e8518
MD5 987aa2410f636f8eaf6fe6853d3b2347
BLAKE2b-256 51dbd78030c8b46e54e8428e8a7ee0994ee73d026193bb6eacfc75445ab1c02a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymemtrace-0.3.1-cp314-cp314-macosx_10_15_universal2.whl
Algorithm Hash digest
SHA256 9702727365880b3b3f6b3b8798cfbebef36cb296b3bddf270328f34573741f9e
MD5 75474f117d5d4e43ab70d3e7df28de43
BLAKE2b-256 aa41d2169641874b1f3f92d7875da623d176284bfb1cca53c5276c39929de6ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymemtrace-0.3.1-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 7cc89085e95f46819a20b8cbeae2265f98aed724295cafd6e518983062b11a3e
MD5 301625adaa2a9cd2686a41564917016e
BLAKE2b-256 3a2480f7280bb7a6b87c794f1b1d43153b70c8a600da4c878d1f5fc553843bcd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymemtrace-0.3.1-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 c34d65cfddebce9349c8c510818eb4cf2de56a578b8eac96107d49e85946be8d
MD5 4fa930daebbd856669c3875ed6a0c11d
BLAKE2b-256 d750e7b1896479a74998e3f1e1daf3729cdcb9e8de4d76bcbebac8f03c004f76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymemtrace-0.3.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 46812884e7703ea0a6a9f8f987601952c8040aaa12f0beeb0625d5431daeab6c
MD5 2bde1143269be5eeffba89465e0ba47b
BLAKE2b-256 486b3aa4b91ecdf09f7b3dd8f2085a82f8188705f090109bc62d58892e65ab91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymemtrace-0.3.1-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 f5f09821b5bcb20f76312874999f5288a924df9c855b472c91fcb2743705eeef
MD5 81215dd4b36b0eb888bb1046eca7bbfd
BLAKE2b-256 2550d9ae2abb8047883d0a2587b747d1c72d697d058cd59adc2aae84a0036a9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymemtrace-0.3.1-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 4de4c72774a9f923da74242f28c7d2e4617687a284744aa05b843eaa111f86b7
MD5 4795f7449740c8a1152ea4dc45e6fda7
BLAKE2b-256 b05fd156c95eef6f8093e8805df03f1ac4f0ea6fefb029ccb864c5574cb360e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pymemtrace-0.3.1-cp38-cp38-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 d18478b2fb312f621d6631aa612bf4305049e79b34561dce02f5cfe454f3179a
MD5 da4194fe1e87dff3c9f830357480abdd
BLAKE2b-256 47592a627180be600935c9839f9699e7cbcdcd8339d735d737799be6970e0161

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