python - 解释 python memory_profiler 的输出

标签 python memory-profiling

请原谅我这个幼稚的问题。我正在尝试监视我的 python 代码的内存使用情况,并且遇到了有前途的 memory_profiler包裹。我对解释@profile 装饰器生成的输出有疑问。

这是我通过运行下面的虚拟代码获得的示例输出:

虚拟.py

from memory_profiler import profile

@profile
def my_func():
   a = [1] * (10 ** 6)
   b = [2] * (2 * 10 ** 7)
   del b
   return a

if __name__ == '__main__':
   my_func()

通过“python dummy.py”调用 dummy.py 返回下表。

Line # Mem usage Increment Line Contents

 3      8.2 MiB      0.0 MiB   @profile
 4                             def my_func():
 5     15.8 MiB      7.6 MiB       a = [1] * (10 ** 6)
 6    168.4 MiB    152.6 MiB       b = [2] * (2 * 10 ** 7)
 7     15.8 MiB   -152.6 MiB       del b
 8     15.8 MiB      0.0 MiB       return a

我的问题是表格第一行的8.2 MiB对应什么。我的猜测是它是 python 解释器本身的初始内存使用;但我不确定。如果是这样,是否有办法从脚本的内存使用量中自动减去此基线使用量?

非常感谢您的时间和考虑!

能心

最佳答案

根据 the docs :

The first column represents the line number of the code that has been profiled, the second column (Mem usage) the memory usage of the Python interpreter after that line has been executed. The third column (Increment) represents the difference in memory of the current line with respect to the last one.

因此,8.2 MiB 是执行第一行后的内存使用量。这包括启动 Python、加载脚本及其所有导入(包括 memory_profiler 本身)等所需的内存。

似乎没有任何记录的选项可以从每个条目中删除它。但是对结果进行后处理并不难。

或者,您真的需要这样做吗?第三列显示了每行之后使用了多少额外内存,无论是那个,还是一系列行的总和,似乎比每行的第二列和开始之间的差异更有趣。

关于python - 解释 python memory_profiler 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20529457/

相关文章:

python - 如何在Python中使用selenium在不同WebDriver打开的不同chrome浏览器窗口之间切换?

python - 当我点击移动时,如何为 pygame 角色行走设置动画

python - Python:无法复制内存使用情况的测试

java - 如何分析在 cloud foundry 上运行的 spring boot webapp 的内存?

testing - 如何关联 ANTS Memory Profiler 中的过滤器以查找内存泄漏?

python - 在 Python 中解析标准输出

遍历和更新列表的Python列表

python - plotly 中的饼图

python - openCv captureFromCam内存泄漏?