Releasing memory in Python

前端 未结 4 1347
南方客
南方客 2020-11-22 06:51

I have a few related questions regarding memory usage in the following example.

  1. If I run in the interpreter,

    foo = [\'bar\' for _ in xrange(         
    
    
            
4条回答
  •  不知归路
    2020-11-22 07:00

    First, you may want to install glances:

    sudo apt-get install python-pip build-essential python-dev lm-sensors 
    sudo pip install psutil logutils bottle batinfo https://bitbucket.org/gleb_zhulik/py3sensors/get/tip.tar.gz zeroconf netifaces pymdstat influxdb elasticsearch potsdb statsd pystache docker-py pysnmp pika py-cpuinfo bernhard
    sudo pip install glances
    

    Then run it in the terminal!

    glances
    

    In your Python code, add at the begin of the file, the following:

    import os
    import gc # Garbage Collector
    

    After using the "Big" variable (for example: myBigVar) for which, you would like to release memory, write in your python code the following:

    del myBigVar
    gc.collect()
    

    In another terminal, run your python code and observe in the "glances" terminal, how the memory is managed in your system!

    Good luck!

    P.S. I assume you are working on a Debian or Ubuntu system

提交回复
热议问题