Zum Inhalt springen

Memray-Befehle

Memray ist ein Speicher-Profiler für Python, entwickelt von Bloomberg. Es verfolgt jede Speicherallokation in Python-Code und C/C++-Erweiterungen und bietet detaillierte Einblicke in die Speichernutzung durch Flammendiagramme, Tabellen und Echtzeit-Tracking.

Installation

Linux/Ubuntu

# Install via pip (Linux only — Memray does not support macOS or Windows)
pip install memray

# Install with all optional dependencies
pip install memray[all]

# Install development version
pip install git+https://github.com/bloomberg/memray.git

# Verify installation
memray --version

memray run — Recording Allocations

# Profile a Python script
memray run my_script.py

# Profile with arguments
memray run my_script.py --arg1 --arg2 value

# Profile a module
memray run -m pytest tests/

# Custom output file
memray run -o my_profile.bin my_script.py

# Track native (C/C++) allocations
memray run --native my_script.py

# Follow child processes (fork/exec)
memray run --follow-fork my_script.py

# Trace Python allocators only (faster, less detail)
memray run --trace-python-allocators my_script.py

# Force overwrite existing output file
memray run --force -o profile.bin my_script.py

# Profile with a maximum memory threshold (aggregate mode)
memray run --aggregate my_script.py

memray flamegraph — Memory Flame Graphs

# Generate an HTML flame graph from a recording
memray flamegraph my_profile.bin

# Custom output file
memray flamegraph -o memory_flamegraph.html my_profile.bin

# Show temporary allocations (freed during profiling)
memray flamegraph --temporary-allocations my_profile.bin

# Show only allocations larger than a threshold
memray flamegraph --temporary-allocation-threshold 1024 my_profile.bin

# Filter to leaks only (allocations never freed)
memray flamegraph --leaks my_profile.bin

# Merge threads into a single view
memray flamegraph --merge-threads my_profile.bin

memray table — Tabular Report

# Generate an HTML table of allocations
memray table my_profile.bin

# Custom output file
memray table -o allocation_table.html my_profile.bin

# Show temporary allocations
memray table --temporary-allocations my_profile.bin

# Show only leaked allocations
memray table --leaks my_profile.bin

memray tree — Call Tree

# Generate a call tree report
memray tree my_profile.bin

# Show largest allocations first
memray tree --biggest-allocs 20 my_profile.bin

# Show temporary allocations
memray tree --temporary-allocations my_profile.bin

memray summary — Allocation Summary

# Show a high-level summary of allocations
memray summary my_profile.bin

memray stats — Detailed Statistics

# Show detailed allocation statistics
memray stats my_profile.bin

# Output includes:
# - Total allocations and deallocations
# - Peak memory usage
# - Allocator breakdown (malloc, calloc, realloc, etc.)
# - Top allocation locations
# - Memory timeline

Live Tracking Mode

# Start a live tracking session (real-time TUI)
memray run --live my_script.py

# Live tracking with remote connection
# Terminal 1: Start the profiled process
memray run --live-remote --live-port 12345 my_script.py

# Terminal 2: Connect to the live session
memray live 12345

# Live tracking of a module
memray run --live -m flask run

Native Allocation Tracking

# Track allocations from C/C++ extensions (numpy, pandas, etc.)
memray run --native my_script.py

# Generate flame graph showing native frames
memray flamegraph my_profile.bin
# Native frames appear in the flame graph alongside Python frames

# Native tracking is essential for:
# - NumPy/SciPy memory usage
# - Pandas DataFrame allocations
# - C extension memory leaks
# - Cython module allocations

Leak Detection

# Record and find leaks
memray run -o leak_check.bin my_script.py

# Generate leak-only flame graph
memray flamegraph --leaks leak_check.bin -o leaks.html

# Generate leak-only table
memray table --leaks leak_check.bin -o leaks_table.html

# Generate leak-only tree view
memray tree --leaks leak_check.bin

pytest Plugin

# Install memray pytest plugin (included with memray)
pip install memray

# Run tests with memory limit enforcement
pytest --memray tests/

# Set a per-test memory limit (fail if exceeded)
pytest --memray --memray-bin-path=./profiles tests/

# Use the marker in tests
# @pytest.mark.limit_memory("100 MB")
# def test_data_processing():
#     process_large_dataset()

# @pytest.mark.limit_leaks("1 MB")
# def test_no_leaks():
#     do_something()
# pytest markers for memory limits
import pytest

# Fail test if it uses more than 100 MB
@pytest.mark.limit_memory("100 MB")
def test_memory_usage():
    data = load_large_dataset()
    result = process(data)
    assert result is not None

# Fail test if it leaks more than 512 KB
@pytest.mark.limit_leaks("512 KB")
def test_no_memory_leaks():
    conn = create_connection()
    conn.execute("SELECT 1")
    conn.close()

Analyzing Output

Reading Flame Graphs

# Memory flame graph interpretation:
# - X-axis width = proportion of total memory allocated
# - Y-axis depth = call stack (bottom = entry, top = allocation site)
# - Wider boxes = more memory allocated through that code path
# - Click to zoom, search to highlight specific functions
# - Colors distinguish Python frames from native frames

Häufige Muster

# Find what allocates the most memory
memray flamegraph my_profile.bin
# Look for the widest boxes at the top of the flame graph

# Find temporary allocations (allocation churn)
memray flamegraph --temporary-allocations my_profile.bin
# Temporary allocations cause GC pressure

# Find memory leaks
memray flamegraph --leaks my_profile.bin
# Leaked allocations are never freed during the profiling run

# Compare peak memory vs. final memory
memray stats my_profile.bin
# Large difference between peak and final suggests temporary spikes

Advanced Usage

# Profile with aggregate mode (lower overhead, less detail)
memray run --aggregate my_script.py

# Combine with other profilers
# 1. Use memray for memory, py-spy for CPU
memray run -o mem.bin my_script.py &
py-spy record -o cpu.svg --pid $!

# Profile a Jupyter notebook
# In a notebook cell:
# %load_ext memray
# %memray_flamegraph
# result = expensive_function()

# Transform binary output to different formats
memray flamegraph profile.bin -o flame.html
memray table profile.bin -o table.html
memray tree profile.bin > tree.txt
memray stats profile.bin > stats.txt

Fehlerbehebung

# "Error: only Linux is supported" — Memray is Linux-only
# Use Docker on macOS/Windows:
# docker run -it --rm -v $(pwd):/app python:3.11 bash
# pip install memray && memray run /app/my_script.py

# Missing native frames — compile extensions with debug info
CFLAGS="-g" pip install numpy

# Large output files — use aggregate mode
memray run --aggregate -o compact.bin my_script.py

# Profile hangs — ensure no stdin reading in profiled script
memray run --force my_script.py < /dev/null

Kurzreferenz

CommandPurpose
memray runRecord memory allocations
memray flamegraphGenerate interactive HTML flame graph
memray tableGenerate HTML allocation table
memray treeShow allocation call tree
memray summaryHigh-level allocation summary
memray statsDetailed allocation statistics
memray run --liveReal-time TUI tracking
memray run --nativeInclude C/C++ allocations
--leaksFilter to leaked allocations only
--temporary-allocationsShow freed allocations