memory_profiler各个参数都是什么意思?
存在疑惑
Line # Mem usage Increment Occurrences Line Contents
=============================================================1096 1581.2 MiB -92.0 MiB 12 while not done and t <= rollout_length:1097 1581.2 MiB -79.0 MiB 11 if config.test_mode == True and config.env_version == "v1" and config.test_ifrender:
while循环中,为什么这里的increment是负值,但是mem usage没有改变?
解决问题
其实主要是机制不理解,尝试使用以下的代码进行测试:
from memory_profiler import profile
@profile
def test_thing1():a = [1] * 1000000for i in range(3):a.extend([1] * 1000000)d = a[:30000]del ddel areturn True
@profile
def test_thing2():a = [1] * 1000000# i = 0a.extend([1] * 1000000)d = a[:30000]del d# i = 1a.extend([1] * 1000000)d = a[:30000]del d# i = 2a.extend([1] * 1000000)d = a[:30000]del d# out of fordel areturn True
if __name__ == "__main__":test_thing1()test_thing2()
这里两个test是完全一样的,只是一个用的是while,另一个用的是单独执行每一行的结果。
结果如下所示:
Filename: e:/pycharm/python_doc/learntoconverse_reconstructed/01example/009memory_profiler_test/test.pyLine # Mem usage Increment Occurrences Line Contents
=============================================================2 51.8 MiB 51.8 MiB 1 @profile3 def test_thing1():4 59.4 MiB 7.6 MiB 1 a = [1] * 10000005 82.5 MiB 0.0 MiB 4 for i in range(3):6 82.5 MiB 22.9 MiB 3 a.extend([1] * 1000000)7 82.5 MiB 0.2 MiB 3 d = a[:30000]8 82.5 MiB 0.0 MiB 3 del d9 52.0 MiB -30.5 MiB 1 del a10 52.0 MiB 0.0 MiB 1 return TrueFilename: e:/pycharm/python_doc/learntoconverse_reconstructed/01example/009memory_profiler_test/test.pyLine # Mem usage Increment Occurrences Line Contents
=============================================================11 52.0 MiB 52.0 MiB 1 @profile12 def test_thing2():13 59.7 MiB 7.6 MiB 1 a = [1] * 100000014 # i = 015 67.3 MiB 7.6 MiB 1 a.extend([1] * 1000000)16 67.3 MiB 0.0 MiB 1 d = a[:30000]17 67.3 MiB 0.0 MiB 1 del d18 # i = 119 74.9 MiB 7.6 MiB 1 a.extend([1] * 1000000)20 74.9 MiB 0.0 MiB 1 d = a[:30000]21 74.9 MiB 0.0 MiB 1 del d22 # i = 223 82.5 MiB 7.6 MiB 1 a.extend([1] * 1000000)24 82.5 MiB 0.0 MiB 1 d = a[:30000]25 82.5 MiB 0.0 MiB 1 del d26 # out of for27 52.0 MiB -30.5 MiB 1 del a28 52.0 MiB 0.0 MiB 1 return True
- 从结果可以发现:
- mem usage指的是循环内部的最大的内存占用值。
- 例如这里,while内部最大是82.5 MiB,所以全部的while内部的行,都被标注为了82.5 MiB
- increment指的是,循环内部,此行语句全部的内存变化的总和。
- 例如a.extend([1] * 1000000)三次执行,每次添加了7.6 MiB的内存,因此在while中显示的是这行语句添加了22.9 MiB的内存。
- mem usage指的是循环内部的最大的内存占用值。
总结:
反正各种问题,实践出真知。完美解释了之前不理解的while内部的情况啊。