推荐使用哪种 Python 内存分析器? [英] Which Python memory profiler is recommended?

查看:38
本文介绍了推荐使用哪种 Python 内存分析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的 Python 应用程序的内存使用情况,特别想知道哪些代码块/部分或对象消耗了最多的内存.Google 搜索显示一个商业版本是 Python 内存验证器(仅限 Windows).>

开源的是 PySizerHeapy.

我没有尝试过任何人,所以我想知道哪一个是最好的考虑:

  1. 提供了大部分细节.

  2. 我必须对我的代码做最少或不做任何更改.

解决方案

guppy3 很简单用.在您的代码中的某个时刻,您必须编写以下内容:

from guppy import hpyh = hpy()打印(h.heap())

这会给你一些这样的输出:

一组132527个对象的分区.总大小 = 8301532 字节.索引计数 % 大小 % 累积 % 种类(类/类的字典)0 35144 27 2140412 26 2140412 26 字符串1 38397 29 1309020 16 3449432 42 元组2 530 0 739856 9 4189288 50 字典(无所有者)

您还可以从对象被引用的位置找出并获得有关的统计信息,但不知何故,有关该对象的文档有点稀少.

还有一个用 Tk 编写的图形浏览器.

对于 Python 2.x,使用 Heapy.

I want to know the memory usage of my Python application and specifically want to know what code blocks/portions or objects are consuming most memory. Google search shows a commercial one is Python Memory Validator (Windows only).

And open source ones are PySizer and Heapy.

I haven't tried anyone, so I wanted to know which one is the best considering:

  1. Gives most details.

  2. I have to do least or no changes to my code.

解决方案

guppy3 is quite simple to use. At some point in your code, you have to write the following:

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

This gives you some output like this:

Partition of a set of 132527 objects. Total size = 8301532 bytes.
Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
0  35144  27  2140412  26   2140412  26 str
1  38397  29  1309020  16   3449432  42 tuple
2    530   0   739856   9   4189288  50 dict (no owner)

You can also find out from where objects are referenced and get statistics about that, but somehow the docs on that are a bit sparse.

There is a graphical browser as well, written in Tk.

For Python 2.x, use Heapy.

这篇关于推荐使用哪种 Python 内存分析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆