<?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>Xebia Blog &#187; OutOfMemoryError</title>
	<atom:link href="http://blog.xebia.com/tag/outofmemoryerror/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com</link>
	<description>Software development done right!</description>
	<lastBuildDate>Wed, 01 Feb 2012 00:30:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Loitering objects make web company lose money</title>
		<link>http://blog.xebia.com/2008/09/15/loitering-objects-make-web-company-lose-money/</link>
		<comments>http://blog.xebia.com/2008/09/15/loitering-objects-make-web-company-lose-money/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 12:32:00 +0000</pubDate>
		<dc:creator>Jeroen Borgers</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Continuity]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[HPJMeter]]></category>
		<category><![CDATA[jconsole]]></category>
		<category><![CDATA[OutOfMemoryError]]></category>
		<category><![CDATA[VisualVM]]></category>

	<!-- AutoMeta Start -->
	<category>great</category>
	<!-- AutoMeta End -->
	
		<guid isPermaLink="false">http://blog.xebia.com/?p=749</guid>
		<description><![CDATA[By Jeroen Borgers Recently, I was called in by a company with a website in trouble. And because they make all their money on-line, it was evident that they really wanted to have the issue solved. The day I came in, the site had gone down about 5 times the last 24 hours. Because of [...]]]></description>
			<content:encoded><![CDATA[<p>By Jeroen Borgers</p>
<p>Recently, I was called in by a company with a website in trouble. And because they make all their money on-line, it was evident that they really wanted to have the issue solved. The day I came in, the site had gone down about 5 times the last 24 hours. Because of this they got less traffic, which directly meant less revenue. The log files showed periods of long response times and OutOfMemoryErrors. Their questions were: Why do we get this behavior? and How do we fix it? My short answer turned out to be: because of too many loitering objects; and this can be fixed by not holding on to them in the HTTP session. <span id="more-749"></span>I’ll explain.</p>
<p><strong>Troubleshooting assignment</strong><br />
As often is the case in this kind of troubleshooting assignments, once you have found exactly what is wrong, you&#8217;ve almost solved it already. The difficulty is in doing the detective work of nailing down the root cause. Or differently put, finding the needle in the haystack. </p>
<p><strong>VisualVM</strong><br />
The tool they already used and actually was new to me was VisualVM.<br />
 <a href='http://blog.xebia.com/wp-content/uploads/2008/09/visualvm-about.jpg'><img src="http://blog.xebia.com/wp-content/uploads/2008/09/visualvm-about.jpg" alt="Visual VM About window" title="visualvm-about" width="500" height="312" class="alignnone size-full wp-image-754" /></a><br />
Figure 1. VisualVM About window</p>
<p>It is similar to jdk&#8217;s jconsole in that you can visually look into the JVM. It actually has a plug-in architecture by which for instance parts of jconsole are plugged into visualVm to see heap and thread usage, loaded classes and MBeans. For lower overhead than jconsole, it however has less detail in sections like heap. It also has a profiler plug-in which is the Netbeans profiler. So you can actually CPU or memory profile local applications from it. And since the recent version 1.6.0_07 the JDK has this great tool bundled with it now. In case you cannot find it in the JDK: they have renamed it to jvisualvm.</p>
<p><a href='http://blog.xebia.com/wp-content/uploads/2008/09/visualvm-heap.jpg'><img src="http://blog.xebia.com/wp-content/uploads/2008/09/visualvm-heap.jpg" alt="VisualVM monitor example showing analysis of a JMeter process" title="visualvm-heap" width="500" height="312" class="alignnone size-full wp-image-751" /></a><br />
Figure 2. VisualVM monitor example </p>
<p>VisualVM helped in that we could see in the generated stack dumps that threads were often waiting for some log4j lock. Reason being the log level was set to DEBUG. Setting it to INFO improved performance somewhat. However, in this case visualVM did not bring me the crucial facts. Reason for this was twofold. The first was the lack of detail I mentioned and the second was the type of tool, being one having to be a witness of the problem situation to be able to analyze it. </p>
<p><strong>Finding the crucial information</strong><br />
The toolset that gave me the first half of the crucial information was the -Xloggc:gc.log flag combined with HPJmeter. This gives the opportunity to analyze after the facts. That night, traffic was rather low and the site did not go down, but it was very slow during some hours. The next day, the log file showed at the time when performance became bad:<br />
<code>15045.703: [GC 1786342K->1481712K(1856512K), 0.1639990 secs]<br />
15053.886: [GC 1790320K->1489985K(1858880K), 0.1722250 secs]<br />
15065.930: [GC 1800257K->1493876K(1859968K), 0.1601460 secs]<br />
15066.090: [Full GC 1493876K->1467212K(1859968K), 3.5031310 secs]<br />
15071.966: [Full GC 1708415K->1471029K(1859968K), 3.5039160 secs]<br />
15078.215: [Full GC 1708277K->1476287K(1859968K), 3.5326720 secs]<br />
15087.847: [Full GC 1708386K->1408698K(1859968K), 4.5038550 secs]<br />
</code><br />
We see here the fall-over from doing mainly GC&#8217;s (young space) to mainly Full GC&#8217;s, so from short pauses to long pauses. Actually every 6 seconds or so, and a collection takes 3.5 s. So a lot of time is spent in gc. As you can see, however, some memory is still reclaimed. With HPJMeter I could see this in nice graphics to draw conclusions:<br />
<a href='http://blog.xebia.com/wp-content/uploads/2008/09/hpjmeter-gctime.jpg'><img src="http://blog.xebia.com/wp-content/uploads/2008/09/hpjmeter-gctime.jpg" alt="HPJmeter showing percentage of time spent in GC." title="hpjmeter-gctime" width="500" height="312" class="alignnone size-full wp-image-750" /></a><br />
Figure 3. HPJmeter showing percentage of time spent in GC.</p>
<p>So, it became clear to me that the site was so slow because up to 95% of the CPU cycles were spent on garbage collection, which left only 5% to the application itself. </p>
<p><strong>Need for more details</strong><br />
What however the reason for this excessive gc was, was not clear to me, yet. I needed more details on the memory usage. So, we switched on the -XX:+PrintGCDetails flag which gave us the second half of the crucial information.<br />
Unfortunately, HPJMeter could not deal with the produced log format, but we got lines at the start of the problems like:<br />
<code>21270.421: [GC [PSYoungGen: 436938K->112249K(481664K)] 1808785K->1489945K(1879808K), 0.1790760 secs]<br />
21277.559: [GC [PSYoungGen: 443385K->111206K(486528K)] 1821081K->1497781K(1884672K), 0.1815330 secs]<br />
21284.659: [GC [PSYoungGen: 442406K->107401K(489536K)] 1828981K->1504035K(1887680K), 0.1780120 secs]<br />
21284.837: [Full GC [PSYoungGen: 107401K->79553K(489536K)] [PSOldGen: 1396634K->1398143K(1398144K)] 1504035K->1477697K(1887680K) [PSPermGen: 79038K->79038K(262144K)], 3.5320210 secs]<br />
21292.969: [Full GC [PSYoungGen: 331200K->83102K(489536K)] [PSOldGen: 1398143K->1398143K(1398144K)] 1729343K->1481246K(1887680K) [PSPermGen: 79047K->79047K(262144K)], 3.5302490 secs]<br />
21302.663: [Full GC [PSYoungGen: 331200K->85665K(489536K)] [PSOldGen: 1398143K->1398143K(1398144K)] 1729343K->1483809K(1887680K) [PSPermGen: 79055K->79055K(262144K)], 3.4508870 secs]<br />
21311.177: [Full GC [PSYoungGen: 330681K->21303K(489536K)] [PSOldGen: 1398143K->1398144K(1398144K)] 1728825K->1419447K(1887680K) [PSPermGen: 79065K->79027K(262144K)], 4.4884740 secs]</code><br />
Again, here we see the fall-over. If you look at the old space: the first full gc puts objects from young in old making it almost full, there is only 1K or less of unused space left shown in the full GC lines. The confusion comes from the young space usage, where most garbage still can be collected. This makes the total heap not full, while the old space is at its limits. So, some things get stuck in old space. The remarkable thing is that the situation recovers after an hour or so and an OOME does not occur. </p>
<p><strong>Heap analyses with Netbeans</strong><br />
So, now it is time to do more heap analyses. Netbeans has a great built-in profiler with a useful heap analyzer. We found that most of the memory was used by Category objects, which was not intended as such. By using the heap walker we could find that references to it were mostly held from Tomcat session objects. So, each HTTP session had its own copy of these Categories and they were kept hanging around for half an hour: the default HTTP session timeout. The term for this kind of objects is: loitering objects.</p>
<p><strong>The quick and the real fix</strong><br />
At this point we could devise a quick remedy: decrease the HTTP session timeout from 30 to 5 minutes. This reduced most pressure on us fixers and gave us some air to breath. Then we created some tests and were able to reproduce the behavior in a test environment. Looking at jconsole also clearly showed the growing usage of old space.<br />
<a href='http://blog.xebia.com/wp-content/uploads/2008/09/jconsole-mem-pools.jpg'><img src="http://blog.xebia.com/wp-content/uploads/2008/09/jconsole-mem-pools.jpg" alt="JConsole showing memory usage for each memory pool" title="jconsole-mem-pools" width="250" height="156" class="alignleft size-full wp-image-755" /></a><br />
Figure 4. JConsole showing memory usage for each memory pool.</p>
<p>In the source code we could easily find the place where all category objects were loaded from the database, put in a cache once and, along the way, also a copy in each user HTTP session. For no reason really. And with 1700 user sessions this took most of the memory.</p>
<p>So, this was a no-brainer to solve in the code. And after deploying and setting back the session timeout to the default 30 minutes, problems were solved. I love it when a plan comes together.</p>
<p><strong>Learn more</strong><br />
If you want to learn more about this stuff and get hands-on experience, come to <a href="http://www.xebia.com/nl/service-offering/training-services/Speeding-up-Java-Applications.html">my class of &#8220;Speeding up Java applications&#8221; in April or November 2009</a>.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin-left: 10px;"><g:plusone size="small" count="1" href="http://blog.xebia.com/2008/09/15/loitering-objects-make-web-company-lose-money/"></g:plusone></div><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.xebia.com%2F2008%2F09%2F15%2Floitering-objects-make-web-company-lose-money%2F&amp;title=Loitering%20objects%20make%20web%20company%20lose%20money" id="wpa2a_2"><img src="http://blog.xebia.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.xebia.com/2008/09/15/loitering-objects-make-web-company-lose-money/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Beware of transitive dependencies&#8230; For they can be old and leaky</title>
		<link>http://blog.xebia.com/2008/09/15/beware-of-transitive-dependencies-for-they-can-be-old-and-leaky/</link>
		<comments>http://blog.xebia.com/2008/09/15/beware-of-transitive-dependencies-for-they-can-be-old-and-leaky/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 11:54:15 +0000</pubDate>
		<dc:creator>Jeroen van Erp</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[eclipse memory analyzer]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[maven2]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[OutOfMemoryError]]></category>
		<category><![CDATA[XML]]></category>

	<!-- AutoMeta Start -->
	<category></category>
	<!-- AutoMeta End -->
	
		<guid isPermaLink="false">http://blog.xebia.com/?p=746</guid>
		<description><![CDATA[In many JEE apps today, you almost cannot forgo XML. Whether it is in configuration, data structures or service interfaces, you will certainly use a number of XML files. In a recent project we had to deal with a number of external services which used an XML interface. Little did we know that we introduced [...]]]></description>
			<content:encoded><![CDATA[<p>In many JEE apps today, you almost cannot forgo XML. Whether it is in configuration, data structures or service interfaces, you will certainly use a number of XML files. In a recent project we had to deal with a number of external services which used an XML interface. Little did we know that we introduced a potential time-bomb in our application&#8230;<span id="more-746"></span></p>
<p>Each of the accessed XML services in our application provided data for the same domain. So instead of writing or generating a great number of Java classes, we decided to use XSLT to transform the XML into a common format we could easily unmarshall. During performance and stress tests, this solution performed very well and showed no signs of degradation.</p>
<p>However&#8230;. Once we hit production, the application went OutOfMemory after a few days on a heap of 1GB. This was bad, how come our performance and stress tests didn&#8217;t show this? We decided to do the only sensible thing, and create a heapdump as soon as the application showed signs of stress. We started monitoring the java process using the jstat utility found in the JDK.</p>
<pre>$ jstat -gcutil &lt;processid&gt; 1s 10000
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00  99.98 100.00  26.66   2896  119.886  6000 32234.945 32354.831
  0.00   0.00 100.00 100.00  26.66   2896  119.886  6001 32234.945 32354.831
  0.00   0.00 100.00 100.00  26.66   2896  119.886  6001 32234.945 32354.831
  0.00   0.00 100.00 100.00  26.66   2896  119.886  6001 32234.945 32354.831
  0.00   0.00 100.00 100.00  26.66   2896  119.886  6001 32234.945 32354.831
  0.00   0.00 100.00 100.00  26.66   2896  119.886  6001 32234.945 32354.831
  0.00   0.00 100.00 100.00  26.66   2896  119.886  6001 32234.945 32354.831
  0.00   0.00 100.00 100.00  26.66   2896  119.886  6002 32240.277 32360.163
 </pre>
<p>As soon as we saw the old space utilization hitting 100% and the Full Garbage Collector being unable to clean things up, we created a heapdump</p>
<pre>$ jmap -dump:format=b,file=/tmp/heap.hprof &lt;processid&gt;</pre>
<p>Using the <a href="http://www.eclipse.org/mat/" title="Eclipse Memory Analyzer">Eclipse Memory Analyzer</a> we looked into the heap dump, to see what was causing our memory to grow. This generated the following picture.</p>
<p><a href='http://blog.xebia.com/wp-content/uploads/2008/09/picture-2.png'><img src="http://blog.xebia.com/wp-content/uploads/2008/09/picture-2-300x269.png" alt="" title="Eclipse-MemoryAnalyzer" width="300" height="269" class="alignnone size-medium wp-image-747" /></a></p>
<p>Clicking on the &#8220;Leak Hunter&#8221; link in the analyzer, quickly showed the possible culprit. The XMLReaderManager class from Xalan somehow had taken up a big chunk of available memory.</p>
<p><a href='http://blog.xebia.com/wp-content/uploads/2008/09/picture-3.png'><img src="http://blog.xebia.com/wp-content/uploads/2008/09/picture-3-300x269.png" alt="" title="Eclipse-LeakHunter" width="300" height="269" class="alignnone size-medium wp-image-748" /></a></p>
<p>Searching through our Maven2 pom files showed no explicit dependency upon Xalan, but the dependency graph in the generated maven site showed it as a transitive dependency. One of our dependencies had xalan-2.6.0 noted down as a dependency. Googling around for this quickly showed us that there were two bugs against xalan, which described this exact problem: <a href="https://issues.apache.org/jira/browse/XALANJ-2178" title="XALANJ-2178">XALANJ-2178</a> and <a href="https://issues.apache.org/jira/browse/XALANJ-2195" title="XALANJ-2195">XALANJ-2195</a>.</p>
<p>Looking at the source code for Xalan 2.6.0 we could indeed clearly notice the memory leak, whereas in 2.7.0 they updated it to prevent the class from keeping references around for too long. But why didn&#8217;t our stress tests on a similar machine and configuration show this?</p>
<p>The answer for this can also be found in the XMLReaderManager. It tries to cache the XMLReader in a ThreadLocal, so that it can give the same XMLReader to the Thread if it requests a new one. However in a JEE system, you normally have threadpools which scale up and down depending on system load at a given time. In our stress tests, we only simulated full loads for a prolonged time, and with this load, the XMLReaders weren&#8217;t leaked, as threads were being reused from the ThreadPool. If we had simulated a more varying load over a larger amount of time, we would have seen this occurring before we hit production.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin-left: 10px;"><g:plusone size="small" count="1" href="http://blog.xebia.com/2008/09/15/beware-of-transitive-dependencies-for-they-can-be-old-and-leaky/"></g:plusone></div><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.xebia.com%2F2008%2F09%2F15%2Fbeware-of-transitive-dependencies-for-they-can-be-old-and-leaky%2F&amp;title=Beware%20of%20transitive%20dependencies%26%238230%3B%20For%20they%20can%20be%20old%20and%20leaky" id="wpa2a_4"><img src="http://blog.xebia.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.xebia.com/2008/09/15/beware-of-transitive-dependencies-for-they-can-be-old-and-leaky/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  blog.xebia.com/tag/outofmemoryerror/feed/ ) in 0.56829 seconds, on Feb 9th, 2012 at 5:51 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 9th, 2012 at 6:51 pm UTC -->
