Python Object Graphs — objgraph 1.7.2 documentation
Python Object Graphs — objgraph 1.7.2 documentation
Python Object Graphs¶
objgraph is a module that lets you visually explore Python object graphs.
You’ll need graphviz if you want to draw the pretty graphs.
I recommend xdot for interactive use. pip install xdot should suffice; objgraph will automatically look for it in your PATH.
Installation and Documentation¶
pip install objgraph or download it from PyPI.
Documentation lives at http://mg.pov.lt/objgraph.
Quick start¶
Try this in a Python shell:
>>> x = []
>>> y = [x, [x], dict(x=x)]
>>> import objgraph
>>> objgraph.show_refs([y], filename='sample-graph.png')
Graph written to ....dot (... nodes)
Image generated as sample-graph.png(If you’ve installed xdot, omit the filename argument to get the interactive viewer.)
You should see a graph like this:
Backreferences¶
Now try
>>> objgraph.show_backrefs([x], filename='sample-backref-graph.png')
...
Graph written to ....dot (8 nodes)
Image generated as sample-backref-graph.pngand you’ll see
Memory leak example¶
The original purpose of objgraph was to help me find memory leaks. The idea was to pick an object in memory that shouldn’t be there and then see what references are keeping it alive.
To get a quick overview of the objects in memory, use the imaginatively-named show_most_common_types():
>>> objgraph.show_most_common_types()
tuple 5224
function 1329
wrapper_descriptor 967
dict 790
builtin_function_or_method 658
method_descriptor 340
weakref 322
list 168
member_descriptor 167
type 163But that’s looking for a small needle in a large haystack. Can we limit our haystack to objects that were created recently? Perhaps.
Let’s define a function that “leaks” memory
>>> class MyBigFatObject(object):
... pass
...
>>> def computate_something(_cache={}):
... _cache[42] = dict(foo=MyBigFatObject(),
... bar=MyBigFatObject())
... # a very explicit and easy-to-find "leak" but oh well
... x = MyBigFatObject() # this one doesn't leakWe take a snapshot of all the objects counts that are alive before we call our function
>>> objgraph.show_growth(limit=3)
tuple 5228 +5228
function 1330 +1330
wrapper_descriptor 967 +967and see what changes after we call it
>>> computate_something()
>>> objgraph.show_growth()
MyBigFatObject 2 +2
dict 797 +1It’s easy to see MyBigFatObject instances that appeared and were not freed. I can pick one of them at random and trace the reference chain back to one of the garbage collector’s roots.
For simplicity’s sake let’s assume all of the roots are modules; if you’ve any examples where that isn’t true, I’d love to hear about them (although see Reference counting bugs).
>>> import inspect, random
>>> objgraph.show_chain(
... objgraph.find_backref_chain(
... random.choice(objgraph.by_type('MyBigFatObject')),
... inspect.ismodule),
... filename='chain.png')
Graph written to ...dot (13 nodes)
Image generated as chain.pngIt is perhaps surprising to find linecache at the end of that chain (apparently doctest monkey-patches it), but the important things – computate_something() and its cache dictionary – are in there.
There are other tools, perhaps better suited for memory leak hunting: heapy, Dozer.
Reference counting bugs¶
Bugs in C-level reference counting may leave objects in memory that do not have any other objects pointing at them. You can find these by calling get_leaking_objects(), but you’ll have to filter out legitimate GC roots from them, and there are a lot of those:
>>> roots = objgraph.get_leaking_objects()
>>> len(roots)
4621>>> objgraph.show_most_common_types(objects=roots)
...
tuple 4333
dict 171
list 74
instancemethod 4
listiterator 2
MemoryError 1
Sub 1
RuntimeError 1
Param 1
Add 1>>> objgraph.show_refs(roots[:3], refcounts=True, filename='roots.png')
...
Graph written to ...dot (19 nodes)
Image generated as roots.pngAPI Documentation¶
More examples, that also double as tests¶
History¶
I’ve developed a set of functions that eventually became objgraph when I was hunting for memory leaks in a Python program. The whole story – with illustrated examples – is in this series of blog posts:
And here’s the change log
Support and Development¶
The source code can be found in this Bazaar repository: https://code.launchpad.net/~mgedmin/objgraph/trunk.
To check it out, use bzr branch lp:objgraph.
Report bugs at https://bugs.launchpad.net/objgraph.
For more information, see Hacking on objgraph.
Python Object Graphs — objgraph 1.7.2 documentation的更多相关文章
- Odoo8查询产品时提示"maximum recursion depth exceeded while calling a Python object"
今天在生产系统中查询产品时,莫名提示错误:maximum recursion depth exceeded while calling a Python object,根据错误日志提示,发现在查询产品 ...
- scrapy RuntimeError: maximum recursion depth exceeded while calling a Python object 超出python最大递归数异常
2019-10-21 19:01:00 [scrapy.core.engine] INFO: Spider opened2019-10-21 19:01:00 [scrapy.extensions.l ...
- pickle — Python object serialization
pickle - Python object serialization 消息队列
- Python Object Oriented
1. Creating class class className: 'Optional class documentation string' class_suite The class has a ...
- python object对象
动态语言的对象属性 既然都是动态语言,自然python和熟知的JavaScript很像,建一个空对象用来存放所有的数据,看看js: var data = {}; data.name = 'CooMar ...
- [Python] Object spread operator in Python
In JS, we have object spread opreator: const x = { a: '1', b: '2' } const y = { c: '3', d: '4' } con ...
- Commons JXPath - Modifying Object Graphs
JXPath 除了可以 XPath 语法访问 JavaBeans.DOM/JDOM,也可以对其属性赋值. 以下面的 JavaBeans 为例. package com.huey.jxpath; imp ...
- python object takes no parameters
class Song(object): def __init__(self,lyrics): self.lyrics = lyrics def sing_me_a_song(self): for li ...
- python object类
这个应为写得,写得蛮啰嗦的,建议耐心的人看看:http://www.cafepy.com/article/python_types_and_objects/python_types_and_objec ...
随机推荐
- Ubuntu 14.04下安装GitLab指南
摘要 GitLab 是一个用于仓库管理系统的开源项目.使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 在GitLab的官方网站上面对Ubuntu的支持也是很好的,有比较详尽的安装指南. ...
- cocos2d-x游戏开发系列教程-坦克大战游戏之所有坦克之间的碰撞检测
上篇我们完成了简单的AI编写,但是各个坦克移动时之间是可以重合的, 这节课我们来完成坦克之间的碰撞检测,还是在上篇的EnemyAI中完成. 1.我先现在坦克类Tank中添加两个成员变量: CC_SYN ...
- 基于visual Studio2013解决C语言竞赛题之0906文件插入
题目
- cmake总结
无论生活还是工作上,做过的事,需要总结下. 接触cmake,一般都会看一本书 <<cmake 实践>>. 这是cmake的入门书.我就不多说了. 下面说一下我对部分cmake命 ...
- mysql备份数据库几种方法
方法一 cmd 到mysql bin文件夹下用 例如以下命令 mysqldump --opt -h192.168.0.156 -uusername -ppassword --skip-lock-tab ...
- Java EE登陆界面生成随机数防止恶意注册或者登录
package cn.com; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.a ...
- URAL 1180. Stone Game (博弈 + 规律)
1180. Stone Game Time limit: 1.0 second Memory limit: 64 MB Two Nikifors play a funny game. There is ...
- asp.net2.0安全性(3)--验证与授权--转载来自车老师
"验证"与"授权"是对网页资源安全管理的两道门. 验证(Authentication):检查用户是否是合法的用户.就像是网站大门口的保卫,服责验证使用的用户名和 ...
- C++基础学习笔记----第十四课(new和malloc的区别、单例模式等深入)
本节主要讲new关键字和malloc函数的差别,编译器对构造函数调用的实质,单例模式的实现等. new和malloc的差别 1.malloc和free是C语言的库函数,以字节为单位申请堆空间.new和 ...
- 在Ubuntu 12.04下编译qtiplot
不在windows下,再加上不想用盗版,所以需要一个origin的替代品——qtiplot.虽然我非常抵抗用这种不停点来点去的软件,用R的ggplot2画图多好啊,高效.优雅.漂亮,但是终抵不过老板一 ...