zoukankan      html  css  js  c++  java
  • python 内存分析 流程图

    memory_profiler 模块被用于在逐行的基础上,测量你代码的内存使用率,也建议安装 psutil 包,使得 memory_profile 模块运行的更快

    from memory_profiler import profile
     
     
    @profile(precision=6)
    def primes(n):
        if n == 2:
            return [2]
        elif n < 2:
            return []
        s = range(3, n + 1, 2)
        mroot = n ** 0.5
        half = (n + 1) / 2 - 1
        i = 0
        m = 3
        while m <= mroot:
            if s[i]:
                j = (m * m - 3) / 2
                s[j] = 0
                while j < half:
                    s[j] = 0
                    j += m
            i = i + 1
            m = 2 * i + 3
        return [2] + [x for x in s if x]
     
     
    len(primes(100000))

    meliae会把某个时刻的内存给dump到一个文件中,然后再对该文件进行分析,当我们的某个python程序占用内存很大,可能有内存泄露发生时,可以使用该工具来进行检测分析

    Guppy (使用了Heapy):使用 guppy 包,你可以跟踪每个类型在你代码中每个阶段(字符, 元组, 字典 等等)有多少对象被创建了,查看占用内存前十位变量的工具

    objgraph模块:该工具允许你查看内存中对象的数量,定位含有该对象的引用的所有代码的位置。

    .定位哪个对象引起内存泄漏
    # -*- coding: utf-8 -*-
    import objgraph
    
    _cache = []
    
    class OBJ(object):
        pass
    
    def func_to_leak():
        o = OBJ()
        _cache.append(o)
        # do something with o, then remove it from _cache 
     
        if True: # this seem ugly, but it always exists
           return 
           
        _cache.remove(o)
     
    if __name__ == '__main__':
       objgraph.show_growth()
        try:
           func_to_leak()
        except:
           pass
        print '********after call func_to_leak*********'
       objgraph.show_growth()
        
    运行结果(我们只关心后一次 show_growth 的结果)如下
    
    wrapper_descriptor           1196    +1196
    function                   1187    +1187
    builtin_function_or_method     739     +739
    method_descriptor            635     +635
    dict                        539     +539
    weakref                     457     +457
    tuple                       364     +364
    getset_descriptor            306     +306
    list                        232     +232
    member_descriptor            227     +227
    ********after call func_to_leak*********
    wrapper_descriptor    1205       +9
    getset_descriptor      310       +4
    member_descriptor      230       +3
    weakref              459       +2
    dict                 541       +2
    OBJ                   1      +1  
    
    从运行结果可以发现,调用函数之后,增加了一个类 OBJ 的实例,然而理论上函数调用结束之后,所有在函数作用域(local)中声明的对象都改被销毁,因此这里就存在内存泄露。
    • 显示占据python程序内存的头N个对象
    • 显示一段时间以后哪些对象被删除活增加了
    • 在我们的脚本中显示某个给定对象的所有引用
     

    ============快速生成陌生代码的函数调用关系

    1、graphviz(Graph Visualization Software-图形可视化软件)

    2、pycallgraph(创建python的可视化图形)

  • 相关阅读:
    apache https部署
    库位码排序优化
    mybatis + easy excel 导出百万级数据仅需要1g内存
    解决springboot打成jar包后, 无法获取(classpath)类路径下的自定义配置文件
    好用的 easyExcel 工具类
    数据结构与算法(相关名词)
    自动填充javabean属性,借助json序列化工具方便生成参数请求体
    用于避免bean对象连点方法调用报空指针异常,排查困难。
    基于FastJson封装的工具类
    itexpdf 工具类
  • 原文地址:https://www.cnblogs.com/testzcy/p/12665878.html
Copyright © 2011-2022 走看看