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

作者:玩转测试开发 时间:2022-05-24 08:55:29 

简介

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运行结果:

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

方法2运行结果:

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

补充

下面小编为大家整理了一下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) 

来源:https://blog.csdn.net/hzblucky1314/article/details/125494000

标签:Python,memory,profiler,内存,占用
0
投稿

猜你喜欢

  • gchart:基于google图表API的jquery组件全攻略:1、入门

    2010-01-25 12:18:00
  • 基于python实现图片转字符画代码实例

    2023-05-17 01:53:37
  • 服务器响应HTTP的类型ContentType大全

    2007-10-23 10:21:00
  • Python编程实现控制cmd命令行显示颜色的方法示例

    2023-07-24 05:39:45
  • textarea的输入限制统计代码statInput

    2008-05-22 13:36:00
  • python密码学对称和非对称密码教程

    2023-06-19 15:56:59
  • Python Prim算法通过遍历墙实现迷宫的生成

    2022-06-26 08:41:09
  • ASP中RegExp对象正则表达式语法及相关例子

    2007-08-12 17:46:00
  • python同时给两个收件人发送邮件的方法

    2021-10-23 07:31:36
  • python通过pillow识别动态验证码的示例代码

    2023-08-27 02:29:21
  • php SQL防注入代码集合

    2023-11-18 03:58:34
  • 天极网页版式设计的思考

    2008-01-18 12:44:00
  • python pandas 对series和dataframe的重置索引reindex方法

    2023-08-25 08:10:57
  • python自动截取需要区域,进行图像识别的方法

    2021-05-14 11:04:42
  • python3下pygame如何实现显示中文

    2021-01-15 00:57:04
  • php字符串截取函数用法分析

    2023-06-28 22:19:26
  • 浅谈pytorch中的nn.Sequential(*net[3: 5])是啥意思

    2023-10-05 05:42:55
  • python实现二级登陆菜单及安装过程

    2023-09-16 16:05:44
  • python使用matplotlib:subplot绘制多个子图的示例

    2021-01-26 18:13:08
  • PHP用mysql数据库存储session的代码

    2023-09-05 00:44:50
  • asp之家 网络编程 m.aspxhome.com