Python guppy/heapy

2024 年 8 月 29 日 | 阅读 26 分钟

guppy3 是一个 Python 包,它向用户提供当前堆状态和堆中可用的对象。此详细信息可以支持开发人员进行内存分析和分析。guppy 包包含一个名为 heapy 的子包。heapy 子包提供了一系列重要方法和当前堆状态。当请求堆状态时,guppy3 包提供一个特殊的 C-nodesets 对象。此 C-nodesets 对象包含内存中可用每个对象的堆状态。guppy3 包还包含与堆中所有可达或不可达对象相关的详细信息,作为一种特殊类型的列表。它还提供与对象数量、对象占用的内存百分比、以字节为单位的大小和类型信息相关的详细信息。除此之外,guppy3 包提供了一系列有用的方法,允许我们访问堆状态的单个元素、查找堆状态之间的差异、查找以字节为单位的对象大小以及许多其他功能。在本教程中,我们将通过不同的示例了解 guppy3 包的用法,以收集与 Python 内存使用相关的​​信息。

那么,让我们开始吧。

了解 guppy3 包

guppy3 包是一个 Python 编程环境和堆分析工具集。此包包含以下子包

  1. gsl
  2. heapy
  3. 集合

以上子包的描述如下

1. 等 - 此子包支持包含 Glue 协议模块的模块。

2. gsl - 此子包包含 Guppy 规范语言的实现。此子包可用于从通用源创建文档和测试。

3. heapy - 此子包支持堆分析工具集。它可用于查找与堆中对象相关的详细信息并以不同方式显示详细信息。

4. 集合 - 此子包支持用 C 语言实现的 Bitsets 和 'nodesets'。

guppy3 包是 Guppy-PE 的一个分支,由 Sverker Nilsson 为 Python 2 创建。

guppy3 包的要求

我们应该有 Python 3.6、3.7、3.8 或 3.9 版本。guppy3 包仅适用于 CPython;因此,不支持 PyPy 和其他 Python 实现。我们可以从 Sverker Nilsson 的 guppy-pe 获取对 Python 2 的支持,此包就是从它派生出来的。

我们可以借助 Tkinter 库使用此包的图形浏览器。此外,为了利用其远程监控功能,还需要线程。

如何安装 guppy3 包?

我们可以通过 pip 安装程序或使用 conda 安装 guppy3 包。两种方法如下所示

使用 pip 安装

使用 conda 安装

验证安装

模块安装完成后,我们可以通过创建一个空的 Python 程序文件并写入如下 import 语句来验证它:

文件:verify.py

现在,保存上述文件并在终端中使用以下命令执行它:

语法

如果上述 Python 程序文件未返回任何错误,则模块已正确安装。但是,如果引发异常,请尝试重新安装模块,并且还建议参考模块的官方文档。

了解 guppy3 包的一些方法和属性

下表包含我们可以通过 guppy3 包使用的​​方法和属性

序号。方法和属性描述
1guppy.hpy()guppy 模块有一个名为 hpy() 的方法,它提供对 heapy 对象的访问。heapy 对象还包含用于访问堆状态和执行其他操作的通用接口。
2heap()此方法提供了可从堆根访问的可达和可见对象列表。我们可以通过调用 hpy() 方法创建的对象来访问此方法。返回的输出以表格形式显示。请注意,它不包括作为 guppy 一部分使用的对象。
3setref()此方法也通过调用 hpy() 方法创建的对象访问。它用于设置一个参考点,此参考点之后创建的所有对象都将在下次调用 heap 方法获取堆状态时可用。它不会包含堆中所有对象;但是,只有在参考点之后创建的对象。
4heapu()我们可以通过调用 guppy 模块的 hpy() 方法创建的对象来访问此方法。此方法向我们提供与堆根不可达的对象列表的堆状态。heapu() 方法返回的对象是一个状态对象 (guppy.heapy.Part.Stat),它包含有关不可达对象的统计信息。
5diff()此方法作为堆状态对象的一部分可用,它接受另一个堆状态作为输入,以找出两个状态之间的对象差异。它返回 guppy.heapy.Part.Stat 类型的对象。
6disjoint()此方法可通过堆状态对象访问,它接受另一个堆状态的输入,并根据两个堆状态是否不相交返回 True/False。
7count堆状态对象的此属性用于返回一个整数,表示接受状态时可用的对象总数。
8大小堆状态对象的此属性用于返回一个整数,表示接受状态时堆的总大小。
9referents此属性用于返回另一个堆状态对象,其中只有其他对象引用的对象的条目。
10referrers此属性用于返回另一个堆状态对象,其中只有引用其他对象的对象的条目。
11stat此属性用于返回 guppy.heapy.Part.Stat 对象,可以对其进行循环以获取与堆状态的单个条目相关的详细信息。
12dump()此方法接受文件名作为输入,堆状态将转储到该文件。
13load()此方法接受文件名作为输入,堆状态将从该文件加载。它可以通过调用 hpy() 方法创建的对象进行调用。它用于返回文件中存在的堆状态的 guppy.heapy.Part.Stat 对象。
14bytype堆状态对象的此属性按对象的类型对堆状态条目进行分组。所有字典条目将被合并为一个条目。
15byrcs堆状态对象的此属性按引用者的类型对堆状态条目进行分组。
16bymodule堆状态对象的此属性按模块对堆状态条目进行分组。
17bysize堆状态对象的此属性按对象的单个大小对堆状态条目进行分组。
18byunity堆状态对象的此属性按总大小对堆状态条目进行分组。
19byvia堆状态对象的此属性按它们被引用的对象对堆状态条目进行分组。
20byidset堆状态对象的此属性按 idset 对堆状态条目进行分组。
21byid堆状态对象的此属性按内存地址对堆状态条目进行分组。
22iso()此方法接受单个对象或多个对象作为输入,并返回表示对象大小的状态。它将列表和字典等复杂对象视为一个对象。
23idset()此方法接受单个或多个对象作为输入,并返回表示对象大小的状态。它将复杂对象(例如列表字典)视为单个对象的列表。

现在让我们考虑一些示例,以了解上述 guppy3 包的方法和属性在 Python 中分析内存使用情况的用法。

基于 Python guppy3 包的一些示例

示例 1

在以下示例中,我们将了解使用 guppy 模块的 guppy.hpy()、heap()setref() 方法访问堆状态的方法。

代码

输出

Heap Status At Starting : 
Heap Size :  12023800  bytes

Partition of a set of 85790 objects. Total size = 12023800 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  25003  29  4135160  34   4135160  34 str
     1  18832  22  1346584  11   5481744  46 tuple
     2   6269   7  1107919   9   6589663  55 types.CodeType
     3  12133  14   916798   8   7506461  62 bytes
     4   5748   7   781728   7   8288189  69 function
     5    863   1   734088   6   9022277  75 type
     6    244   0   494712   4   9516989  79 dict of module
     7    863   1   461784   4   9978773  83 dict of type
     8   1319   2   415880   3  10394653  86 dict (no owner)
     9    150   0   219792   2  10614445  88 set
<282 more rows. Type e.g. '_.more' to view.>

Heap Status After Setting Reference Point :
Heap Size :  616  bytes

Partition of a set of 3 objects. Total size = 616 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      1  33      408  66       408  66 types.FrameType
     1      1  33      136  22       544  88 function
     2      1  33       72  12       616 100 builtins.weakref

Heap Status After Creating Few Objects :
Heap Size :  56632  bytes

Partition of a set of 1333 objects. Total size = 56632 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0   1328 100    37184  66     37184  66 int
     1      1   0    12728  22     49912  88 list
     2      1   0     6104  11     56016  99 numpy.ndarray
     3      1   0      408   1     56424 100 types.FrameType
     4      1   0      136   0     56560 100 function
     5      1   0       72   0     56632 100 builtins.weakref

Memory Usage After Creation Of Objects :  56016  bytes

说明

在上述代码片段中,我们解释了 guppy.hpy()、heap()setref() 方法的用法。我们首先在脚本开始时收集了堆状态。然后我们设置了参考点并再次检索了堆状态。我们创建了一些对象,例如列表、字符串和随机数的 NumPy 数组。完成这些对象的创建后,我们再次调用 heap() 方法以获取包含这些对象详细信息的堆状态。

上面显示的输出表明它包含有关对象数量和整个堆的总大小、对象计数、大小、该对象类型占用的内存百分比以及类型信息的详细信息。第二个堆状态没有显示太多信息,因为设置参考点后没有发生太多事情。第三个堆状态对象包含有关设置参考点后创建的对象的​​信息。

示例 2

在以下示例中,我们将了解使用 heapu() 方法访问从堆根不可达对象的方法。

代码

输出

GC Collectable Objects Which Are not Reachable from Root of Heap
Total Objects :  587
Total Size :  82948  Bytes
Number of Entries :  17
Entries : 
Index Count  Size  Cumulative Size         Object Name
    0   56   29088   29088                          dict
    1   46   18768   47856                          type
    2  130    9360   57216   types.WrapperDescriptorType
    3   92    4888   62104                         tuple
    4   65    4160   66264    types.MemberDescriptorType
    5    3    4072   70336                          list
    6   93    3906   74242                         bytes
    7   52    3744   77986    types.MethodDescriptorType
    8   16    2610   80596                           str
    9   21    1512   82108       types.BuiltinMethodType
   10    6     384   82492    types.GetSetDescriptorType
   11    1     160   82652                     sys.flags
   12    2     144   82796types.ClassMethodDescriptorType
   13    1      64   82860              types.MethodType
   14    1      48   82908encodings.utf_8.IncrementalDecoder
   15    1      24   82932        builtins.stderrprinter
   16    1      16   82948                 Token.MISSING

First 5 Entries :
Index Count  Size  Cumulative Size         Object Name
    0   56   29088   29088                          dict
    1   46   18768   47856                          type
    2  130    9360   57216   types.WrapperDescriptorType
    3   92    4888   62104                         tuple
    4   65    4160   66264    types.MemberDescriptorType

Directly Printing Results Without Iteration
Partition of a set of 725 objects. Total size = 99179 bytes.
 Index  Count   %     Size   % Cumulative  % Type
     0     66   9    34496  35     34496  35 dict
     1     54   7    22032  22     56528  57 type
     2    147  20    10584  11     67112  68 types.WrapperDescriptorType
     3    113  16     6064   6     73176  74 tuple
     4     74  10     5328   5     78504  79 types.MethodDescriptorType
     5     66   9     4224   4     82728  83 types.MemberDescriptorType
     6      3   0     4072   4     86800  88 list
     7     93  13     3906   4     90706  91 bytes
     8     22   3     3089   3     93795  95 str
     9     40   6     2880   3     96675  97 types.BuiltinMethodType
<39 more rows. Type e.g. '_.more' to view.>

Measuring Unreachable Objects From This Reference Point Onwards
Partition of a set of 725 objects. Total size = 99179 bytes.
 Index  Count   %     Size   % Cumulative  % Type
     0     66   9    34496  35     34496  35 dict
     1     54   7    22032  22     56528  57 type
     2    147  20    10584  11     67112  68 types.WrapperDescriptorType
     3    113  16     6064   6     73176  74 tuple
     4     74  10     5328   5     78504  79 types.MethodDescriptorType
     5     66   9     4224   4     82728  83 types.MemberDescriptorType
     6      3   0     4072   4     86800  88 list
     7     93  13     3906   4     90706  91 bytes
     8     22   3     3089   3     93795  95 str
     9     40   6     2880   3     96675  97 types.BuiltinMethodType
<39 more rows. Type e.g. '_.more' to view.>

说明

在上述代码片段中,我们首先从堆中检索了一组不可达对象,然后利用不同的方法访问单个状态行以检索与单个数据类型相关的​​信息。

示例 3

在以下示例中,我们将了解从整个堆状态对象中检索单个条目的方法。

代码

输出

Heap Status At Starting : 
Partition of a set of 87291 objects. Total size = 21079156 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0     69   0  9007150  43   9007150  43 numpy.ndarray
     1  25008  29  4135434  20  13142584  62 str
     2  18834  22  1346760   6  14489344  69 tuple
     3   6270   7  1108095   5  15597439  74 types.CodeType
     4  12135  14   916976   4  16514415  78 bytes
     5   5748   7   781728   4  17296143  82 function
     6    863   1   734088   3  18030231  86 type
     7    244   0   494712   2  18524943  88 dict of module
     8    863   1   461784   2  18986727  90 dict of type
     9   1319   2   415880   2  19402607  92 dict (no owner)
<282 more rows. Type e.g. '_.more' to view.>

Accessing Individual Element of Heap

First Element :
Partition of a set of 69 objects. Total size = 9007150 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0     69 100  9007150 100   9007150 100 numpy.ndarray

Second Element :
Partition of a set of 25008 objects. Total size = 4135434 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     1  25008 100  4135434 100  13142584 318 str

Third Element :
Partition of a set of 18834 objects. Total size = 1346760 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     2  18834 100  1346760 100  14489344 1076 tuple

Total Heap Size :  20.10 MB

Size Of Object : 0 -  8.59 MB
Size Of Object : 1 -  3.94 MB
Size Of Object : 2 -  1.28 MB
Size Of Object : 3 -  1.06 MB
Size Of Object : 4 -  895.48 KB
Size Of Object : 5 -  763.41 KB
Size Of Object : 6 -  717.28 KB
Size Of Object : 7 -  483.12 KB
Size Of Object : 8 -  450.96 KB
Size Of Object : 9 -  406.13 KB
Size Of Object : 10 -  214.64 KB
Size Of Object : 11 -  151.73 KB
Size Of Object : 12 -  132.05 KB
Size Of Object : 13 -  98.14 KB
Size Of Object : 14 -  91.27 KB

说明

在上述代码片段中创建了一些列表后,我们检索了堆状态。也可以借助列表索引从堆状态对象访问单个对象。然后我们打印了堆状态的不同单个条目。我们还创建了一个简单的方法,它接受以字节为单位的大小作为输入,并以 KB/MB/GB 为单位返回大小。

示例 4

在以下示例中,我们将了解如何查找两个堆状态之间的差异,以检查两个堆状态调用之间创建的对象总数。对于此示例,我们将使用 diff() 方法和 disjoint() 方法。

代码

输出

Heap Status At Starting : 
Heap Size :  12024920  bytes

Partition of a set of 85803 objects. Total size = 12024920 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  25016  29  4136028  34   4136028  34 str
     1  18832  22  1346712  11   5482740  46 tuple
     2   6269   7  1107919   9   6590659  55 types.CodeType
     3  12133  14   916898   8   7507557  62 bytes
     4   5748   7   781728   7   8289285  69 function
     5    863   1   734088   6   9023373  75 type
     6    244   0   494712   4   9518085  79 dict of module
     7    863   1   461784   4   9979869  83 dict of type
     8   1319   2   415880   3  10395749  86 dict (no owner)
     9    150   0   219792   2  10615541  88 set
<282 more rows. Type e.g. '_.more' to view.>

Heap Status After Creating Few Objects :
Heap Size :  12081288  bytes

Partition of a set of 87135 objects. Total size = 12081288 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  25016  29  4136028  34   4136028  34 str
     1  18832  22  1346712  11   5482740  45 tuple
     2   6269   7  1108167   9   6590907  55 types.CodeType
     3  12133  14   916898   8   7507805  62 bytes
     4   5748   7   781728   6   8289533  69 function
     5    863   1   734088   6   9023621  75 type
     6    244   0   494712   4   9518333  79 dict of module
     7    863   1   461784   4   9980117  83 dict of type
     8   1319   2   415880   3  10395997  86 dict (no owner)
     9    150   0   219792   2  10615789  88 set
<282 more rows. Type e.g. '_.more' to view.>

Memory Usage After Creation Of Objects :  56120  bytes

Finding Out Difference Between Two Heap Status :
Whether Two Heap Status Are Disjoint :  False
Total Objects :  1332
Total Size :  56368  Bytes
Number of Entries :  8
Entries :
Index Count  Size  Cumulative Size         Object Name
    0 1329   37216   37216                           int
    1    1   12728   49944                          list
    2    1    6104   56048                 numpy.ndarray
    3    0     248   56296                types.CodeType
    4    1      72   56368              builtins.weakref
    5    0       0   56368          ctypes.CFunctionType
    6    0       0   56368               ctypes._FuncPtr
    7    0       0   56368       dict of ctypes._FuncPtr

说明

在上述代码片段中,我们首先在开始时获取了堆状态。然后我们创建了一些对象列表和字符串对象。创建这些对象后,我们再次获取了另一个堆状态。然后我们在第二个堆状态对象上调用了 diff() 方法,将第一个堆状态对象作为参数传递,以获取两个堆快照之间的差异。然后我们遍历了 stats 对象并打印了对象的差异。

示例 5

在以下示例中,我们将了解 count、size、referents、referrersstat 等一些属性以及 dump()load() 等一些方法的用法。

代码

输出

Few Important Properties/Methods of Heap Status Object() :

Count of Objects in Heap :  85798

Heap Size :  12024638  bytes

Heap 1st Entry :
Partition of a set of 25016 objects. Total size = 4136095 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  25016 100  4136095 100   4136095 100 str

Set of Objects Referred to By Any Objects :

Partition of a set of 85796 objects. Total size = 12019590 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  25016  29  4136095  34   4136095  34 str
     1  18830  22  1346552  11   5482647  46 tuple
     2   6268   7  1107991   9   6590638  55 types.CodeType
     3  12132  14   916913   8   7507551  62 bytes
     4   5749   7   781864   7   8289415  69 function
     5    863   1   734496   6   9023911  75 type
     6    244   0   494712   4   9518623  79 dict of module
     7    863   1   461784   4   9980407  83 dict of type
     8   1315   2   410360   3  10390767  86 dict (no owner)
     9    150   0   219792   2  10610559  88 set
<282 more rows. Type e.g. '_.more' to view.>

Set of Objects Directly Refer to Any Objects :

Partition of a set of 44131 objects. Total size = 6711964 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  18826  43  1346712  20   1346712  20 tuple
     1   6269  14  1108167  17   2454879  37 types.CodeType
     2   5749  13   781864  12   3236743  48 function
     3    864   2   735560  11   3972303  59 type
     4    244   1   494712   7   4467015  67 dict of module
     5    863   2   461784   7   4928799  73 dict of type
     6    911   2   389432   6   5318231  79 dict (no owner)
     7    136   0   216768   3   5534999  82 set
     8   2158   5   155376   2   5690375  85 types.WrapperDescriptorType
     9    491   1   113912   2   5804287  86 dict of function
<241 more rows. Type e.g. '_.more' to view.>

Stat Object :
Partition of a set of 85798 objects. Total size = 12024638 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  25016  29  4136095  34   4136095  34 str
     1  18830  22  1346552  11   5482647  46 tuple
     2   6268   7  1107743   9   6590390  55 types.CodeType
     3  12132  14   916913   8   7507303  62 bytes
     4   5748   7   781728   7   8289031  69 function
     5    863   1   734088   6   9023119  75 type
     6    244   0   494712   4   9517831  79 dict of module
     7    863   1   461784   4   9979615  83 dict of type
     8   1319   2   415880   3  10395495  86 dict (no owner)
     9    150   0   219792   2  10615287  88 set
<282 more rows. Type e.g. '_.more' to view.>
First 5 Entries :
Index Count  Size  Cumulative Size         Object Name
    025016 4136095 4136095                           str
    118830 1346552 5482647                         tuple
    2 6268 1107743 6590390                types.CodeType
    312132  916913 7507303                         bytes
    4 5748  781728 8289031                      function

Dumping Heap Status to a File :
['.loader: _load_stat\n', '.format: SetFormat\n', '.timemade: 1649769632.860880\n', '.count: 85798\n', '.size: 12024638\n']
First 5 Entries From Loaded File : 
Index Count  Size  Cumulative Size         Object Name
    025016 4136095 4136095                           str
    118830 1346552 5482647                         tuple
    2 6268 1107743 6590390                types.CodeType
    312132  916913 7507303                         bytes
    4 5748  781728 8289031                      function

说明

在上述代码片段中,我们解释了 count、size、referents、referrersstat 等属性以及 dump()load() 等方法的用法。

示例 6

在以下示例中,我们将了解可通过堆状态对象使用的​​不同属性,这些属性允许我们根据不同属性(例如类型、大小、引用者、内存地址等)对堆状态条目进行分组。

代码

输出

=========== Heap Status At Starting : ============
Partition of a set of 42072 objects. Total size = 5086505 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  12893  31  1168594  23   1168594  23 str
     1   8414  20   582912  11   1751506  34 tuple
     2    610   1   515280  10   2266786  45 type
     3   2910   7   514056  10   2780842  55 types.CodeType
     4   5565  13   396264   8   3177106  62 bytes
     5   2669   6   362984   7   3540090  70 function
     6    610   1   297456   6   3837546  75 dict of type
     7    106   0   174272   3   4011818  79 dict of module
     8     98   0   169136   3   4180954  82 set
     9    377   1   134664   3   4315618  85 dict (no owner)
<147 more rows. Type e.g. '_.more' to view.>

============ Heap Status Grouped By Type : ==========
Partition of a set of 42072 objects. Total size = 5086505 bytes.
 Index  Count   %     Size   % Cumulative  % Type
     0  12893  31  1168594  23   1168594  23 str
     1   1766   4   736256  14   1904850  37 dict
     2   8414  20   582912  11   2487762  49 tuple
     3    610   1   515280  10   3003042  59 type
     4   2910   7   514056  10   3517098  69 types.CodeType
     5   5565  13   396264   8   3913362  77 bytes
     6   2669   6   362984   7   4276346  84 function
     7     98   0   169136   3   4445482  87 set
     8   1170   3    84240   2   4529722  89 types.WrapperDescriptorType
     9    941   2    67752   1   4597474  90 builtins.weakref
<96 more rows. Type e.g. '_.more' to view.>

==== Heap Status Grouped By Referrers of kind(class/dict of class) : ===
Partition of a set of 42072 objects. Total size = 5087009 bytes.
 Index  Count   %     Size   % Cumulative  % Referrers by Kind (class / dict of class)
     0  11255  27   824572  16    824572  16 types.CodeType
     1   5314  13   667128  13   1491700  29 function
     2   5040  12   533219  10   2024919  40 dict of type
     3   1972   5   410976   8   2435895  48 type
     4   5191  12   365199   7   2801094  55 tuple
     5   1480   4   247408   5   3048502  60 dict of module
     6    207   0   186655   4   3235157  64 dict of module, tuple
     7    718   2   181110   4   3416267  67 function, tuple
     8     75   0   153856   3   3570123  70 dict of _frozen_importlib_external.FileFinder
     9   2251   5   138361   3   3708484  73 set
<546 more rows. Type e.g. '_.more' to view.>

========== Heap Status Grouped By Module : ============
Partition of a set of 42072 objects. Total size = 5087526 bytes.
 Index  Count   %     Size   % Cumulative  % Module
     0  41966 100  5079894 100   5079894 100 ~module
     1      1   0       72   0   5079966 100 __main__
     2      1   0       72   0   5080038 100 _abc
     3      1   0       72   0   5080110 100 _ast
     4      1   0       72   0   5080182 100 _bootlocale
     5      1   0       72   0   5080254 100 _codecs
     6      1   0       72   0   5080326 100 _collections
     7      1   0       72   0   5080398 100 _ctypes
     8      1   0       72   0   5080470 100 _functools
     9      1   0       72   0   5080542 100 _heapq
<97 more rows. Type e.g. '_.more' to view.>

========== Heap Status Grouped By Individual Size : ==============
Partition of a set of 42072 objects. Total size = 5087894 bytes.
 Index  Count   %     Size   % Cumulative  % Individual Size
     0   2809   7   494752  10    494752  10       176
     1    401   1   426664   8    921416  18      1064
     2   2734   6   371824   7   1293240  25       136
     3   4836  11   348192   7   1641432  32        72
     4   2709   6   173376   3   1814808  36        64
     5   2919   7   163464   3   1978272  39        56
     6    614   1   142448   3   2120720  42       232
     7   2575   6   123600   2   2244320  44        48
     8    184   0   117760   2   2362080  46       640
     9     94   0   110544   2   2472624  49      1176
<625 more rows. Type e.g. '_.more' to view.>

========= Heap Status Grouped By Total Size : ============
Partition of a set of 42072 objects. Total size = 5087931 bytes.
 Index  Count   %     Size   % Cumulative  % 
     0  42072 100  5087931 100   5087931 100 

======= Heap Status Grouped By Referred Via : =============
Partition of a set of 42072 objects. Total size = 5088145 bytes.
 Index  Count   %     Size   % Cumulative  % Referred Via:
     0   1359   3   506095  10    506095  10 '.__dict__'
     1   2529   6   447756   9    953851  19 '.__code__'
     2   2910   7   274420   5   1228271  24 '.co_code'
     3   2230   5   180816   4   1409087  28 '.co_names'
     4    698   2   179824   4   1588911  31 '.__doc__', '[0]'
     5    595   1   175017   3   1763928  35 "['__doc__']"
     6   2000   5   140714   3   1904642  37 '.__qualname__'
     7   2264   5   139011   3   2043653  40 'list(_)[0]'
     8   1701   4   121032   2   2164685  43 '.co_consts'
     9   2618   6   120252   2   2284937  45 '.co_lnotab'
<7259 more rows. Type e.g. '_.more' to view.>

========= Heap Status Grouped By IDset : ============
Partition of a set of 42072 objects. Total size = 5090746 bytes.
 Index  Count   %     Size   % Cumulative  % by identity set
     0      1   0    32984   1     32984   1 <1 set: 0x1df4279a2e0>
     1      1   0    32984   1     65968   1 <1 set: 0x1df4279a4a0>
     2      1   0     9312   0     75280   1 <1 dict (no owner): 0x1df422f3ec0*313>
     3      1   0     9312   0     84592   2 <1 dict (no owner): 0x1df42518f40*326>
     4      1   0     8408   0     93000   2 <1 set: 0x1df42388740>
     5      1   0     8408   0    101408   2 <1 set: 0x1df423889e0>
     6      1   0     8408   0    109816   2 <1 set: 0x1df42388c80>
     7      1   0     8408   0    118224   2 <1 set: 0x1df42388e40>
     8      1   0     8408   0    126632   2 <1 set: 0x1df4279ae40>
     9      1   0     8408   0    135040   3 <1 set: 0x1df427b6120>
<42062 more rows. Type e.g. '_.more' to view.>

======== Heap Status Grouped By Address : ==========
Set of 42072  objects. Total size = 5090834 bytes.
 Index     Size   %   Cumulative  %   Brief
     0    32984   0.6     32984   0.6 set: 0x1df4279a2e0
     1    32984   0.6     65968   1.3 set: 0x1df4279a4a0
     2     9312   0.2     75280   1.5 dict (no owner): 0x1df422f3ec0*320
     3     9312   0.2     84592   1.7 dict (no owner): 0x1df42518f40*326
     4     8408   0.2     93000   1.8 set: 0x1df42388740
     5     8408   0.2    101408   2.0 set: 0x1df423889e0
     6     8408   0.2    109816   2.2 set: 0x1df42388c80
     7     8408   0.2    118224   2.3 set: 0x1df42388e40
     8     8408   0.2    126632   2.5 set: 0x1df4279ae40
     9     8408   0.2    135040   2.7 set: 0x1df427b6120
<42062 more rows. Type e.g. '_.more' to view.>

说明

在上述代码片段中,我们解释了 bytype、byrcs、bymodule、bysize、byunity、byvia、byidsetbyid 等属性的用法。

示例 7

在以下示例中,我们将了解通过堆对象可用的两个支持方法的用法。这些方法是 iso()idset()

代码

输出

============== ISO Method Examples ====================

Partition of a set of 1 object. Total size = 12029656 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      1 100 12029656 100  12029656 100 list

Partition of a set of 1 object. Total size = 50 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      1 100       50 100        50 100 str

Partition of a set of 1 object. Total size = 59 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      1 100       59 100        59 100 str

Partition of a set of 1 object. Total size = 56 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      1 100       56 100        56 100 list

Partition of a set of 1 object. Total size = 49 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      1 100       49 100        49 100 str

============== IDSET Method Examples ===================

Partition of a set of 1500000 objects. Total size = 41999996 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0 1500000 100 41999996 100  41999996 100 int
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      1 100       50 100        50 100 str

Partition of a set of 8 objects. Total size = 400 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      8 100      400 100       400 100 str




说明

在上述代码片段中,我们解释了 iso()idset() 方法的用法,它们接受单个或多个对象作为输入并返回指定对象大小的状态。我们还可以观察两种方法结果之间的差异。

示例 8

在以下示例中,我们将了解检查通过 guppy 可用的对象特定方法/属性的 doc 的方法。

代码

输出

============== Heap Documents ====================
Top level interface to Heapy. Available attributes:
Anything            Prod                Via                 iso
Clodo               Rcs                 doc                 load
Id                  Root                findex              monitor
Idset               Size                heap                pb
Module              Type                heapu               setref
Nothing             Unity               idset               test
Use eg: the_heap.doc. for info on .
Doc On Individual Method/Function/Class :

ISO Method Doc :

iso( 0..*:node: Any+) -> IdentitySet[1]

Create a set of objects based on identity.

Argument
    node: Any+
        Any kind of objects are valid arguments.
Note
    This method is the same as idset[2] except for the argument.
References
    [0] heapy_Use.html#heapykinds.Use.iso
    [1] heapy_UniSet.html#heapykinds.IdentitySet
    [2] heapy_Use.html#heapykinds.Use.idset

Root Class Doc :

Root: RootStateType

This attribute is a symbolic root containing attributes from which all
reachable objects in the heap can be reached. It is the only value (a
singleton) of its kind; see [1] for a description of its attributes.

References
        [0] heapy_Use.html#heapykinds.Use.Root
        [1] heapy_RootState.html#heapykinds.RootStateType
============= Heap Status Documents ================
all                 byvia               get_render          pathsout
biper               count               get_rp              prod
brief               dictof              get_shpaths         referents
by                  diff                imdom               referrers
byclodo             disjoint            indisize            rp
byid                doc                 kind                shpaths
byidset             dominos             maprox              size
bymodule            domisize            more                sp
byprod              dump                nodes               stat
byrcs               er                  owners              test_contains
bysize              fam                 partition           theone
bytype              get_ckc             parts
byunity             get_examples        pathsin
Doc On Individual Method/Function/Class :

Doc on parts Property of Heap Status :

An iterable object, that can be used to iterate over the 'parts' of
self. The iteration order is determined by the sorting order the set
has, in the table printed when partitioned.

Doc on byrcs Property of Heap Status :

A copy of self, but with 'Rcs' as the equivalence relation.

说明

在上述代码片段中,我们通过调用堆对象的 doc 属性访问了堆的文档。它将显示整个堆对象的文档,列出可通过该对象使用的​​所有方法/属性。我们还通过在 doc 属性上调用该方法属性的名称来访问单个方法/属性文档。我们还访问了堆状态对象的文档。