Registry / testing / pytest-memray

pytest-memray

JSON →
library1.10.0pypypi✓ verified 27d ago

pytest-memray is a pytest plugin for easy integration of memray, a powerful memory profiler for Python. It allows developers to analyze memory allocations, detect memory leaks, and identify memory-intensive hotspots directly within their pytest test suite. The current version, 1.8.0, supports Python 3.8 and higher, and is actively maintained with regular releases.

pip install pytest-memray
INSTALL
IMPORT
SIG · PYTEST-MEMRAY
P
pytest-memray
testingpythonv1.10.0
Install
4.5s avg
Import
554ms
Disk
76MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.10.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.590s · 78.3MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 4.5s · import 0.518s · 74MB
76MB installed
● package 76MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LeaksFilterFunction
✓ from pytest_memray import LeaksFilterFunction
✗ import pytest
marks
✓ from pytest_memray import marks
✗ import pytest
Stack
✓ from pytest_memray import Stack
✗ import pytest

To activate `pytest-memray` during a test run, simply add the `--memray` flag to your `pytest` command. For granular control and enforcement within tests, use markers like `@pytest.mark.limit_memory` to set a peak memory limit or `@pytest.mark.limit_leaks` to detect memory leaks. If a test exceeds its limit, `pytest` will report a failure.

import pytest @pytest.mark.limit_memory('24 MB') def test_foobar(): data = [i for i in range(1_000_000)] # Allocates ~8MB assert len(data) == 1_000_000 # To run this test with memory profiling: # pytest --memray your_test_file.py
Debug
Known issues
gotchaPython's internal object allocators, caches (e.g., `re`, `logging`), and pytest's output capturing can result in reported allocations that are not true application-level memory leaks. Interpreting memory reports requires understanding these internal behaviors.
fix
Account for Python and pytest internal allocations. Run tests in a loop or with `current_thread_only=True` in markers for clearer leak detection. Consider `memray`'s own documentation on 'false positives'.
affects: All versions
gotchaThe underlying `memray` library, which `pytest-memray` integrates with, primarily supports Linux and macOS. While the Python package can be installed elsewhere, full functionality and development might be limited or require a POSIX-compliant system, especially for native-level tracking.
fix
Run tests and profiling on Linux or macOS environments for optimal results and full feature access.
affects: All versions
gotchaPrior to version 1.3.2, `pytest-memray` had compatibility issues with `pytest-xdist` when running tests in parallel, potentially leading to incorrect or failed memory tracking. Users on older versions might experience instability.
fix
Upgrade to `pytest-memray` version 1.3.2 or newer to ensure proper compatibility with `pytest-xdist`.
affects: <1.3.2
gotchaUsing very long test names or file paths can lead to `OSError` exceptions due to file system limitations when `memray` attempts to write its binary capture files. This can prevent reports from being generated.
fix
Keep test file paths and test function names reasonably short, especially if encountering `OSError` related to filename length.
affects: All versions
breakingThe `limit_memory` and `limit_leaks` markers in versions prior to 1.6.0 tracked all memory allocations across all threads. Version 1.6.0 introduced a `current_thread_only=True` keyword argument to these markers, allowing more precise tracking of allocations solely within the test's thread. Existing tests on older versions might report higher memory usage if other threads were active.
fix
Upgrade to `pytest-memray` 1.6.0 or newer and consider adding `current_thread_only=True` to your markers for more focused memory tracking, if appropriate for your tests.
affects: <1.6.0
Upgrade
Version history
1.10.0latest on PyPI · released Aug 7, 2026
Audit
Dependencies
pytestrequiredRequired as it is a pytest plugin.
memrayrequiredThe core memory profiling library that pytest-memray integrates with.
Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
2
Resources
pytest-memray — pip install pytest-memray · libregistry