memray: Profile Python Memory Allocations

memray: Profile Python Memory Allocations

Summary

Memray traces memory allocations in Python programs, including native C and C++ code, to help explain high memory use and find leaks. Use it to inspect allocation call stacks through command-line reports or its Python API.

At a glance

Language
Python
License
Apache-2.0
Stars
15.3k
Forks
470
Added to OSRepos
February 9, 2026
Last analyzed
October 3, 2026
View on GitHub

Topics

Click on any tag to explore related repositories

Use at your own risk

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.

Overview

Memray is a Python memory profiler for developers investigating where an application allocates memory, why its peak usage is high, or what may be causing a leak. Unlike sampling profilers, it traces function calls to show allocation call stacks, and can include allocations made in native extensions and the Python interpreter.

It can run a script or module from the command line, monitor allocations interactively, and turn captured data into reports such as flame graphs and tables. A Python API is available for profiling a chosen section of a running program.

Key Features

  • Trace Python allocation call stacks rather than sampling them.
  • Optionally include native C and C++ calls in profiles.
  • Generate HTML flame graphs and tables, plus terminal tree, summary, and statistics reports.
  • Inspect allocations interactively in a terminal while a program runs.
  • Profile Python threads and native threads.
  • Use a Tracker context manager to capture memory from selected code.
  • Integrate allocation checks into tests using the separate pytest-memray plugin.

Use Cases

  • Python application developers can trace a memory spike to the functions and call paths responsible.
  • Teams investigating suspected leaks can capture a workload and examine repeated or unexpected allocations.
  • Developers using extensions such as NumPy or pandas can enable native tracking to inspect allocations beyond Python code.
  • Test maintainers can use pytest-memray to report allocation behavior or set memory limits for selected tests.
  • Engineers working on long-running scripts can use live mode to inspect allocation patterns before a run finishes.

Project Facts

  • Language: Python
  • License: Apache-2.0
  • Stars: 15.3k
  • Forks: 470
  • Topics: hacktoberfest, memory, memory-leak, memory-leak-detection, memory-profiler, profiler, python, python3
  • Archived: No

Getting Started

Install from PyPI and profile a script:

python3 -m pip install memray
python3 -m memray run -o output.bin my_script.py
python3 -m memray flamegraph output.bin

See the README and documentation for module execution, native tracking, other reports, and API usage.

Considerations

  • Memray supports Linux and macOS only; it cannot be installed on other platforms.
  • It requires Python 3.9 or later.
  • Memray includes a C extension. If a suitable binary wheel is unavailable, building from source requires system dependencies, including liblz4 and platform-specific libraries.
  • Native tracking provides broader visibility into C and C++ allocations, but the README notes it is slower than tracking without native code.

Source repository

Open the original repository on GitHub.

25 counted GitHub visits

View on GitHub

Related repositories

Similar repositories that may be relevant next.

OS
OSRepos

Analysis and discovery of open source repositories. Find interesting projects and follow their updates.

Monitor your website with YourWebsiteScore

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of third-party repository code is at your own risk. Always review source code, dependencies, licenses, and security implications before running anything.

© 2025 OSRepos. Built with Nuxt 3 and lots of ❤️