python library memory_profiler

python library memory_profiler

  1. 模块说明

    1
    memory_profiler是监控python进程工具,它可以分析出每一行代码所增减的内存状况
  2. 安装

    1
    [root@dev test]# pip install memory_profiler psutil
  3. 示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    [root@dev test]# cat del.py
    import time

    @profile
    def my_func():
    a = [1] * (10 ** 6)
    b = [2] * (2 * 10 ** 7)
    time.sleep(10)
    del b
    del a
    print "+++++++++"

    if __name__ == '__main__':
    my_func()


    [root@dev test]# python -m memory_profiler del.py
    +++++++++
    Filename: del.py

    Line # Mem usage Increment Line Contents
    ================================================
    3 11.285 MiB 0.000 MiB @profile
    4 def my_func():
    5 18.930 MiB 7.645 MiB a = [1] * (10 ** 6)
    6 171.520 MiB 152.590 MiB b = [2] * (2 * 10 ** 7)
    7 171.531 MiB 0.012 MiB time.sleep(10)
    8 18.941 MiB -152.590 MiB del b
    9 11.309 MiB -7.633 MiB del a
    10 11.312 MiB 0.004 MiB print "+++++++++"
  4. 对象不删除,直接赋值,内存是否会继续增长

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    方式一:
    [root@dev test]# cat aaa.py
    @profile
    def my_func():
    a = 'a' * 1024 * 1024 * 1024;
    a = 'a' * 1024 * 1024
    a = 'a' * 1024
    del a
    print "+++++++++"

    if __name__ == '__main__':
    my_func()

    [root@dev test]# python -m memory_profiler aaa.py
    +++++++++
    Filename: aaa.py

    Line # Mem usage Increment Line Contents
    ================================================
    1 11.281 MiB 0.000 MiB @profile
    2 def my_func():
    3 1035.297 MiB 1024.016 MiB a = 'a' * 1024 * 1024 * 1024;
    4 12.285 MiB -1023.012 MiB a = 'a' * 1024 * 1024
    5 12.285 MiB 0.000 MiB a = 'a' * 1024
    6 12.285 MiB 0.000 MiB del a
    7 12.289 MiB 0.004 MiB print "+++++++++"


    方式二:
    [root@dev test]# cat bbb.py
    @profile
    def my_func():
    a = 'a' * 1024 * 1024 * 1024;
    del a
    a = 'a' * 1024 * 1024
    del a
    a = 'a' * 1024
    del a
    print "+++++++++"

    if __name__ == '__main__':
    my_func()
    [root@dev test]# python -m memory_profiler bbb.py
    +++++++++
    Filename: bbb.py

    Line # Mem usage Increment Line Contents
    ================================================
    1 11.277 MiB 0.000 MiB @profile
    2 def my_func():
    3 1035.293 MiB 1024.016 MiB a = 'a' * 1024 * 1024 * 1024;
    4 11.289 MiB -1024.004 MiB del a
    5 12.285 MiB 0.996 MiB a = 'a' * 1024 * 1024
    6 12.285 MiB 0.000 MiB del a
    7 12.285 MiB 0.000 MiB a = 'a' * 1024
    8 12.285 MiB 0.000 MiB del a
    9 12.289 MiB 0.004 MiB print "+++++++++"

    综上,是否del对象没有影响,新赋的值会替代旧的值
  5. 对象赋值是否会增加同样的内存

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    方式一:
    [root@dev test]# cat ccc.py
    @profile
    def my_func():
    a = 'a' * 1024 * 1024 * 1024;
    b = a
    del a
    print "+++++++++"

    if __name__ == '__main__':
    my_func()

    [root@dev test]# python -m memory_profiler ccc.py
    +++++++++
    Filename: ccc.py

    Line # Mem usage Increment Line Contents
    ================================================
    1 11.285 MiB 0.000 MiB @profile
    2 def my_func():
    3 1035.301 MiB 1024.016 MiB a = 'a' * 1024 * 1024 * 1024;
    4 1035.301 MiB 0.000 MiB b = a
    5 1035.301 MiB 0.000 MiB del a
    6 1035.305 MiB 0.004 MiB print "+++++++++"

    方式二:
    [root@dev test]# python -m memory_profiler ddd.py
    +++++++++
    Filename: ddd.py

    Line # Mem usage Increment Line Contents
    ================================================
    1 11.281 MiB 0.000 MiB @profile
    2 def my_func():
    3 1035.297 MiB 1024.016 MiB a = 'a' * 1024 * 1024 * 1024;
    4 1035.297 MiB 0.000 MiB b = a
    5 1035.297 MiB 0.000 MiB del a
    6 11.293 MiB -1024.004 MiB del b
    7 11.297 MiB 0.004 MiB print "+++++++++"

    综上,把a赋值给b,内存没有增加.但是只删除其中一个对象的时候,内存不会减