Python利用memory_profiler查看内存占用情况

 更新时间:2022年06月28日 08:40:23   作者:玩转测试开发  
memory_profiler是第三方模块,用于监视进程的内存消耗以及python程序内存消耗的逐行分析。本文将利用memory_profiler查看代码运行占用内存情况,感兴趣的可以了解一下

简介

memory_profiler是第三方模块,用于监视进程的内存消耗以及python程序内存消耗的逐行分析。它是一个纯python模块,依赖于psutil模块。

安装

pip install memory_profiler

使用方法

1、通过装饰器运行

@profile
def func1():

2、通过命令行运行

python -m memory_profiler test_code.py

案例源码:

# -*- coding: utf-8 -*-
# time: 2022/6/11 21:17
# file: test_code.py
# 公众号: 玩转测试开发
from memory_profiler import profile

loop = 50000


@profile
def func1():
    s1 = [i for i in range(loop)]
    s2 = []
    for i in range(loop):
        if i & 1 == 1:
            s2.append(i)
    result = sum(s1) + sum(s2)
    del s1
    del s2
    return result


if __name__ == '__main__':
    result = func1()
    print(result)

方法1运行结果:

方法2运行结果:

补充

下面小编为大家整理了一下memory_profiler的一些使用

1、直接打印结果到终端上

#coding:utf8 
from memory_profiler import profile 
 
@profile 
def test1(): 
    c=list() 
    for item in range(10000): 
        c.append(item) 
 
 
if __name__=='__main__': 
    test1() 

结果如下

Filename: D:/python/test_sip/test_check_es.py 
 
Line #    Mem usage    Increment   Line Contents 
================================================ 
   474     16.6 MiB     16.6 MiB   @profile 
   475                             def test1(): 
   476     16.6 MiB      0.0 MiB       c=list() 
   477     17.0 MiB      0.0 MiB       for item in range(10000): 
   478     17.0 MiB      0.1 MiB           c.append(item) 

2、定义输出到文件,定义结果保留的小数位

#coding:utf8 
from memory_profiler import profile 
 
@profile(precision=4,stream=open('memory_profiler.log','w+')) 
def test1(): 
    c=list() 
    for item in range(10000): 
        c.append(item) 
 
 
if __name__=='__main__': 
    test1() 

结果如下

Filename: D:/python/test_sip/test_check_es.py 
 
Line #    Mem usage    Increment   Line Contents 
================================================ 
   474  16.5391 MiB  16.5391 MiB   @profile(precision=4,stream=open('memory_profiler.log','w+')) 
   475                             def test1(): 
   476  16.5430 MiB   0.0039 MiB       c=list() 
   477  16.8906 MiB   0.0039 MiB       for item in range(10000): 
   478  16.8906 MiB   0.0391 MiB           c.append(item) 

到此这篇关于Python利用memory_profiler查看内存占用情况的文章就介绍到这了,更多相关Python memory_profiler查看内存占用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python PyQt拖动控件对齐到网格的方法步骤

    Python PyQt拖动控件对齐到网格的方法步骤

    pyqt是一个用于创建GUI应用程序的跨平台工具包,它将python与qt库融为一体,下面这篇文章主要给大家介绍了关于Python PyQt拖动控件对齐到网格的方法步骤,需要的朋友可以参考下
    2022-12-12
  • python plotly绘制直方图实例详解

    python plotly绘制直方图实例详解

    这篇文章主要介绍了python plotly绘制直方图实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 使用tensorboard可视化loss和acc的实例

    使用tensorboard可视化loss和acc的实例

    今天小编就为大家分享一篇使用tensorboard可视化loss和acc的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • python输出带颜色字体实例方法

    python输出带颜色字体实例方法

    在本篇文章里小编给大家整理了关于python输出带颜色字体实例以及相关代码,有需要的朋友们可以学习参考下。
    2019-09-09
  • python七种方法判断字符串是否包含子串

    python七种方法判断字符串是否包含子串

    这篇文章主要介绍了python七种方法判断字符串是否包含子串,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2020-08-08
  • python Matplotlib模块的使用

    python Matplotlib模块的使用

    这篇文章主要介绍了python Matplotlib模块的使用,帮助大家更好的利用python处理图像,感兴趣的朋友可以了解下
    2020-09-09
  • Python进程崩溃AttributeError异常问题解决

    Python进程崩溃AttributeError异常问题解决

    这篇文章主要介绍了Python进程崩溃(AttributeError异常)问题解决,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下方法
    2023-06-06
  • 使用opencv识别图像红色区域,并输出红色区域中心点坐标

    使用opencv识别图像红色区域,并输出红色区域中心点坐标

    这篇文章主要介绍了使用opencv识别图像红色区域,并输出红色区域中心点坐标,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • 详解PyTorch nn.Embedding() 嵌入

    详解PyTorch nn.Embedding() 嵌入

    在自然语言处理(NLP)中,将文本序列转化为数字序列(tokenid)后,为了使模型能更好地理解这些数字背后的含义,引入了嵌入层(Embedding)通过简单的示例,可以看出Embedding的获取过程及其在理解语言中的关键作用
    2024-11-11
  • python在windows命令行下输出彩色文字的方法

    python在windows命令行下输出彩色文字的方法

    这篇文章主要介绍了python在windows命令行下输出彩色文字的方法,涉及Python文字特效操作技巧,需要的朋友可以参考下
    2015-03-03

最新评论