栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pythonmemoryerror鐨勬椂鍊欐墿澶ц櫄鎷熷唴瀛樻湁鐢ㄥ悧(pythonmemoryerror)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pythonmemoryerror鐨勬椂鍊欐墿澶ц櫄鎷熷唴瀛樻湁鐢ㄥ悧(pythonmemoryerror)

python: memory_profiler包,记录代码内存使用情况,查找内存泄漏原因

前言安装简单使用记录时间-内存的使用情况

前言

最近用paddlepaddle,发现训练每个epoch时,内存都会增加几百兆,这明显就是出现了内存泄漏。python中内存泄露的原因比C++(一般就是动态内存分配)更加隐蔽,可以借助memory_profiler这个包监控python代码的内存使用。

安装

直接pip安装就完事了

pip install memory_profiler 
简单使用

只需要在.py文件中导入memory_profiler包中的profiler方法,然后作为某个函数的装饰器,就能够记录该函数内部代码的内存使用情况了:

# learn_mprof.py
from memory_profiler import profile
import numpy as np

@profile
def count():
    print('+++++++++++')
    a = 0;
    b = 1;
    c = a + b
    print('a+b=c')
    print('{}+{}={}'.format(a, b, c))
    print('+++++++++++')
    arr = np.ones([1000, 1000])
    print(arr.shape)
    print('+++++++++++')

if __name__ == '__main__':
    count()

然后运行:

python learn_mprof.py

输出结果:

+++++++++++
a+b=c
0+1=1
+++++++++++
(1000, 1000)
+++++++++++
Filename: learn_mprof.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
     4     53.4 MiB     53.4 MiB           1   @profile
     5                                         def count():
     6     53.4 MiB      0.0 MiB           1       print('+++++++++++')
     7     53.4 MiB      0.0 MiB           1       a = 0;
     8     53.4 MiB      0.0 MiB           1       b = 1;
     9     53.4 MiB      0.0 MiB           1       c = a + b
    10     53.4 MiB      0.0 MiB           1       print('a+b=c')
    11     53.4 MiB      0.0 MiB           1       print('{}+{}={}'.format(a, b, c))
    12     53.4 MiB      0.0 MiB           1       print('+++++++++++')
    13     61.1 MiB      7.7 MiB           1       arr = np.ones([1000, 1000])
    14     61.1 MiB      0.0 MiB           1       print(arr.shape)
    15     61.1 MiB      0.0 MiB           1       print('+++++++++++')

Mem usage表示当前内存使用,Increment表示该行代码的内存增量,Occurences表示该行代码运行了几次。

如果要找内存泄漏,直接看Increment就OK了;如果想找内存瓶颈,就看Mem usage。

记录时间-内存的使用情况

如果想记录.py文件运行时,内存使用随时间的变化,也很简单,直接运行:

mprof run learn_mprof.py

输出结果:

mprof: Sampling memory every 0.1s
running new process
running as a Python program...
+++++++++++
a+b=c
0+1=1
+++++++++++
(1000, 1000)
+++++++++++
Filename: learn_mprof.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
     4     53.6 MiB     53.6 MiB           1   @profile
     5                                         def count():
     6     53.6 MiB      0.0 MiB           1       print('+++++++++++')
     7     53.6 MiB      0.0 MiB           1       a = 0;
     8     53.6 MiB      0.0 MiB           1       b = 1;
     9     53.6 MiB      0.0 MiB           1       c = a + b
    10     53.6 MiB      0.0 MiB           1       print('a+b=c')
    11     53.6 MiB      0.0 MiB           1       print('{}+{}={}'.format(a, b, c))
    12     53.6 MiB      0.0 MiB           1       print('+++++++++++')
    13     61.3 MiB      7.7 MiB           1       arr = np.ones([1000, 1000])
    14     61.3 MiB      0.0 MiB           1       print(arr.shape)
    15     61.3 MiB      0.0 MiB           1       print('+++++++++++')

此时,在运行目录中会多出一个mprofile_xxxxxxxxxxxx.dat文件,可以通过这个文件画出时间-内存图:

mprof plot mprofile_xxxxxxxxxxxxxxxx.dat

效果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/772523.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号