Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 10 years ago.
The community reviewed whether to reopen this question 2 years ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
考虑 objgraph 库(请参阅这篇博文作为示例案件)。
Consider the objgraph library (see this blog post for an example use case).
Muppy 是(又一个)Python 内存使用分析器。 该工具集的重点是识别内存泄漏。
Muppy 试图帮助开发人员识别 Python 应用程序的内存泄漏。 它可以跟踪运行时的内存使用情况并识别泄漏的对象。 此外,还提供了一些工具,可以定位未发布对象的来源。
Muppy is (yet another) Memory Usage Profiler for Python. The focus of this toolset is laid on the identification of memory leaks.
Muppy tries to help developers to identity memory leaks of Python applications. It enables the tracking of memory usage during runtime and the identification of objects which are leaking. Additionally, tools are provided which allow to locate the source of not released objects.
我正在开发一个名为 memprof 的 Python 内存分析器:
http://jmdana.github.io/memprof/
它允许您在执行修饰方法期间记录并绘制变量的内存使用情况。 您只需使用以下方法导入库:
并使用以下方法装饰您的方法:
这是有关绘图外观的示例:
该项目托管在 GitHub 中:
https://github.com/jmdana/memprof
I'm developing a memory profiler for Python called memprof:
http://jmdana.github.io/memprof/
It allows you to log and plot the memory usage of your variables during the execution of the decorated methods. You just have to import the library using:
And decorate your method using:
This is an example on how the plots look like:
The project is hosted in GitHub:
https://github.com/jmdana/memprof
我发现 meliae 比 Heapy 或 PySizer 功能更强大。 如果您碰巧正在运行 wsgi web 应用程序,那么 Dozer 是 Dowser 的一个很好的中间件包装器
I found meliae to be much more functional than Heapy or PySizer. If you happen to be running a wsgi webapp, then Dozer is a nice middleware wrapper of Dowser
还可以尝试 pytracemalloc 项目,它提供了每个 Python 行号的内存使用情况。
编辑(2014/04):它现在有一个 Qt GUI 来分析快照。
Try also the pytracemalloc project which provides the memory usage per Python line number.
EDIT (2014/04): It now has a Qt GUI to analyze snapshots.
我推荐 Dowser。 设置非常简单,并且您不需要对代码进行零更改。 您可以通过简单的 Web 界面查看随时间变化的每种类型的对象计数、查看活动对象列表、查看对活动对象的引用。
您导入 memdebug,然后调用 memdebug.start。 就这样。
我还没有尝试过 PySizer 或 Heapy。 我很感激其他人的评论。
更新
以上代码适用于
CherryPy 2.X
,CherryPy 3.X
的server.quickstart
方法已已删除,并且engine.start
不采用blocking
标志。 因此,如果您使用的是CherryPy 3.X
I recommend Dowser. It is very easy to setup, and you need zero changes to your code. You can view counts of objects of each type through time, view list of live objects, view references to live objects, all from the simple web interface.
You import memdebug, and call memdebug.start. That's all.
I haven't tried PySizer or Heapy. I would appreciate others' reviews.
UPDATE
The above code is for
CherryPy 2.X
,CherryPy 3.X
theserver.quickstart
method has been removed andengine.start
does not take theblocking
flag. So if you are usingCherryPy 3.X
guppy3 使用起来非常简单。 在代码中的某个时刻,您必须编写以下内容:
这会给您一些像这样的输出:
您还可以找到引用对象的位置并获取有关该对象的统计信息,但不知何故,相关文档有点稀疏。
还有一个用 Tk 编写的图形浏览器。
对于 Python 2.x,请使用 Heapy。
guppy3 is quite simple to use. At some point in your code, you have to write the following:
This gives you some output like this:
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.
我的模块 memory_profiler 能够打印内存使用情况的逐行报告,并在 Unix 上运行和 Windows(最后一个需要 psutil)。 输出不是很详细,但目标是让您概述代码在哪里消耗了更多内存,而不是对分配的对象进行详尽的分析。
使用
@profile
修饰函数并使用-m memory_profiler
标志运行代码后,它将打印如下逐行报告:My module memory_profiler is capable of printing a line-by-line report of memory usage and works on Unix and Windows (needs psutil on this last one). Output is not very detailed but the goal is to give you an overview of where the code is consuming more memory, not an exhaustive analysis on allocated objects.
After decorating your function with
@profile
and running your code with the-m memory_profiler
flag it will print a line-by-line report like this: