我试图通过重新创建memory_profiler文档中给出的示例来衡量python函数的内存使用情况。我只希望每隔0.1秒就有一个内存使用量的输出向量,从中可以找到最大内存使用量。当我运行它时,我遇到了我无法理解的运行时错误。
我以各种方式更改了代码,并在网上查看,但似乎无法修复。
from memory_profiler import memory_usage
import time
from time import sleep
def f():
# a function that has growing memory requirements with time
a = 1
sleep(.1)
b = a*1000
sleep(.1)
c = b*1000
return a
mem_usage = memory_usage(f) # run function while tracking memory usage
every 0.1 seconds
print('Memory usage (in chunks of 0.1 seconds): %s' %mem_usage)
print('Maximum memory usage: %s' %max(mem_usage))
我遇到以下运行时错误:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start you
child processes and you have forgotten to use the proper idi
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.