Memray is a memory profiler for Python that tracks heap allocations in both Python code and native C or C++ extensions. It captures memory events by hooking into the language runtime and traversing call stacks, providing a comprehensive view of how an application consumes memory. The tool is designed to minimize performance impact on the target application by using thread-local buffering and streaming data to an external process or file.
The project distinguishes itself through its ability to monitor complex, multi-threaded systems and child processes in real-time. It provides diagnostic utilities that allow developers to attach to running processes, filter memory data by specific threads, and visualize allocation patterns through interactive flame graphs. These graphs support temporal inspection, enabling users to analyze memory usage across specific time intervals during execution.
Beyond live monitoring, the tool includes mechanisms for automated memory validation and regression testing. It can detect memory leaks by identifying objects that remain in memory at the end of a session and allows developers to enforce resource consumption thresholds within continuous integration pipelines. The profiler also generates detailed statistics and hierarchical call graphs, which help pinpoint inefficient allocation patterns such as frequent, short-lived object creation.