通过Python函数跟踪*最大*内存使用情况

ast*_*rog 38 python memory profiling

我想知道在调用函数期间分配的最大RAM量是多少(在Python中).关于跟踪RAM使用的SO还有其他问题:

建议使用哪种Python内存分析器?

如何在Python中分析内存使用情况?

但是那些似乎允许你在heap()调用方法(在guppy的情况下)时更多地跟踪内存使用情况.但是,我想要跟踪的是外部库中的一个函数,我无法修改它,并且它会增长以使用大量的RAM,但是一旦函数执行完成就会释放它.有没有办法找出函数调用期间使用的RAM总量是多少?

Ada*_*wis 26

这个问题看起来很有趣,这让我有理由去看看Guppy/Heapy,感谢你.

我试图约2小时到达Heapy做监控而不修改其源函数调用/过程运气.

我确实找到了使用内置Python库完成任务的方法resource.请注意,文档未指出RU_MAXRSS值返回的值.另一个SO用户注意到它是以kB为单位的.运行Mac OSX 7.3并观察我的系统资源在下面的测试代码中爬升,我相信返回的值是以字节为单位,而不是KBytes.

关于我如何使用resource库监视库调用的10000英尺视图是在一个单独的(可监视的)线程中启动该函数,并在主线程中跟踪该进程的系统资源.下面我有两个你需要运行的文件来测试它.

图书馆资源监控 - whatever_you_want.py

import resource
import time

from stoppable_thread import StoppableThread


class MyLibrarySniffingClass(StoppableThread):
    def __init__(self, target_lib_call, arg1, arg2):
        super(MyLibrarySniffingClass, self).__init__()
        self.target_function = target_lib_call
        self.arg1 = arg1
        self.arg2 = arg2
        self.results = None

    def startup(self):
        # Overload the startup function
        print "Calling the Target Library Function..."

    def cleanup(self):
        # Overload the cleanup function
        print "Library Call Complete"

    def mainloop(self):
        # Start the library Call
        self.results = self.target_function(self.arg1, self.arg2)

        # Kill the thread when complete
        self.stop()

def SomeLongRunningLibraryCall(arg1, arg2):
    max_dict_entries = 2500
    delay_per_entry = .005

    some_large_dictionary = {}
    dict_entry_count = 0

    while(1):
        time.sleep(delay_per_entry)
        dict_entry_count += 1
        some_large_dictionary[dict_entry_count]=range(10000)

        if len(some_large_dictionary) > max_dict_entries:
            break

    print arg1 + " " +  arg2
    return "Good Bye World"

if __name__ == "__main__":
    # Lib Testing Code
    mythread = MyLibrarySniffingClass(SomeLongRunningLibraryCall, "Hello", "World")
    mythread.start()

    start_mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    delta_mem = 0
    max_memory = 0
    memory_usage_refresh = .005 # Seconds

    while(1):
        time.sleep(memory_usage_refresh)
        delta_mem = (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) - start_mem
        if delta_mem > max_memory:
            max_memory = delta_mem

        # Uncomment this line to see the memory usuage during run-time 
        # print "Memory Usage During Call: %d MB" % (delta_mem / 1000000.0)

        # Check to see if the library call is complete
        if mythread.isShutdown():
            print mythread.results
            break;

    print "\nMAX Memory Usage in MB: " + str(round(max_memory / 1000.0, 3))
Run Code Online (Sandbox Code Playgroud)

可停止的线程 - stoppable_thread.py

import threading
import time

class StoppableThread(threading.Thread):
    def __init__(self):
        super(StoppableThread, self).__init__()
        self.daemon = True
        self.__monitor = threading.Event()
        self.__monitor.set()
        self.__has_shutdown = False

    def run(self):
        '''Overloads the threading.Thread.run'''
        # Call the User's Startup functions
        self.startup()

        # Loop until the thread is stopped
        while self.isRunning():
            self.mainloop()

        # Clean up
        self.cleanup()

        # Flag to the outside world that the thread has exited
        # AND that the cleanup is complete
        self.__has_shutdown = True

    def stop(self):
        self.__monitor.clear()

    def isRunning(self):
        return self.__monitor.isSet()

    def isShutdown(self):
        return self.__has_shutdown


    ###############################
    ### User Defined Functions ####
    ###############################

    def mainloop(self):
        '''
        Expected to be overwritten in a subclass!!
        Note that Stoppable while(1) is handled in the built in "run".
        '''
        pass

    def startup(self):
        '''Expected to be overwritten in a subclass!!'''
        pass

    def cleanup(self):
        '''Expected to be overwritten in a subclass!!'''
        pass
Run Code Online (Sandbox Code Playgroud)


Fab*_*osa 22

可以使用memory_profiler执行此操作.该函数memory_usage返回一个值列表,这些值表示一段时间内的内存使用情况(默认情况下为.1秒的块).如果您需要最大值,请使用该列表的最大值.小例子:

from memory_profiler import memory_usage
from time import sleep

def f():
    # a function that with growing
    # memory consumption
    a = [0] * 1000
    sleep(.1)
    b = a * 100
    sleep(.1)
    c = b * 100
    return a

mem_usage = memory_usage(f)
print('Memory usage (in chunks of .1 seconds): %s' % mem_usage)
print('Maximum memory usage: %s' % max(mem_usage))
Run Code Online (Sandbox Code Playgroud)

在我的情况下(memory_profiler 0.25)如果打印以下输出:

Memory usage (in chunks of .1 seconds): [45.65625, 45.734375, 46.41015625, 53.734375]
Maximum memory usage: 53.734375
Run Code Online (Sandbox Code Playgroud)

  • 如果函数`f`有参数`args`和关键字参数`kw`,你可以用`memory_usage((f,args,kw))`来调用它. (5认同)
  • 你可能想返回 main 函数的返回值,那么你应该设置 `retval=True` 就像 `mem_usage, result = memory_usage((f, args, kwargs), retval=True)` (4认同)

Jen*_*aKh 11

改进@Vader B 的答案(因为它对我来说不起作用):

$ /usr/bin/time --verbose  ./myscript.py
        Command being timed: "./myscript.py"
        User time (seconds): 16.78
        System time (seconds): 2.74
        Percent of CPU this job got: 117%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:16.58
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 616092   # WE NEED THIS!!!
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 432750
        Voluntary context switches: 1075
        Involuntary context switches: 118503
        Swaps: 0
        File system inputs: 0
        File system outputs: 800
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0
Run Code Online (Sandbox Code Playgroud)


小智 5

这似乎适用于Windows.不了解其他操作系统.

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
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以使用 python 库资源来获取内存使用情况。

import resource
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
Run Code Online (Sandbox Code Playgroud)

它将以千字节为单位提供内存使用量,以 MB 除以 1000 进行转换。