V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
starry97
V2EX  ›  Python

Python 内存问题

  •  
  •   starry97 · 2020-03-09 20:24:03 +08:00 · 2261 次点击
    这是一个创建于 1513 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    7 条回复    2020-03-10 10:30:39 +08:00
    wuwukai007
        1
    wuwukai007  
       2020-03-09 20:29:49 +08:00
    a.clear()
    b.clear()
    c.clear()
    del a,b,c
    starry97
        2
    starry97  
    OP
       2020-03-09 20:33:36 +08:00
    @wuwukai007 list 好像没有 clear 方法吧,

    主要是想知道 python 的回收机制咋弄的,这些变量已经没使用了怎么还存在
    starry97
        3
    starry97  
    OP
       2020-03-09 20:35:58 +08:00
    @wuwukai007 使用的是 Python2.7 版本
    chenxytw
        4
    chenxytw  
       2020-03-09 20:55:35 +08:00
    没复现。。。。。
    lxy42
        5
    lxy42  
       2020-03-09 22:02:57 +08:00
    没有复现, 理论上 del a b c 会 free 它们的内存.
    lxy42
        6
    lxy42  
       2020-03-09 22:06:24 +08:00
    这里的回收机制就是引用计数器变为 0, 调用 list 的 list_dealloc 方式释放占用的内存, 最终调用 C 的 free 函数.
    chenstack
        7
    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 '
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   902 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:26 · PVG 03:26 · LAX 12:26 · JFK 15:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.