Python利用memory_profiler实现内存分析

memory_profiler是第三方模块,用于监视进程的内存消耗以及python程序内存消耗的逐行分析。本文将利用memory_profiler实现内存分析,需要的可以参考一下

任何编程语言开发的项目代码都是需要考虑内存问题的,有时候当项目体量比较庞大以后若是出现内存泄漏等问题分析起来更是哦力不从心的。

因此,平时建议从开发的每个函数入手尽量编写的标准、规范,不至于造成后期无法修复的BUG,这个python非标准模块memory_profiler值得一看。

使用memory_profiler能分析出每行代码块的内存资源使用情况,有两种方式可以参考,一种是开发完代码块通过命令行的方式执行即可。

另一种则在直接代码块时直接生成内r内存资源情况的日志可以随时查看。

使用python pip的方式安装memory_profiler非标准库,默认使用清华大学的python镜像站。

pip install memory_profiler -i https://pypi.tuna.tsinghua.edu.cn/simple/ 

开发一个函数func_while,其中运行一个100万次的循环并且在循环中打印每一次循环执行时的时间戳,将内存使用情况保存到日志文件memory.log中。

# Importing the timeit module. import timeit # A logging library. from loguru import logger # A decorator that will wrap the function and add some code to it. from memory_profiler import profile @profile(precision=4, stream=open("memory.log", "w+")) def func_while():     """     It prints the numbers from 0 to 999999.     """     begin = timeit.default_timer()     logger.info("开始循环应用:{0}".format(begin))     n = 0     while n < 1000000:         logger.info('当前时间戳:{0}'.format(timeit.default_timer()))         n = n + 1     end = timeit.default_timer()     logger.info("结束循环应用:{0}".format(end))     logger.info('循环应用总共用时:{0}'.format(str(end - begin))) func_while() # 2022-09-17 22:18:18.767 | INFO     | __main__:func_while:39 - 当前时间戳:1397.349649192 # 2022-09-17 22:18:18.769 | INFO     | __main__:func_while:39 - 当前时间戳:1397.350927206 # 2022-09-17 22:18:18.770 | INFO     | __main__:func_while:39 - 当前时间戳:1397.352256128 # 2022-09-17 22:18:18.771 | INFO     | __main__:func_while:39 - 当前时间戳:1397.353639651 # 2022-09-17 22:18:18.773 | INFO     | __main__:func_while:39 - 当前时间戳:1397.354919308 # 2022-09-17 22:18:18.774 | INFO     | __main__:func_while:43 - 结束循环应用:1397.35619568 # 2022-09-17 22:18:18.775 | INFO     | __main__:func_while:45 - 循环应用总共用时:1394.6941001149999 

从上面的运行时间可以看出整个100万次的循环整整跑了23分钟才完成,本身电脑性能不是很好为了测试差点就宕机了。下面是memory.log内存分析的文件中的部分截图。

从结果可以发现在我的while循环这一行下面的代码块整个内存显示-65303MB左右,可以看出整个内存消耗出现非常大的问题,怪不得的应用的主线程直接就卡死了。

在上面的分析中,我们选用的内存统计的精度是保留四位小数,也就是@profile注解的precision属性值的设置是4。

接下来使用第二种方式,也就是直接运行查看效果,或者在命令行执行.py的python文件效果是一样的都会展示出内存的消耗情况,但是这种情况可能会出现内存精度缺失的情况。

为了保险起见,这次我还是直接选用1万次循环来进行测试查看效果,循环次数过多怕把我的操作机直接搞崩溃了!

@profile(precision=4) def func_while2():     """     It prints the numbers from 0 to 9999.     """     begin = timeit.default_timer()     logger.info("开始循环应用:{0}".format(begin))     n = 0     while n < 10000:         logger.info('当前时间戳:{0}'.format(timeit.default_timer()))         n = n + 1     end = timeit.default_timer()     logger.info("结束循环应用:{0}".format(end))     logger.info('循环应用总共用时:{0}'.format(str(end - begin))) func_while2() # 2022-09-17 22:37:38.086 | INFO     | __main__:func_while2:81 - 当前时间戳:15.020861643 # 2022-09-17 22:37:38.087 | INFO     | __main__:func_while2:85 - 结束循环应用:15.022343696 # 2022-09-17 22:37:38.089 | INFO     | __main__:func_while2:87 - 循环应用总共用时:12.908313867 # Filename: C:/the-public/the-public/test013/test7.py # # Line #    Mem usage    Increment  Occurrences   Line Contents # ============================================================= #     73  29.7266 MiB  29.7266 MiB           1   @profile(precision=4) #     74                                         def func_while2(): #     75  29.7266 MiB   0.0000 MiB           1       begin = timeit.default_timer() #     76  29.7422 MiB   0.0156 MiB           1       logger.info("开始循环应用:{0}".format(begin)) #     77 #     78  29.7422 MiB   0.0000 MiB           1       n = 0 #     79 #     80  29.8125 MiB   0.0000 MiB       10001       while n < 10000: #     81  29.8125 MiB   0.0703 MiB       10000           logger.info('当前时间戳:{0}'.format(timeit.default_timer())) #     82  29.8125 MiB   0.0000 MiB       10000           n = n + 1 #     83 #     84  29.8125 MiB   0.0000 MiB           1       end = timeit.default_timer() #     85  29.8125 MiB   0.0000 MiB           1       logger.info("结束循环应用:{0}".format(end)) #     86 #     87  29.8125 MiB   0.0000 MiB           1       logger.info('循环应用总共用时:{0}'.format(str(end - begin))) 

显然执行1万次循环结果算是正常的,增量只有0.0703 MiB,只用了13秒就执行完成了,可能使用for循环的话效果还要好一些。

到此这篇关于Python利用memory_profiler实现内存分析的两种方法总结的文章就介绍到这了,更多相关Python memory_profiler内存分析内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是Python利用memory_profiler实现内存分析的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python