memray
TLDR
Profile memory usage
SYNOPSIS
memray [run] [flamegraph] [summary] [tree] [attach] [options] [args]
DESCRIPTION
memray profiles Python memory allocations, tracking where memory is allocated and by which code. It helps identify memory leaks, high-water marks, and allocation patterns.
The tool tracks every allocation and deallocation, recording stack traces. Post-run analysis generates visualizations showing memory usage over time and by location.
Flame graphs show allocation call stacks, with width representing allocation size. They quickly identify which functions allocate most memory.
Live mode provides real-time visualization during execution. It's useful for understanding memory patterns in long-running applications.
Native tracking includes C extension allocations, not just Python objects. This reveals memory used by numpy, pandas, and other libraries with native components.
Process attachment profiles already-running applications, useful for production debugging without restart.
PARAMETERS
run SCRIPT
Profile script execution.flamegraph FILE
Generate flame graph HTML.summary FILE
Show allocation summary.tree FILE
Show tree view of allocations.stats FILE
Show statistics.table FILE
Show table of allocations.attach PID
Attach to running process.-o FILE, --output FILE
Output file for profile data.--live
Show live memory view.--native
Track native (C/C++) allocations.--follow-fork
Follow child processes.-f, --force
Overwrite existing output.
CAVEATS
Tracking overhead slows execution. Large profiles consume disk space. Native tracking requires debug symbols for readable output. Some allocations may be missed in highly optimized code.
HISTORY
memray was developed by Bloomberg and released as open source in 2022. It was designed for debugging memory issues in large-scale Python applications used in financial services.
SEE ALSO
py-spy(1), tracemalloc(3), valgrind(1), heaptrack(1)


