Tracking *maximum* memory usage by a Python function

前端 未结 8 1637
轻奢々
轻奢々 2020-12-02 09:07

I want to find out what the maximum amount of RAM allocated during the call to a function is (in Python). There are other questions on SO related to tracking RAM usage:

相关标签:
8条回答
  • 2020-12-02 10:00

    This appears to work under Windows. Don't know about other operating systems.

    In [50]: import os
    
    In [51]: import psutil
    
    In [52]: process = psutil.Process(os.getpid())
    
    In [53]: process.get_ext_memory_info().peak_wset
    Out[53]: 41934848
    
    0 讨论(0)
  • 2020-12-02 10:01

    Have been struggling with this task as well. After experimenting with psutil and methods from Adam, I wrote a function (credits to Adam Lewis) to measure the memory used by a specific function. People may find it easier to grab and use.

    1) measure_memory_usage

    2) test measure_memory_usage

    I found that materials about threading and overriding superclass are really helpful in understanding what Adam is doing in his scripts. Sorry I cannot post the links due to my "2 links" maximum limitation.

    0 讨论(0)
提交回复
热议问题