Python 内存问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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

Python 内存问题

  •  
  •   starry97 Mar 9, 2020 3016 views
    This topic created in 2242 days ago, the information mentioned may be changed or develped.

    现在遇见一个奇怪的问题 使用的 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 replies    2020-03-10 10:30:39 +08:00
    wuwukai007
        1
    wuwukai007  
       Mar 9, 2020
    a.clear()
    b.clear()
    c.clear()
    del a,b,c
    starry97
        2
    starry97  
    OP
       Mar 9, 2020
    @wuwukai007 list 好像没有 clear 方法吧,

    主要是想知道 python 的回收机制咋弄的,这些变量已经没使用了怎么还存在
    starry97
        3
    starry97  
    OP
       Mar 9, 2020
    @wuwukai007 使用的是 Python2.7 版本
    chenxytw
        4
    chenxytw  
       Mar 9, 2020
    没复现。。。。。
    lxy42
        5
    lxy42  
       Mar 9, 2020
    没有复现, 理论上 del a b c 会 free 它们的内存.
    lxy42
        6
    lxy42  
       Mar 9, 2020
    这里的回收机制就是引用计数器变为 0, 调用 list 的 list_dealloc 方式释放占用的内存, 最终调用 C 的 free 函数.
    chenstack
        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'
    About     Help     Advertise     Blog     API     FAQ     Solana     3201 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 36ms UTC 12:38 PVG 20:38 LAX 05:38 JFK 08:38
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86