/usr/share/doc/python-objgraph-doc/html/index.html is in python-objgraph-doc 1.8.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Python Object Graphs — objgraph 1.8.0 documentation</title>
<link rel="stylesheet" href="_static/mg.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.8.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="objgraph 1.8.0 documentation" href="#" />
<link rel="next" title="objgraph" href="objgraph.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="py-modindex.html" title="Python Module Index"
>modules</a></li>
<li class="right" >
<a href="objgraph.html" title="objgraph"
accesskey="N">next</a> |</li>
<li><a href="#">home</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="python-object-graphs">
<h1>Python Object Graphs<a class="headerlink" href="#python-object-graphs" title="Permalink to this headline">¶</a></h1>
<p><tt class="docutils literal"><span class="pre">objgraph</span></tt> is a module that lets you visually explore Python object graphs.</p>
<p>You’ll need <a class="reference external" href="http://www.graphviz.org/">graphviz</a> if you want to draw
the pretty graphs.</p>
<p>I recommend <a class="reference external" href="http://pypi.python.org/pypi/xdot">xdot</a> for interactive use.
<tt class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">xdot</span></tt> should suffice; objgraph will automatically look for it
in your <tt class="docutils literal"><span class="pre">PATH</span></tt>.</p>
<div class="section" id="installation-and-documentation">
<h2>Installation and Documentation<a class="headerlink" href="#installation-and-documentation" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">objgraph</span></tt> or <a class="reference external" href="http://pypi.python.org/pypi/objgraph">download it from PyPI</a>.</p>
<p>Documentation lives at <a class="reference external" href="http://mg.pov.lt/objgraph">http://mg.pov.lt/objgraph</a>.</p>
</div>
<div class="section" id="quick-start">
<h2>Quick start<a class="headerlink" href="#quick-start" title="Permalink to this headline">¶</a></h2>
<p>Try this in a Python shell:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">>>> </span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">],</span> <span class="nb">dict</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="p">)]</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">objgraph</span>
<span class="gp">>>> </span><span class="n">objgraph</span><span class="o">.</span><span class="n">show_refs</span><span class="p">([</span><span class="n">y</span><span class="p">],</span> <span class="n">filename</span><span class="o">=</span><span class="s">'sample-graph.png'</span><span class="p">)</span>
<span class="go">Graph written to ....dot (... nodes)</span>
<span class="go">Image generated as sample-graph.png</span>
</pre></div>
</div>
<p>(If you’ve installed <tt class="docutils literal"><span class="pre">xdot</span></tt>, omit the filename argument to get the
interactive viewer.)</p>
<p>You should see a graph like this:</p>
<div class="figure">
<img alt="[graph of objects reachable from y]" src="_images/sample-graph.png" />
</div>
</div>
<div class="section" id="backreferences">
<h2>Backreferences<a class="headerlink" href="#backreferences" title="Permalink to this headline">¶</a></h2>
<p>Now try</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">objgraph</span><span class="o">.</span><span class="n">show_backrefs</span><span class="p">([</span><span class="n">x</span><span class="p">],</span> <span class="n">filename</span><span class="o">=</span><span class="s">'sample-backref-graph.png'</span><span class="p">)</span>
<span class="gp">... </span>
<span class="go">Graph written to ....dot (8 nodes)</span>
<span class="go">Image generated as sample-backref-graph.png</span>
</pre></div>
</div>
<p>and you’ll see</p>
<div class="figure">
<a class="reference internal image-reference" href="_images/sample-backref-graph.png"><img alt="[graph of objects from which y is reachable]" src="_images/sample-backref-graph.png" /></a>
</div>
</div>
<div class="section" id="memory-leak-example">
<h2>Memory leak example<a class="headerlink" href="#memory-leak-example" title="Permalink to this headline">¶</a></h2>
<p>The original purpose of <a class="reference internal" href="objgraph.html#module-objgraph" title="objgraph"><tt class="xref py py-mod docutils literal"><span class="pre">objgraph</span></tt></a> 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.</p>
<p>To get a quick overview of the objects in memory, use the imaginatively-named
<a class="reference internal" href="objgraph.html#objgraph.show_most_common_types" title="objgraph.show_most_common_types"><tt class="xref py py-func docutils literal"><span class="pre">show_most_common_types()</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">objgraph</span><span class="o">.</span><span class="n">show_most_common_types</span><span class="p">()</span>
<span class="go">tuple 5224</span>
<span class="go">function 1329</span>
<span class="go">wrapper_descriptor 967</span>
<span class="go">dict 790</span>
<span class="go">builtin_function_or_method 658</span>
<span class="go">method_descriptor 340</span>
<span class="go">weakref 322</span>
<span class="go">list 168</span>
<span class="go">member_descriptor 167</span>
<span class="go">type 163</span>
</pre></div>
</div>
<p>But that’s looking for a small needle in a large haystack. Can we limit
our haystack to objects that were created recently? Perhaps.</p>
<p>Let’s define a function that “leaks” memory</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyBigFatObject</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">computate_something</span><span class="p">(</span><span class="n">_cache</span><span class="o">=</span><span class="p">{}):</span>
<span class="gp">... </span> <span class="n">_cache</span><span class="p">[</span><span class="mi">42</span><span class="p">]</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">foo</span><span class="o">=</span><span class="n">MyBigFatObject</span><span class="p">(),</span>
<span class="gp">... </span> <span class="n">bar</span><span class="o">=</span><span class="n">MyBigFatObject</span><span class="p">())</span>
<span class="gp">... </span> <span class="c"># a very explicit and easy-to-find "leak" but oh well</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">MyBigFatObject</span><span class="p">()</span> <span class="c"># this one doesn't leak</span>
</pre></div>
</div>
<p>We take a snapshot of all the objects counts that are alive before
we call our function</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">objgraph</span><span class="o">.</span><span class="n">show_growth</span><span class="p">(</span><span class="n">limit</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="go">tuple 5228 +5228</span>
<span class="go">function 1330 +1330</span>
<span class="go">wrapper_descriptor 967 +967</span>
</pre></div>
</div>
<p>and see what changes after we call it</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">computate_something</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">objgraph</span><span class="o">.</span><span class="n">show_growth</span><span class="p">()</span>
<span class="go">MyBigFatObject 2 +2</span>
<span class="go">dict 797 +1</span>
</pre></div>
</div>
<p>It’s easy to see <tt class="xref py py-class docutils literal"><span class="pre">MyBigFatObject</span></tt> 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.</p>
<p>For simplicity’s sake let’s assume all of the roots are modules. <tt class="docutils literal"><span class="pre">objgraph</span></tt>
provides a function, <a class="reference internal" href="objgraph.html#objgraph.is_proper_module" title="objgraph.is_proper_module"><tt class="xref py py-func docutils literal"><span class="pre">is_proper_module()</span></tt></a>, to check this. If
you’ve any examples where that isn’t true, I’d love to hear about them
(although see <a class="reference internal" href="#leaking-objects"><em>Reference counting bugs</em></a>).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">random</span>
<span class="gp">>>> </span><span class="n">objgraph</span><span class="o">.</span><span class="n">show_chain</span><span class="p">(</span>
<span class="gp">... </span> <span class="n">objgraph</span><span class="o">.</span><span class="n">find_backref_chain</span><span class="p">(</span>
<span class="gp">... </span> <span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">objgraph</span><span class="o">.</span><span class="n">by_type</span><span class="p">(</span><span class="s">'MyBigFatObject'</span><span class="p">)),</span>
<span class="gp">... </span> <span class="n">objgraph</span><span class="o">.</span><span class="n">is_proper_module</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">filename</span><span class="o">=</span><span class="s">'chain.png'</span><span class="p">)</span>
<span class="go">Graph written to ...dot (13 nodes)</span>
<span class="go">Image generated as chain.png</span>
</pre></div>
</div>
<div class="figure">
<a class="reference internal image-reference" href="_images/chain.png"><img alt="[chain of references from a module to a MyBigFatObject instance]" src="_images/chain.png" /></a>
</div>
<p>It is perhaps surprising to find <tt class="xref py py-mod docutils literal"><span class="pre">linecache</span></tt> at the end of that chain
(apparently <tt class="xref py py-mod docutils literal"><span class="pre">doctest</span></tt> monkey-patches it), but the important things –
<tt class="xref py py-func docutils literal"><span class="pre">computate_something()</span></tt> and its cache dictionary – are in there.</p>
<p>There are other tools, perhaps better suited for memory leak hunting:
<a class="reference external" href="http://pypi.python.org/pypi/guppy">heapy</a>,
<a class="reference external" href="http://pypi.python.org/pypi/Dozer">Dozer</a>.</p>
</div>
<div class="section" id="reference-counting-bugs">
<span id="leaking-objects"></span><h2>Reference counting bugs<a class="headerlink" href="#reference-counting-bugs" title="Permalink to this headline">¶</a></h2>
<p>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
<a class="reference internal" href="objgraph.html#objgraph.get_leaking_objects" title="objgraph.get_leaking_objects"><tt class="xref py py-func docutils literal"><span class="pre">get_leaking_objects()</span></tt></a>, but you’ll have to filter out legitimate GC
roots from them, and there are a <em>lot</em> of those:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">roots</span> <span class="o">=</span> <span class="n">objgraph</span><span class="o">.</span><span class="n">get_leaking_objects</span><span class="p">()</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">roots</span><span class="p">)</span>
<span class="go">4621</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">objgraph</span><span class="o">.</span><span class="n">show_most_common_types</span><span class="p">(</span><span class="n">objects</span><span class="o">=</span><span class="n">roots</span><span class="p">)</span>
<span class="gp">... </span>
<span class="go">tuple 4333</span>
<span class="go">dict 171</span>
<span class="go">list 74</span>
<span class="go">instancemethod 4</span>
<span class="go">listiterator 2</span>
<span class="go">MemoryError 1</span>
<span class="go">Sub 1</span>
<span class="go">RuntimeError 1</span>
<span class="go">Param 1</span>
<span class="go">Add 1</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">objgraph</span><span class="o">.</span><span class="n">show_refs</span><span class="p">(</span><span class="n">roots</span><span class="p">[:</span><span class="mi">3</span><span class="p">],</span> <span class="n">refcounts</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s">'roots.png'</span><span class="p">)</span>
<span class="gp">... </span>
<span class="go">Graph written to ...dot (19 nodes)</span>
<span class="go">Image generated as roots.png</span>
</pre></div>
</div>
<div class="figure">
<a class="reference internal image-reference" href="_images/roots.png"><img alt="[GC roots and potentially leaked objects]" src="_images/roots.png" /></a>
</div>
</div>
<div class="section" id="api-documentation">
<h2>API Documentation<a class="headerlink" href="#api-documentation" title="Permalink to this headline">¶</a></h2>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="objgraph.html"><tt class="docutils literal"><span class="pre">objgraph</span></tt></a><ul>
<li class="toctree-l2"><a class="reference internal" href="objgraph.html#statistics">Statistics</a></li>
<li class="toctree-l2"><a class="reference internal" href="objgraph.html#locating-and-filtering-objects">Locating and Filtering Objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="objgraph.html#traversing-and-displaying-object-graphs">Traversing and Displaying Object Graphs</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="section" id="more-examples-that-also-double-as-tests">
<h2>More examples, that also double as tests<a class="headerlink" href="#more-examples-that-also-double-as-tests" title="Permalink to this headline">¶</a></h2>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="references.html">Too many references</a></li>
<li class="toctree-l1"><a class="reference internal" href="references.html#reference-counts">Reference counts</a></li>
<li class="toctree-l1"><a class="reference internal" href="extra-info.html">Extra information</a></li>
<li class="toctree-l1"><a class="reference internal" href="highlighting.html">Highlighting</a></li>
<li class="toctree-l1"><a class="reference internal" href="uncollectable.html">Uncollectable garbage</a></li>
<li class="toctree-l1"><a class="reference internal" href="generator-sample.html">Stack frames and generators</a></li>
<li class="toctree-l1"><a class="reference internal" href="chain.html">Graph searches</a></li>
<li class="toctree-l1"><a class="reference internal" href="quoting.html">Quoting unsafe characters</a></li>
</ul>
</div>
</div>
<div class="section" id="history">
<h2>History<a class="headerlink" href="#history" title="Permalink to this headline">¶</a></h2>
<p>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:</p>
<ul class="simple">
<li><a class="reference external" href="http://mg.pov.lt/blog/hunting-python-memleaks.html">Hunting memory leaks in Python</a></li>
<li><a class="reference external" href="http://mg.pov.lt/blog/python-object-graphs.html">Python object graphs</a></li>
<li><a class="reference external" href="http://mg.pov.lt/blog/object-graphs-with-graphviz.html">Object graphs with graphviz</a></li>
</ul>
<p>And here’s the change log</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="CHANGES.html">Changes</a><ul>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id1">1.8.0 (2014-02-13)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id2">1.7.2 (2012-10-23)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id3">1.7.1 (2011-12-11)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id4">1.7.0 (2011-03-11)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id5">1.6.0 (2010-12-18)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id6">1.5.1 (2010-12-09)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id7">1.5.0 (2010-12-05)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id8">1.4.0 (2010-11-03)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id9">1.3.1 (2010-07-17)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id10">1.3 (2010-07-13)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id11">1.2 (2009-03-25)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id12">1.1 (2008-09-10)</a></li>
<li class="toctree-l2"><a class="reference internal" href="CHANGES.html#id13">1.0 (2008-06-14)</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="section" id="support-and-development">
<h2>Support and Development<a class="headerlink" href="#support-and-development" title="Permalink to this headline">¶</a></h2>
<p>The source code can be found in this Git repository:
<a class="reference external" href="https://github.com/mgedmin/objgraph">https://github.com/mgedmin/objgraph</a>.</p>
<p>To check it out, use <tt class="docutils literal"><span class="pre">git</span> <span class="pre">clone</span> <span class="pre">https://github.com/mgedmin/objgraph</span></tt>.</p>
<p>Report bugs at <a class="reference external" href="https://github.com/mgedmin/objgraph/issues">https://github.com/mgedmin/objgraph/issues</a>.</p>
<p>For more information, see <a class="reference internal" href="HACKING.html#hacking"><em>Hacking on objgraph</em></a>.</p>
<div class="toctree-wrapper compound">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="#">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Python Object Graphs</a><ul>
<li><a class="reference internal" href="#installation-and-documentation">Installation and Documentation</a></li>
<li><a class="reference internal" href="#quick-start">Quick start</a></li>
<li><a class="reference internal" href="#backreferences">Backreferences</a></li>
<li><a class="reference internal" href="#memory-leak-example">Memory leak example</a></li>
<li><a class="reference internal" href="#reference-counting-bugs">Reference counting bugs</a></li>
<li><a class="reference internal" href="#api-documentation">API Documentation</a></li>
<li><a class="reference internal" href="#more-examples-that-also-double-as-tests">More examples, that also double as tests</a></li>
<li><a class="reference internal" href="#history">History</a></li>
<li><a class="reference internal" href="#support-and-development">Support and Development</a></li>
</ul>
</li>
</ul>
<h4>Next topic</h4>
<p class="topless"><a href="objgraph.html"
title="next chapter"><tt class="docutils literal"><span class="pre">objgraph</span></tt></a></p>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="py-modindex.html" title="Python Module Index"
>modules</a></li>
<li class="right" >
<a href="objgraph.html" title="objgraph"
>next</a> |</li>
<li><a href="#">home</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2010, Marius Gedminas.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
</div>
</body>
</html>
|