Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Bloomberg Open-Sources Python Memory Profiler Memray

Bloomberg Open-Sources Python Memory Profiler Memray

This item in japanese

Developed at Bloomberg and now open source, Memray is a memory profiler that can track memory allocations in Python code, including native extensions and in the Python interpreter itself.

Memory profiling is a very powerful technique that makes it possible to understand how a program uses memory, thus helping to detect memory leaks or understand which parts of the program cause the most memory usage.

Contrary to sampling memory profilers such as py-spy, Memray is able to trace every function call, including calls into C/C++ libraries, providing a detailed representation of the call stack. According to Bloomberg, this does not come at the expense of performance, with profiling only minimally slowing down interpreted code. Native code profiling tends to be slower, though, but it must be explicitly enabled.

Memray is able to generate various kinds of reports about the collected memory usage data, including flame graphs, which are useful to identify the most frequent code-paths quickly and accurately.

According to Yury Selivanov, co-founder and CEO of EgdeDB, the tool provides unique insights into Python programs that were not available previously.

The tool is a must for any long-running services implemented with Python. With memray you can generate flame charts or all allocations and trace absolutely everything.

Memray can be used as a command-line tool to run and profile a Python program:

$ python3 -m memray run -o output.bin
$ python3 -m memray flamegraph output.bin

Alternatively, Memray can be integrated in your test suite using pytest-memray.

In addition, you can use the --native command line option to have all C/C++ calls profiled or the --live option to inspect memory allocation in real-time while a program is running.

Memray requires a Linux x86/64 box and can be installed using python3 -m pip install memray.

About the Author

Rate this Article