代码之家  ›  专栏  ›  技术社区  ›  Fragsworth

Python内存泄漏[关闭]

  •  237
  • Fragsworth  · 技术社区  · 17 年前

    9 回复  |  直到 17 年前
        1
  •  88
  •   0 _ Edward Ned Harvey    9 年前

    看看这篇文章: Tracing python memory leaks

    此外,请注意 garbage collection module set_debug 功能。另外,看看 this code by Gnibbler

        2
  •  71
  •   linqu    10 年前

    我尝试了前面提到的大多数选项,但发现这个小巧直观的软件包是最好的: pympler

    跟踪未被垃圾回收的对象非常简单,请查看以下小示例:

    通过安装软件包 pip install pympler

    from pympler.tracker import SummaryTracker
    tracker = SummaryTracker()
    
    # ... some code you want to investigate ...
    
    tracker.print_diff()
    

    输出显示了所有已添加的对象,以及它们消耗的内存。

    样本输出:

                                     types |   # objects |   total size
    ====================================== | =========== | ============
                                      list |        1095 |    160.78 KB
                                       str |        1093 |     66.33 KB
                                       int |         120 |      2.81 KB
                                      dict |           3 |       840 B
          frame (codename: create_summary) |           1 |       560 B
              frame (codename: print_diff) |           1 |       480 B
    

    此软件包提供了许多其他功能。检查 pympler's documentation Identifying memory leaks .

        3
  •  22
  •   Denis Ryzhkov    12 年前

    mem_top 我创建的工具

    它帮助我解决了一个类似的问题

        4
  •  12
  •   Antony Hatchkins Alexander Hamilton    12 年前

    Tracemalloc module a third-party library (不过还没有测试过)。

    我建议您结合使用tracemalloc pyrasite top 10 snippet 在一个 pyrasite-shell 将为您提供足够的信息和提示,以便在10分钟内修复泄漏。然而,如果你仍然无法找到泄漏原因,pyrasite shell结合本线程中提到的其他工具可能会给你更多的提示。您还应该查看pyrasite提供的所有额外帮助程序(如内存查看器)。

        5
  •  9
  •   user1527491    9 年前

    您应该特别查看您的全局或静态数据(长期数据)。

    垃圾收集器只能收集不再被引用的数据。但是,静态数据可以连接应该释放的数据元素。

    另一个问题可能是内存周期,但至少在理论上,垃圾回收器应该找到并消除周期——至少只要它们不依赖于一些长期存在的数据。

    哪些类型的长寿命数据特别麻烦?好好看看任何列表和字典——它们可以无限增长。在字典中,你甚至可能没有看到问题的出现,因为当你访问字典时,字典中的关键字数量可能对你来说不是很明显。..

        6
  •  7
  •   logix    9 年前

    要检测和定位长时间运行的进程的内存泄漏,例如在生产环境中,您现在可以使用 stackimpact tracemalloc this post .

    enter image description here

        7
  •  4
  •   martiert    17 年前

    就最佳实践而言,请注意递归函数。在我的例子中,我遇到了递归问题(不需要递归)。我正在做的一个简化示例:

    def my_function():
        # lots of memory intensive operations
        # like operating on images or huge dictionaries and lists
        .....
        my_flag = True
        if my_flag:  # restart the function if a certain flag is true
            my_function()
    
    def main():
        my_function()
    

    以这种递归方式操作不会触发垃圾回收并清除函数的剩余部分,因此每次内存使用都在不断增长。

    def my_function():
        # lots of memory intensive operations
        # like operating on images or huge dictionaries and lists
        .....
        my_flag = True
        .....
        return my_flag
    
    def main():
        result = my_function()
        if result:
            my_function()
    
        8
  •  3
  •   Dmitry Rubanovich    14 年前

    不确定python中内存泄漏的“最佳实践”,但python应该通过垃圾收集器清除自己的内存。所以主要我会从检查一些短的循环列表开始,因为它们不会被垃圾回收器回收。

        9
  •  3
  •   The4thIceman    9 年前