<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jabbering Giraffe &#187; visualvm</title>
	<atom:link href="http://happygiraffe.net/blog/tag/visualvm/feed/" rel="self" type="application/rss+xml" />
	<link>http://happygiraffe.net/blog</link>
	<description></description>
	<lastBuildDate>Wed, 19 Oct 2011 10:40:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Heap Dump Analysis</title>
		<link>http://happygiraffe.net/blog/2009/04/02/heap-dump-analysis/</link>
		<comments>http://happygiraffe.net/blog/2009/04/02/heap-dump-analysis/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 08:30:40 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[visualvm]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1498</guid>
		<description><![CDATA[As part of the investigation into cocoon memory usage, I had to try and understand what was going on inside the JVM. The best way to do that is a heap dump. The general idea is to dump out the &#8230; <a href="http://happygiraffe.net/blog/2009/04/02/heap-dump-analysis/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As part of the investigation into cocoon memory usage, I had to try and understand what was going on inside the JVM.  The best way to do that is a heap dump.  The general idea is to dump out the contents of the JVM&#8217;s memory into a file, then analyse it using a tool.</p>
<p>First, getting a heap dump.  This used to be something of a pain, but Java 6 now includes the <a href="http://java.sun.com/javase/6/docs/technotes/tools/share/jmap.html">jmap -dump</a> command.  This allows you to (relatively) easily extract a heap dump from a running JVM.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #007800;">pid</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>some<span style="color: #000000; font-weight: bold;">/</span>where<span style="color: #000000; font-weight: bold;">/</span>pid.txt<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #007800;">jmap</span>=<span style="color: #007800;">$JAVA_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>jmap
  <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #007800;">$jmap</span> <span style="color: #660033;">-F</span> -dump:live,<span style="color: #007800;">format</span>=b,<span style="color: #007800;">file</span>=mla-post-cache-clear.hprof <span style="color: #007800;">$pid</span></pre></div></div>

<p>That takes a little while (a minute or two) to run, and ended up with a 500Mb file.  It&#8217;s best to bring it back to your workstation in order to look through.  Thankfully it compresses quite nicely.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #000000; font-weight: bold;">time</span> rsync <span style="color: #660033;">-avz</span> server:mla-post-cache-clear.hprof .
  receiving <span style="color: #c20cb9; font-weight: bold;">file</span> list ... <span style="color: #000000; font-weight: bold;">done</span>
  mla-post-cache-clear.hprof
&nbsp;
  sent <span style="color: #000000;">42</span> bytes  received <span style="color: #000000;">72988157</span> bytes  <span style="color: #000000;">3105880.81</span> bytes<span style="color: #000000; font-weight: bold;">/</span>sec
  total <span style="color: #c20cb9; font-weight: bold;">size</span> is <span style="color: #000000;">580115453</span>  speedup is <span style="color: #000000;">7.95</span>
  rsync <span style="color: #660033;">-avz</span> server:mla-post-cache-clear.hprof .  8.42s user 2.85s system <span style="color: #000000;">47</span><span style="color: #000000; font-weight: bold;">%</span> cpu <span style="color: #000000;">23.643</span> total</pre></div></div>

<p>Now things get interesting.  We can fire up <a href="https://visualvm.dev.java.net/">visualvm</a>.  Inside visualvm, do File &rarr; Load… and point it at the heap dump file we just obtained.  You should end up with something like this:</p>
<div style="text-align:center;"><img src="http://happygiraffe.net/blog/wp-content/uploads/2009/04/visualvm-heap-dump-summary.png" alt="VisualVM 1.1 heap dump summary" border="0" width="444" height="409" /></div>
<p>Now this is interesting, but not <em>that</em> useful.  If you click on the classes button, you see this:</p>
<div style="text-align:center;"><img src="http://happygiraffe.net/blog/wp-content/uploads/2009/04/visualvm-11-classes-in-heap-dump.png" alt="VisualVM 1.1 classes in heap dump" border="0" width="680" height="487" /></div>
<p>What&#8217;s really interesting about that is the <code>byte[]</code> line.  Those instances are only 2% of objects, but are taking up 67% of memory!  If you double click on that line, you get taken through to the instances view.</p>
<div style="text-align:center;"><img src="http://happygiraffe.net/blog/wp-content/uploads/2009/04/visualvm-11-instances-inheap-dump.png" alt="VisualVM 1.1 instances inheap dump" border="0" width="686" height="518" /></div>
<p>There are three panels here:</p>
<ol>
<li>The left hand view is a list of all the instances, ordered by size.  In this case the top instances are the same size, which is quite suspicious.</li>
<li>The top right hand view is a breakdown of the fields in the object.  Not that useful for a <code>byte[]</code>.</li>
<li>The bottom right hand view is the useful one — it lists all references to this instance.  Here it&#8217;s showing that thi <code>byte[]</code> is linked to a ByteArrayOutputStream.</li>
</ol>
<p>Now, you can expand the references to that <code>byte[]</code> to find out where it&#8217;s being referenced.  But there&#8217;s a really nifty trick.  If you right-click, you get an option to &#8220;Show Nearest GC Root&#8221;.  And this is what it shows (after a little while).</p>
<div style="text-align:center;"><img src="http://happygiraffe.net/blog/wp-content/uploads/2009/04/visualvm-11-nearest-gc-root.png" alt="VisualVM 1.1 nearest GC root" border="0" width="800" height="301" /></div>
<p>This isn&#8217;t the simplest thing to interpret.  But, it does show that the <a href="https://svn.apache.org/repos/asf/cocoon/trunk/subprojects/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/HttpServletResponseBufferingWrapper.java">HttpServletResponseBufferingWrapper</a> is holding the <code>byte[]</code> itself.  Whilst that servlet response is held by a <a href="http://svn.apache.org/repos/asf/cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/transformation/TraxTransformer.java">TraxTransformer</a>.  That sounds like a good place to begin investigating.  Why is TraxTransformer holding on to ServletResponses?</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2009/04/02/heap-dump-analysis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

