真正的内存vs配置文件内存python [英] real memory vs profiled memory python
问题描述
使用 memory_profiler 来协助需要在不同位置释放内存的项目。开发环境是OS X雪豹。
如下图所示,配置文件内存高达 414.699 MiB
,但活动监视器显示该过程的峰值将近两倍(超过 900 MB
)。
行编号内存使用增量行内容
============================== ==================
24 20.441 MiB 0.000 MiB @profile
25 def do_work():
26按顺序调用每个函数
27 20.445 MiB 0.004 MiB x = audio.AudioQuantumList()
28 137.098 MiB 116.652 MiB audiofile = make_objects(/ Users / path / audio / Track01.mp3)
29 295.480 MiB 158.383 MiB audiofile2 = make_objects(/ Users / path / audio / Track02.mp3)
30 414.699 MiB 119.219 MiB audiofile3 = make_objects(/ Users / path / audio / Track03.mp3)
31 414.699 MiB 0.000 MiB x = add_to_list(audiofile,x)
32 417.426 MiB 2.727 MiB audiofile = clear_memory(audiofile)
33 417.426 MiB 0.000 MiB gc.collect()
34 417.426 MiB 0.000 MiB x = add_to_list audiofile2,x)
35 425.047 MiB 7.621 MiB audiofile2 = clear_memory(audiofile2)
36 285.344 MiB -139.703 MiB gc.collect()
37 285.344 MiB 0.000 MiB x = add_to_list(audiofile3,x )
38 340.082 MiB 54.738 MiB audiofile3 = clear_memory(audiofile3)
39 339.582 MiB -0.500 MiB gc.collect()
memory_profiler显示CPU实际使用的内存的一半是否正常?那么事实上这里发生了什么?
还要注意,当 gc.collect
没有明确调用时,在 36
行被释放的 -139.703 MiB
成为:
======================================== ========
35 374.895 MiB -45.617 MiB audiofile2 = clear_memory(audiofile2)
在逐行报告中, memory_profiler
会测量每行执行后的内存使用情况。在函数内部的内存峰值中,例如在 make_objects
内部,但是在函数返回之前释放内存,那么memory_profiler将不会报告该用法。
变通办法还包括装饰嵌套函数(例如make_objects)或使用mprof(与memory_profiler一起发布)来报告内存使用情况作为时间函数。
Using memory_profiler to aid in project that is requiring freeing up some memory at various points. The development environment is OS X snow leopard.
The profiled memory, as shown below, is peaking at around 414.699 MiB
, but the Activity Monitor is showing the process peaking at nearly twice that (more than 900 MB
).
Line # Mem usage Increment Line Contents
================================================
24 20.441 MiB 0.000 MiB @profile
25 def do_work():
26 "Call each function in order"
27 20.445 MiB 0.004 MiB x = audio.AudioQuantumList()
28 137.098 MiB 116.652 MiB audiofile = make_objects("/Users/path/audio/Track01.mp3")
29 295.480 MiB 158.383 MiB audiofile2 = make_objects("/Users/path/audio/Track02.mp3")
30 414.699 MiB 119.219 MiB audiofile3 = make_objects("/Users/path/audio/Track03.mp3")
31 414.699 MiB 0.000 MiB x = add_to_list(audiofile, x)
32 417.426 MiB 2.727 MiB audiofile = clear_memory(audiofile)
33 417.426 MiB 0.000 MiB gc.collect()
34 417.426 MiB 0.000 MiB x = add_to_list(audiofile2, x)
35 425.047 MiB 7.621 MiB audiofile2 = clear_memory(audiofile2)
36 285.344 MiB -139.703 MiB gc.collect()
37 285.344 MiB 0.000 MiB x = add_to_list(audiofile3, x)
38 340.082 MiB 54.738 MiB audiofile3 = clear_memory(audiofile3)
39 339.582 MiB -0.500 MiB gc.collect()
Is it normal for memory_profiler to show half of the memory that's actually being used by the CPU(s)? And is that in fact what is occurring here?
Also note that when gc.collect
is not explicitly called, the -139.703 MiB
freed at line 36
become:
================================================
35 374.895 MiB -45.617 MiB audiofile2 = clear_memory(audiofile2)
In the line-by-line report, memory_profiler
measures the memory usage after the execution of each line. In the memory peaks inside a function, e.g inside make_objects
but the memory is released before the function returns, then memory_profiler will not report that usage.
Workarounds include decorating also the nested functions (e.g. make_objects) or using mprof (distributed with memory_profiler) to report memory usage as a function of time.
这篇关于真正的内存vs配置文件内存python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!