Trying to understand python memory profiler

爷,独闯天下 提交于 2019-12-08 07:55:20

问题


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

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

回答1:


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).



来源:https://stackoverflow.com/questions/45318536/trying-to-understand-python-memory-profiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!