gpt4 book ai didi

python - 推荐使用哪种 Python 内存分析器?

转载 作者:IT老高 更新时间:2023-10-28 12:00:23 25 4
gpt4 key购买 nike

我想知道我的 Python 应用程序的内存使用情况,特别想知道哪些代码块/部分或对象占用了最多的内存。谷歌搜索显示一个商业广告是 Python Memory Validator (仅限 Windows)。

而开源的是 PySizerHeapy .

我没有尝试过任何人,所以我想知道哪个是最好的考虑:

  1. 提供大部分细节。

  2. 我必须对我的代码做最少的更改或不做任何更改。

最佳答案

我的模块memory_profiler能够打印内存使用的逐行报告,并且可以在 Unix 和 Windows 上运行(最后一个需要 psutil)。输出不是很详细,但目的是让您大致了解代码消耗更多内存的位置,而不是对已分配对象的详尽分析。

在使用 @profile 装饰您的函数并使用 -m memory_profiler 标志运行您的代码后,它将打印如下逐行报告:

Line #    Mem usage  Increment   Line Contents
==============================================
3 @profile
4 5.97 MB 0.00 MB def my_func():
5 13.61 MB 7.64 MB a = [1] * (10 ** 6)
6 166.20 MB 152.59 MB b = [2] * (2 * 10 ** 7)
7 13.61 MB -152.59 MB del b
8 13.61 MB 0.00 MB return a

关于python - 推荐使用哪种 Python 内存分析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/110259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com