Python guppy/heapy2024 年 8 月 29 日 | 阅读 26 分钟 guppy3 是一个 Python 包,它向用户提供当前堆状态和堆中可用的对象。此详细信息可以支持开发人员进行内存分析和分析。guppy 包包含一个名为 heapy 的子包。heapy 子包提供了一系列重要方法和当前堆状态。当请求堆状态时,guppy3 包提供一个特殊的 C-nodesets 对象。此 C-nodesets 对象包含内存中可用每个对象的堆状态。guppy3 包还包含与堆中所有可达或不可达对象相关的详细信息,作为一种特殊类型的列表。它还提供与对象数量、对象占用的内存百分比、以字节为单位的大小和类型信息相关的详细信息。除此之外,guppy3 包提供了一系列有用的方法,允许我们访问堆状态的单个元素、查找堆状态之间的差异、查找以字节为单位的对象大小以及许多其他功能。在本教程中,我们将通过不同的示例了解 guppy3 包的用法,以收集与 Python 内存使用相关的信息。 那么,让我们开始吧。 了解 guppy3 包guppy3 包是一个 Python 编程环境和堆分析工具集。此包包含以下子包
以上子包的描述如下 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 包使用的方法和属性
现在让我们考虑一些示例,以了解上述 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、referrers 和 stat 等一些属性以及 dump() 和 load() 等一些方法的用法。 代码 输出 Few Important Properties/Methods of Heap Status Object( 说明 在上述代码片段中,我们解释了 count、size、referents、referrers 和 stat 等属性以及 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 % 说明 在上述代码片段中,我们解释了 bytype、byrcs、bymodule、bysize、byunity、byvia、byidset 和 byid 等属性的用法。 示例 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. 说明 在上述代码片段中,我们通过调用堆对象的 doc 属性访问了堆的文档。它将显示整个堆对象的文档,列出可通过该对象使用的所有方法/属性。我们还通过在 doc 属性上调用该方法属性的名称来访问单个方法/属性文档。我们还访问了堆状态对象的文档。 |
我们请求您订阅我们的新闻通讯以获取最新更新。