彭博社开源 Memray，一个 Python 内存剖析器
Python
![](/data/attachment/album/202205/12/081556a84j8f331irlszo2.jpg)


Memray 是一个由彭博社开发的<ruby> 内存剖析器 <rt>  memory profiler </rt></ruby>，现在已经开源。它可以跟踪 Python 代码中的内存分配，包括本地扩展和 Python 解释器本身。内存剖析是了解程序如何利用内存的有力工具，因此可以检测内存泄漏或确定程序中哪些区域消耗的内存最多。


与 py-spy 等抽样内存剖析器相比，Memray 可以跟踪每个函数调用，包括对 C/C++ 库的调用，并详细显示调用栈。彭博社称，这并不以牺牲性能为代价，剖析只使解释代码的速度变慢一点。然而，原生代码剖析的速度较慢，因此需要直接启用。


Memray 可以根据获得的内存消耗数据生成各种报告，包括火焰图，这对快速、准确地识别最常见的代码路径很有价值。


据 EgdeDB 的联合创始人兼 CEO Yury Selivanov 称，该工具提供了以前无法获得的对 Python 应用的洞察力。Memray 可以用来从命令行中执行和剖析 Python 应用。



```
$ python3 -m memray run -o output.bin my_script.py
$ python3 -m memray flamegraph output.bin

```

另外，你可以使用 pytest-memray 将 Memray 集成到你的测试套件中。你也可以用 `-native` 命令行选项对所有的 C/C++ 调用进行剖析，或者用 `-live` 命令行选项在程序执行过程中实时分析内存分配。Memray 可以在 Linux x86/64 系统上用 `python3 -m pip install memray` 来安装。


（题图由 [Frantisek Krejci](https://pixabay.com/zh/users/frantisek_krejci-810589/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=7152438) 在 [Pixabay](https://pixabay.com/zh/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=7152438) 上发布 ）




---


via: <https://www.opensourceforu.com/2022/04/bloomberg-open-sources-memray-a-python-memory-profiler/>


作者：[Laveesh Kocher](https://www.opensourceforu.com/author/laveesh-kocher/) 选题：[lkxed](https://github.com/lkxed) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Memray is a memory profiler that was developed at Bloomberg and is now open source. It can track memory allocations in Python code, including native extensions and the Python interpreter itself. Memory profiling is a strong tool for understanding how a program utilises memory and, as a result, detecting memory leaks or determining which areas of the program consume the most memory.

In contrast to sampling memory profilers like py-spy, Memray can track every function call, including calls into C/C++ libraries, and display the call stack in detail. Bloomberg claims that this does not come at the sacrifice of performance, with profiling only slowing down interpreted code by a little amount. However, native code profiling is slower and must be enabled directly.

Memray may generate a variety of reports based on the acquired memory consumption data, including flame graphs, which are valuable for rapidly and precisely identifying the most common code-paths.

According to Yury Selivanov, co-founder and CEO of EgdeDB, the tool gives previously unavailable insights into Python applications. Memray can be used to execute and profile a Python application from the command line:

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

Alternatively, you can use pytest-memray to integrate Memray into your test suite. You can also profile all C/C++ calls with the —native command line option, or analyse memory allocation in real time while a programme is executing with the —live command line option. Memray can be installed with python3 -m pip install memray on a Linux x86/64 system.