Total memory used by Python process?

前端 未结 12 716
眼角桃花
眼角桃花 2020-11-22 04:28

Is there a way for a Python program to determine how much memory it\'s currently using? I\'ve seen discussions about memory usage for a single object, but what I need is tot

12条回答
  •  再見小時候
    2020-11-22 04:49

    Here is a useful solution that works for various operating systems, including Linux, Windows, etc.:

    import os
    import psutil
    process = psutil.Process(os.getpid())
    print(process.memory_info().rss)  # in bytes 
    

    With Python 2.7 and psutil 5.6.3, the last line should be

    print(process.memory_info()[0])
    

    instead (there was a change in the API later).

    Note: do pip install psutil if it is not installed yet.

提交回复
热议问题