Python 内存问题

2020-03-09 20:24:03 +08:00
 starry97

现在遇见一个奇怪的问题 使用的 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

2278 次点击
所在节点    Python
7 条回复
wuwukai007
2020-03-09 20:29:49 +08:00
a.clear()
b.clear()
c.clear()
del a,b,c
starry97
2020-03-09 20:33:36 +08:00
@wuwukai007 list 好像没有 clear 方法吧,

主要是想知道 python 的回收机制咋弄的,这些变量已经没使用了怎么还存在
starry97
2020-03-09 20:35:58 +08:00
@wuwukai007 使用的是 Python2.7 版本
chenxytw
2020-03-09 20:55:35 +08:00
没复现。。。。。
lxy42
2020-03-09 22:02:57 +08:00
没有复现, 理论上 del a b c 会 free 它们的内存.
lxy42
2020-03-09 22:06:24 +08:00
这里的回收机制就是引用计数器变为 0, 调用 list 的 list_dealloc 方式释放占用的内存, 最终调用 C 的 free 函数.
chenstack
2020-03-10 10:30:39 +08:00
没有复现,我这边运行的结果
python2 test.py
Filename: test.py

Line #    Mem usage    Increment   Line Contents
================================================
     3     12.8 MiB     12.8 MiB   @profile
     4                             def test():
     5     89.2 MiB     76.4 MiB      a = [1] * 10000000
     6    165.5 MiB     76.3 MiB      b = [2] * 10000000
     7    241.8 MiB     76.3 MiB      c = [3] * 10000000
     8    318.2 MiB     76.3 MiB      d = [4] * 10000000
     9    394.5 MiB     76.3 MiB      e = [5] * 10000000
    10    470.8 MiB     76.3 MiB      f = [6] * 10000000
    11    394.5 MiB      0.0 MiB      del a
    12    318.2 MiB      0.0 MiB      del b
    13    241.9 MiB      0.0 MiB      del c


end 
Filename: test.py

Line #    Mem usage    Increment   Line Contents
================================================
    16     12.8 MiB     12.8 MiB   @profile
    17                             def test1():
    18     13.0 MiB      0.2 MiB      test()
    19     13.0 MiB      0.0 MiB      gc.collect()
    20     13.0 MiB      0.0 MiB      print 'end '

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/651318

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX