【问题标题】:Detecting memory leaks & dumping statistics in python [duplicate]在python中检测内存泄漏和转储统计信息[重复]
【发布时间】:2026-01-10 20:40:01
【问题描述】:

我在寻找python内存调试技术? 基本上我正在寻找可用于 python 的工具,看看当 python 进程占用大量内存时我们可以查看哪些数据? 我的目标是隔离这种内存消耗过程和转储统计数据

【问题讨论】:

    标签: python memory-leaks


    【解决方案1】:

    看看guppy

    from guppy import hpy
    import networkx as nx
    
    h = hpy()
    L=[1,2,3]
    
    h.heap()
    
    
    
    > Partition of a set of 89849 objects. Total size = 12530016 bytes.
    >  Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
    >     0  40337  45  3638400  29   3638400  29 str
    >     1  21681  24  1874216  15   5512616  44 tuple
    >     2   1435   2  1262344  10   6774960  54 dict (no owner)
    

    这将大大有助于告诉您哪些地方正在使用大量内存。

    当一个对象不再可访问时,Python 会清除内存——程序中没有任何东西指向它。有时你可能想知道到底是什么仍然指向一个特定的对象。为此,您可能会查看this question,它告诉您如何找出仍然指代对象的事物。

    【讨论】: