在Python Kivy中查找程序使用的总内存的方法

4 投票
1 回答
584 浏览
提问于 2025-04-17 23:57

有没有办法在Python Kivy中找到一个进程和整个程序使用的总内存?也就是说,我想知道:

  1. 这个程序总共使用了多少内存?
  2. 有哪些对象在活动中,它们各自使用了多少内存?

谢谢!

1 个回答

2

Heapy 是一个用于Python的内存分析工具。你可以这样使用它:

>>> from guppy import hpy
>>> h = hpy()
>>> h.heap()

运行后,你会看到类似这样的输出:

Partition of a set of 1449133 objects. Total size = 102766644 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0 985931  68 46300932  45  46300932  45 str
     1  24681   2 22311624  22  68612556  67 dict of pkgcore.ebuild.ebuild_src.package
     2  49391   3 21311864  21  89924420  88 dict (no owner)
     3 115974   8  3776948   4  93701368  91 tuple
     4 152181  11  3043616   3  96744984  94 long
     5  36009   2  1584396   2  98329380  96 weakref.KeyedRef
     6  11328   1  1540608   1  99869988  97 dict of pkgcore.ebuild.ebuild_src.ThrowAwayNameSpace
     7  24702   2   889272   1 100759260  98 types.MethodType
     8  11424   1   851840   1 101611100  99 list
     9  24681   2   691068   1 102302168 100 pkgcore.ebuild.ebuild_src.package
<54 more rows. Type e.g. '_.more' to view.>

这个输出“基本上是你在内存中能找到的东西的快照”。我之前没有做过很多Kivy的开发,所以没机会去分析内存,但我觉得这个工具应该能正常工作。

你可以参考:

撰写回答