真正的内存vs配置文件内存python [英] real memory vs profiled memory python

查看:156
本文介绍了真正的内存vs配置文件内存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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆