Determine free RAM in Python

后端 未结 5 1501

I would like my python script to use all the free RAM available but no more (for efficiency reasons). I can control this by reading in only a limited amount of data but I n

5条回答
  •  無奈伤痛
    2020-12-15 08:26

    Another option is the built-in Python package psutil:

    stats = psutil.virtual_memory()  # returns a named tuple
    available = getattr(stats, 'available')
    print(available)
    

    According to the documentation, the available field "is calculated by summing different memory values depending on the platform and it is supposed to be used to monitor actual memory usage in a cross platform fashion."

    Note the return value will be in bytes

提交回复
热议问题