我想测量代码中每个for循环的RAM使用情况。我在互联网上搜索并找到了
process = psutil.Process(os.getpid())
和print(process.memory_info().rss)
来测量RAM。但是这段代码获取整个进程的pid而不是特定的部分。
有没有办法测量代码中每个部分的RAM使用情况?例如,在下面的代码中,我们有3个for
循环,它们填充3个不同的字典。 我想打印出每个for循环的RAM使用情况,并在处理每个循环之间,如果RAM超过一个阈值,我想终止该for循环。dict1 = {}
dict2 = {}
dict3 = {}
for i in range (200):
do something with dict1
if RAM usage of this block exceeds 1GB then break
this loop used: x Mb
for i in range (500):
do something with dict2
if RAM usage of this block exceeds 1GB then break
this loop used: x2 Mb
for i in range (800):
do something with dict3
if RAM usage of this block exceeds 1GB then break
this loop used: x3 Mb
我很感激能够帮助我很多的回答。