试图了解python内存探查器 [英] Trying to understand python memory profiler
问题描述
我正在使用 Memory Profiler 模块来获取我的python代码的内存使用情况遵循此答案.但是,我无法解释%memit
魔术的输出(或者无法使用模块的@profile
装饰器或mprof run
的输出).
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).
例如
%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()
返回列表之间的区别.我在这里使用它们只是为了演示这两种情况.
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.
我的问题是
-
peak memory
和increment
的实际含义是什么? - 我应该从此输出报告什么是脚本(或函数)的总内存使用量?
- What does
peak memory
andincrement
actually mean? - What should I report as the total memory usage of a script (or a function) from this output?
推荐答案
峰值内存是指程序运行时系统的峰值内存使用情况(包括其他进程的内存使用情况).
Peak memory refers to the peak memory usage of your system (including memory usage of other processes) during the program runtime.
增量是程序运行前相对于内存使用量的内存使用量增量(即increment
= peak memory
-starting memory
).
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
).
因此您将报告increment
.峰值内存仅能帮助您确定程序运行期间离使用所有RAM的距离.
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
本质上是为第6行提供了内存使用量,但报告了相对于第4行(实际上是第1行,但大概在第1-3行中没有计算)的增量.
%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).
这篇关于试图了解python内存探查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!