1
使用memory_profiler來幫助需要釋放一些內存的項目。開發環境是OS X雪豹。真實內存vs異形內存python
如下圖所示,配置文件的內存高達414.699 MiB
左右,但活動監視器顯示的進程達到峯值的近兩倍(大於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()
memory_profiler是否正常顯示CPU實際使用的內存的一半?實際上這裏發生了什麼?
還要注意的是,當gc.collect
不顯式調用,在行36
釋放了-139.703 MiB
成爲:
================================================
35 374.895 MiB -45.617 MiB audiofile2 = clear_memory(audiofile2)