
现在遇见一个奇怪的问题 使用的 Python2.7
代码:
from memory_profiler import profile import gc @profile def test(): a = [1] * 10000000 b = [2] * 10000000 c = [3] * 10000000 d = [4] * 10000000 e = [5] * 10000000 f = [6] * 10000000 del a del b del c @profile def test1(): test() gc.collect() print 'end ' if __name__ == '__main__': test1() 发现 a,b,c,d,e,f 这些变量在调用完 test 后还存在,执行了 gc.collect()还是存在
Line # Mem usage Increment Line COntents================================================ 26 36.4 MiB 36.4 MiB @profile 27 def test(): 28 112.7 MiB 76.3 MiB a = [1] * 10000000 29 189.0 MiB 76.3 MiB b = [2] * 10000000 30 265.3 MiB 76.3 MiB c = [3] * 10000000 31 341.6 MiB 76.3 MiB d = [4] * 10000000 32 417.9 MiB 76.3 MiB e = [5] * 10000000 33 494.2 MiB 76.3 MiB f = [6] * 10000000 34 494.2 MiB 0.0 MiB del a 35 494.2 MiB 0.0 MiB del b 36 494.2 MiB 0.0 MiB del c Line # Mem usage Increment Line COntents================================================ 39 36.4 MiB 36.4 MiB @profile 40 def test1(): 41 494.2 MiB 457.8 MiB test() 42 494.2 MiB 0.0 MiB gc.collect() 43 494.2 MiB 0.0 MiB print 'end ' 不知道怎么上传图片,抱歉只能这么看了 0.0
1 wuwukai007 Mar 9, 2020 a.clear() b.clear() c.clear() del a,b,c |
2 starry97 OP |
3 starry97 OP @wuwukai007 使用的是 Python2.7 版本 |
4 chenxytw Mar 9, 2020 没复现。。。。。 |
5 lxy42 Mar 9, 2020 没有复现, 理论上 del a b c 会 free 它们的内存. |
6 lxy42 Mar 9, 2020 这里的回收机制就是引用计数器变为 0, 调用 list 的 list_dealloc 方式释放占用的内存, 最终调用 C 的 free 函数. |
7 chenstack Mar 10, 2020 没有复现,我这边运行的结果 python2test.py Filename:test.py Line#MemusageIncrementLineContents ================================================ 312.8MiB12.8MiB@profile 4deftest(): 589.2MiB76.4MiBa=[1]*10000000 6165.5MiB76.3MiBb=[2]*10000000 7241.8MiB76.3MiBc=[3]*10000000 8318.2MiB76.3MiBd=[4]*10000000 9394.5MiB76.3MiBe=[5]*10000000 10470.8MiB76.3MiBf=[6]*10000000 11394.5MiB0.0MiBdela 12318.2MiB0.0MiBdelb 13241.9MiB0.0MiBdelc end Filename:test.py Line#MemusageIncrementLineContents ================================================ 1612.8MiB12.8MiB@profile 17deftest1(): 1813.0MiB0.2MiBtest() 1913.0MiB0.0MiBgc.collect() 2013.0MiB0.0MiBprint'end' |