memray
Memory profiler for Python by Bloomberg — tracks memory allocations and identifies memory leaks with flamegraph visualization. memray features: command-line memray run script.py, memray flamegraph output.bin, @memray.profile() decorator, live mode (memray run --live), memray stats for summary statistics, native mode for C extension tracking (--native), tracking of Python + native (C/C++) allocations, aggregated allocations view, temporal allocation timeline, thread-specific tracking, pytest plugin (pytest-memray), and Docker-friendly design. Finds memory leaks, unexpected allocations, and OOM root causes in Python services.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
memray output files contain allocation traces that may include memory content snapshots — treat output files as sensitive if profiling agents that handle secrets. --native mode requires elevated privileges (ptrace). No network calls during profiling.
⚡ Reliability
Best When
Diagnosing Python memory leaks, OOM errors, or unexpected memory growth in agents, ML pipelines, and data processing scripts — memray's flamegraph visualization makes it immediately obvious which allocation path causes the problem.
Avoid When
You need CPU profiling (use py-spy), are on Windows, or need zero-overhead always-on production monitoring.
Use Cases
- • Agent memory leak detection — memray run --output=output.bin agent_script.py; memray flamegraph output.bin — generates interactive HTML flamegraph; agent developer identifies which function accumulates memory across 1000 agent iterations; Bloomberg-developed, production-tested on large Python services
- • Agent pytest memory regression — @pytest.mark.limit_memory('100 MB'); def test_agent_processing(): result = run_agent_on_large_dataset() — pytest-memray plugin enforces memory limit in test; agent CI pipeline fails if processing 10K records exceeds 100MB memory budget; catches memory regressions before production
- • Agent live memory monitoring — memray run --live agent_service.py — opens terminal dashboard showing real-time memory allocation by function; agent developer watches which function grows during load test; identifies leak source without stopping process
- • Agent native extension profiling — memray run --native ml_agent.py — tracks allocations in C extensions (numpy, torch) not just Python objects; agent ML pipeline OOM during inference tracked to PyTorch tensor buffer not Python objects; native mode essential for ML workloads
- • Agent programmatic profiling — with memray.Tracker('output.bin'): heavy_agent_function() — profile specific code section without full script; agent inline profiling wraps specific suspicious functions for targeted analysis without profiling entire agent startup
Not For
- • CPU profiling — memray is memory-only; for CPU profiling use py-spy or cProfile/viztracer
- • Windows — memray is Linux/macOS only; for Windows memory profiling use memory-profiler or tracemalloc
- • Production always-on profiling — memray overhead (10-30%) is too high for always-on production; use for targeted profiling sessions
Interface
Authentication
No auth — local profiling tool.
Pricing
memray is Apache 2.0 licensed by Bloomberg. Free for all use.
Agent Metadata
Known Gotchas
- ⚠ Output file grows large for long-running agents — memray run long_running_agent.py creates binary output tracking every allocation; 1-hour agent run can create 10GB+ output file; use memray run --aggregate for aggregated stats (no temporal data, smaller file) or memray run --trace-python-allocators only
- ⚠ Native mode requires ptrace permissions — memray run --native script.py on Linux may fail with 'Permission denied' if ptrace_scope > 1; agent Docker containers need --cap-add=SYS_PTRACE or run as root; native tracking essential for diagnosing OOM in PyTorch/NumPy C extensions
- ⚠ Live mode terminal requires TTY — memray run --live script.py requires interactive terminal; agent CI/CD systems without TTY raise error; use memray run without --live in CI and analyze output file separately with memray flamegraph
- ⚠ pytest-memray limit_memory is total not peak — @pytest.mark.limit_memory('50 MB') checks total bytes allocated not peak memory in use; agent tests with heavy allocate/deallocate cycles may fail limit even if peak RSS is low; use memray stats to distinguish peak from total allocations
- ⚠ Context manager requires explicit file path — with memray.Tracker('out.bin'): ... writes to file; without explicit path raises TypeError; agent inline profiling must specify output path: with memray.Tracker(f'/tmp/agent_profile_{time.time()}.bin'): run_agent()
- ⚠ flamegraph is localhost HTML not CLI — memray flamegraph output.bin opens browser or generates HTML file; agent CI pipeline must use memray flamegraph output.bin --output report.html for file output; default behavior opens browser which fails in headless CI
Alternatives
Full Evaluation Report
Comprehensive deep-dive: security analysis, reliability audit, agent experience review, cost modeling, competitive positioning, and improvement roadmap for memray.
AI-powered analysis · PDF + markdown · Delivered within 30 minutes
Package Brief
Quick verdict, integration guide, cost projections, gotchas with workarounds, and alternatives comparison.
Delivered within 10 minutes
Score Monitoring
Get alerted when this package's AF, security, or reliability scores change significantly. Stay ahead of regressions.
Continuous monitoring
Scores are editorial opinions as of 2026-03-06.