试图了解python内存分析器(Trying to understand python memory profiler)

编程入门 行业动态 更新时间:2024-10-25 11:24:28
试图了解python内存分析器(Trying to understand python memory profiler)

我正在使用Memory Profiler模块来获取我的python代码的内存使用情况。 但是,我无法解释%memit magic的输出(或使用模块中的@profile装饰器或mprof run的输出)。

例如,

%memit range(10000)

给我peak memory: 97.41 MiB, increment: 0.24 MiB

而,

%memit xrange(10000)

显示peak memory: 97.46 MiB, increment: 0.00 MiB 。 我确实理解xrange返回xrange type而不是range()返回列表之间的区别。 我在这里使用它们只是为了演示这两种情况。

我的问题是

peak memory和increment实际意味着什么? 我应该从此输出报告脚本(或函数)的总内存使用量?

I am using Memory Profiler module to get the memory usage of my python code following this answer. However, I am not able to interpret the output from %memit magic (or the output using the @profile decorator from the module or mprof run for that matter).

For instance,

%memit range(10000)

gives me peak memory: 97.41 MiB, increment: 0.24 MiB

while,

%memit xrange(10000)

shows peak memory: 97.46 MiB, increment: 0.00 MiB. I do understand the difference between xrange returning an xrange type as opposed to range() returning a list. I used them here just to demonstrate the two scenarios.

My question is

What does peak memory and increment actually mean? What should I report as the total memory usage of a script (or a function) from this output?

最满意答案

峰值内存是指程序运行期间系统的峰值内存使用量(包括其他进程的内存使用量)。

增量是内存使用量相对于程序运行前的内存使用量的increment (即increment = peak memory - starting memory )。

所以你要报告increment 。 峰值内存只是帮助您计算在程序运行期间使用所有RAM的距离。

如果您参考自述文件的使用部分中的逐行示例:

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

%memit是从第6行给你的内存使用,但报告相对于第4行的增量(实际上是第1行,但可能是第1-3行没有计算)。

Peak memory refers to the peak memory usage of your system (including memory usage of other processes) during the program runtime.

Increment is the increment in memory usage relative to the memory usage just before the program is run (i.e. increment = peak memory - starting memory).

So you'd report increment. Peak memory just helps you figure how close you are to using all your RAM during a program run.

If you refer to the line-by-line example in the usage section of the readme:

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

%memit is essentially giving you the memory usage from line 6, but reports the increment relative to line 4 (actually line 1, but presumably, there's no computation in lines 1-3).

更多推荐

本文发布于:2023-08-02 17:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1378333.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分析器   内存   python   profiler   memory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!