<?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>The Empty Way &#187; Java</title>
	<atom:link href="http://blog.emptyway.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.emptyway.com</link>
	<description>Programming, Web and all things Computer.</description>
	<lastBuildDate>Sat, 21 Nov 2009 23:06:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Measuring JVM performance</title>
		<link>http://blog.emptyway.com/2008/04/03/measuring-jvm-performance/</link>
		<comments>http://blog.emptyway.com/2008/04/03/measuring-jvm-performance/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 19:00:51 +0000</pubDate>
		<dc:creator>Vladimir Sizikov</dc:creator>
				<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.emptyway.com/2008/04/03/measuring-jvm-performance/</guid>
		<description><![CDATA[It was a &#8220;JVM performance week&#8221; in JRuby-land. It all started when some recent code changes is JRuby caused an interesting performance degradation with JDK6 on multi-core CPUs. See the original post to the [jvm-l] list and extended coverage of what happened on Charlie&#8217;s blog. So, while following the discussion and participating, I learned a [...]]]></description>
			<content:encoded><![CDATA[<p>It was a &#8220;JVM performance week&#8221; in JRuby-land. It all started when some recent code changes is JRuby caused an interesting performance degradation with JDK6 on multi-core CPUs. See the <a href="http://groups.google.com/group/jvm-languages/t/9dfdd23cfcc6f84f">original post</a> to the [jvm-l] list and extended coverage of what happened on <a href="http://headius.blogspot.com/2008/04/shared-data-considered-harmful.html">Charlie&#8217;s blog</a>. So, while following the discussion and participating, I learned a few new things and got some interesting additional info and links that I&#8217;d like to share:</p>
<ol>
<li>Let&#8217;s start with basics. John Rose posted a good starting guide for those who&#8217;d like to micro-benchmark things on JVM: <a href="http://wikis.sun.com/display/HotSpotInternals/MicroBenchmarks">So You Want to Write a Micro-Benchmark</a>.
<li><a href="http://wikis.sun.com/display/HotSpotInternals/PerformanceTechniques">Performance techniques used in the Hotspot JVM</a> wiki entry with the list of optimizations JVM does, sweet and to the point.
<li>Fundamental work by <i>Ulrich Drepper</i>: <a href="http://people.redhat.com/drepper/cpumemory.pdf">What Every Programmer Should Know About Memory</a>, a MUST read, but painful <img src='http://blog.emptyway.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
<li><a href="http://weblogs.java.net/blog/kohsuke/archive/2008/03/deep_dive_into.html">Deep dive into assembly code from Java</a> blog post by Kohsuke Kawaguchi, on how to print out the assembly code that JIT is producing.
<li><a href="http://wikis.sun.com/display/HotSpotInternals/PrintAssembly">Examining generated code</a> with OpenJDK 7, exciting stuff!
<li><a href="http://www.javaspecialists.eu/archive/Issue158.html">Polymorphism Performance Mysteries Explained</a>, the Issue #158 of The Java Specialists&#8217; Newsletter.
<li><a href="http://mailinator.blogspot.com/2008/03/how-fast-is-java-volatile-or-atomic-or.html">How fast is Java Volatile? or Atomic? or Synchronized?</a>, with nice charts and comparisons.
<li><a href="http://www-128.ibm.com/developerworks/java/library/j-jtp10185/">Escape analysis can help optimize synchronization</a> by Brian Goetz.</li>
</ol>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.emptyway.com/2008/04/03/measuring-jvm-performance/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Quick Search for Javadocs in Firefox</title>
		<link>http://blog.emptyway.com/2007/04/12/quick-search-for-javadocs-in-firefox/</link>
		<comments>http://blog.emptyway.com/2007/04/12/quick-search-for-javadocs-in-firefox/#comments</comments>
		<pubDate>Thu, 12 Apr 2007 09:24:42 +0000</pubDate>
		<dc:creator>Vladimir Sizikov</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.emptyway.com/2007/04/12/quick-search-for-javadocs-in-firefox/</guid>
		<description><![CDATA[Here&#8217;s a very neat trick to quickly open Javadoc documentation for particular Java class, in Firefox browser. I got it from Dmitri and been using it for years. Basically, it adds a &#8220;Quick Search&#8221; bookmark to Firefox, with shortcut to invoke it. So, open up your Firefox bookmarks and create a new one, with the [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a very neat trick to quickly open Javadoc documentation for particular Java class, in Firefox browser. I got it from Dmitri and been using it for years. Basically, it adds a &#8220;Quick Search&#8221; bookmark to Firefox, with shortcut to invoke it.</p>
<p>So, open up your Firefox bookmarks and create a new one, with the following info:</p>
<ul>
<li>Name: Quick Javadoc Search</li>
<li>Location: <a href="http://www.google.com/search?q=%s&amp;q=%22&amp;q=%20%28Java%202%20Platform%20SE%205.0%29%22%20site%3Ajava.sun.com&amp;btnI=I%27m%20Feeling%20Lucky">Copy This Link</a></li>
<li>Keyword: jd</li>
</ul>
<p>This is it. If you look at the link closer, you&#8217;ll notice that we just use Google search for &#8220;(Java Platform 5.0)&#8221; on java.sun.com site in &#8220;I&#8217;m feeling lucky&#8221; mode.</p>
<p>Now, any time you enter in the Firefox, for example,</p>
<blockquote><p>jd String</p></blockquote>
<p>it will get you to the javadocs for java.lang.String. <img src='http://blog.emptyway.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  In most cases you don&#8217;t even need to fully qualify the class name!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.emptyway.com/2007/04/12/quick-search-for-javadocs-in-firefox/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Comparison of Java ME unit testing frameworks</title>
		<link>http://blog.emptyway.com/2007/04/05/comparison-of-java-me-unit-testing-frameworks/</link>
		<comments>http://blog.emptyway.com/2007/04/05/comparison-of-java-me-unit-testing-frameworks/#comments</comments>
		<pubDate>Thu, 05 Apr 2007 08:39:10 +0000</pubDate>
		<dc:creator>Vladimir Sizikov</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[ME Framework]]></category>

		<guid isPermaLink="false">http://blog.emptyway.com/2007/04/05/comparison-of-java-me-unit-testing-frameworks/</guid>
		<description><![CDATA[With the recent ME Framework open sourcing, and with all those discussions on what ME Framework can do for application developers (as opposed to TCK writers), I took a look at the APIs for popular JUnit-like unit testing frameworks for Java ME, to see what we can do better in our ME Framework. Please note [...]]]></description>
			<content:encoded><![CDATA[<p class="entrytext">With the recent <a href="https://cqme.dev.java.net/framework.html" onclick="javascript:urchinTracker('/outbound/cqme.dev.java.net/framework.html');">ME Framework</a> open sourcing, and with all those discussions on what ME Framework can do for application developers (as opposed to <a href="http://en.wikipedia.org/wiki/Technology_Compatibility_Kit" onclick="javascript:urchinTracker('/outbound/en.wikipedia.org/wiki/Technology_Compatibility_Kit');">TCK</a> writers), I took a look at the APIs for popular JUnit-like unit testing frameworks for Java ME, to see what we can do better in our ME Framework.</p>
<p>Please note that this comparison is focused mostly on the API.</p>
<p>Popular Unit testing framework for Java ME:</p>
<ol>
<li><a href="#j2meunit">J2MEUnit</a></li>
<li><a href="#jmunit">JMUnit</a></li>
<li><a href="#sonyjunit">Sony Ericsson Mobile JUnit 1.0</a></li>
</ol>
<p>All three frameworks are close to the original <a href="http://junit.org/index.htm" onclick="javascript:urchinTracker('/outbound/junit.org/index.htm');">JUnit</a>, with adjustments needed to support the Java ME Platform. In all 3 cases, a test class should extend a base class from the appropriate framework. And all typical JUnit helper methods are provided to the subclasses, e.g.: assertTrue, assertFalse, assertEquals, etc. (J2MEUnit provides the bare minimum of such methods, leaving quite some of them out).</p>
<p>JUnit standard hook methods are also present: setUp() and tearDown(), to setup test environment and to tear it down after the test is done.</p>
<p>So, in all 3 cases, developers who are familiar with JUnit, can start writing new unit tests for Java ME with fewer problems, since they basically know the core API.</p>
<p>The differences can be seen in areas where support for Java ME was added (one of the major problems is the lack of reflection in Java ME, while the original JUnit relies heavily on it). While the tests themselves look pretty similar for all three frameworks, the way the test suites are constructed differs.</p>
<h3><a title="j2meunit" name="j2meunit"></a>1. <a href="http://j2meunit.sourceforge.net/doc.html" onclick="javascript:urchinTracker('/outbound/j2meunit.sourceforge.net/doc.html');">J2MEUnit </a></h3>
<p>With absence of reflection, J2MEUnit authors decided that test suites should be created manually, explicitly adding new tests to the test suite, like this:</p>
<pre>
  aSuite.addTest(
      new TestOne("testOne", new TestMethod() {
          public void run(TestCase tc) {
              ((TestOne) tc).testOne();
          }
      })
   );</pre>
<p>The name of the test provided in the constructor, and the functionality of the test is enclosed in the anonymous inner class that implements the TestMethod.</p>
<p>Pretty heavyweight and not that simple, if you ask me.</p>
<h3><a title="jmunit" name="jmunit"></a>2. <a href="http://sourceforge.net/projects/jmunit/" onclick="javascript:urchinTracker('/outbound/sourceforge.net/projects/jmunit/');">JMUnit</a></h3>
<p>JMUnit test class must call a two-parameters constructor of the base class, specifying the number of the “sub”-tests, and the name of the test.</p>
<pre>
public TemperatureConversionTest() {
    super(4,"TemperatureConversionTest");
}</pre>
<p>The test selection and execution is done via method test(int), that should be implemented by every test:</p>
<pre>
public void test(int testNumber) throws Throwable {
    switch(testNumber) {
        case 0:testfahrenheitToCelsius();break;
        case 1:testcelsiusToFahrenheit();break;
        case 2:testisHotter();break;
        case 3:testisCooler();break;
        default: break;
    }
}</pre>
<p>The base class will call this method with all int values, from 0 to number-of-tests.</p>
<p>In my opinion, this approach is less convoluted and more straightforward then J2MEUnit. JMUnit also provides better GUI on a device that shows the results.</p>
<p>JMUnit provides two versions, one is suitable for CLDC 1.0, and the another one is for CLDC 1.1.</p>
<p>Overall, JMUnit provides more extensive set of helper methods (asserts), with better GUI, and with more straightforward configuration.</p>
<p>Netbeans’ Mobility Pack has <a href="http://blogs.sun.com/lukas/entry/junit_test_support_changed_to" onclick="javascript:urchinTracker('/outbound/blogs.sun.com/lukas/entry/junit_test_support_changed_to');">switched from J2MEUnit to JMUnit recently</a>.</p>
<h3><a title="sonyjunit" name="sonyjunit"></a>3. <a href="http://developer.sonyericsson.com/site/global/newsandevents/latestnews/newsjuly06/p_mobile_juint1.0_javame_cldc.jsp" onclick="javascript:urchinTracker('/outbound/developer.sonyericsson.com/site/global/newsandevents/latestnews/newsjuly06/p_mobile_juint1.0_javame_cldc.jsp');">Sony Ericsson Mobile JUnit 1.0</a></h3>
<p>The folks from Sony Ericsson did an excellent job on this, providing good extension to JUnit for Java ME, and with <a href="http://developer.sonyericsson.com/getDocument.do?docId=87520" onclick="javascript:urchinTracker('/outbound/developer.sonyericsson.com/getDocument.do?docId=87520');">great documentation</a> <img src="http://blog.emptyway.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
<p>Sony Ericsson approach, it seems, was to provide experience as close to original JUnit as possible. In most cases, to port JUnit tests to Sony Ericsson unit tests is just a matter of replacing the base test class all the tests extend (from JUnit one to Sony Ericsson one.)</p>
<p>Couple of innovative things were done in this library. The absence of reflection is handled on Java SE side, where the tests are being analyzed and proper extra classes are created, so that developers are freed from manual work of specifying all their test names. Sony Ericsson’s version is clearly better here then the two alternatives (J2MEUnit and JMUnit).</p>
<p>The library also provides nice additions on top of typical JUnit API. For example, it not only provides setUp() method, but also setUp(MIDlet midlet), for those tests that would like to access the midlet that runs the tests.</p>
<p>The GUI on the device looks nice too, with tabbed interface for passed/failed tests. Sample project for WTK is also provided. And the basic code coverage can be calculated, which is an unexpected plus.</p>
<p>Integration with Ant, NetBeans and Eclipse is provided.</p>
<p>Overall, Sony Ericsson Mobile JUnit 1.0 is very well done and easily beats other alternatives, discussed above. They provide the best JUnit “emulation”, and resolve absence of reflection in innovative way simplifying developers life.</p>
<p>For additional information, take a look at the following article: “<a href="http://www.devx.com/wireless/Article/32540" onclick="javascript:urchinTracker('/outbound/www.devx.com/wireless/Article/32540');">JUnit Testing Using Java ME JUnit Frameworks</a>“.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.emptyway.com/2007/04/05/comparison-of-java-me-unit-testing-frameworks/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t always trust javadocs</title>
		<link>http://blog.emptyway.com/2007/04/04/dont-always-trust-javadocs/</link>
		<comments>http://blog.emptyway.com/2007/04/04/dont-always-trust-javadocs/#comments</comments>
		<pubDate>Wed, 04 Apr 2007 07:42:56 +0000</pubDate>
		<dc:creator>Vladimir Sizikov</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.emptyway.com/2007/04/04/dont-always-trust-javadocs/</guid>
		<description><![CDATA[I&#8217;ve been looking for ways to improve performance in some of my code, at least trying to grab all those &#8220;low-hanging&#8221; performance fruits, when you just change a couple of lines and the performance dramatically improves. With profilers like NetBeans Profiler, it&#8217;s almost too easy and doesn&#8217;t take much time. I&#8217;ve noticed that we use [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been looking for ways to improve performance in some of my code, at least trying to grab all those &#8220;low-hanging&#8221; performance fruits, when you just change a couple of lines and the performance dramatically improves. With profilers like <a href="http://profiler.netbeans.org/">NetBeans Profiler</a>, it&#8217;s almost too easy and doesn&#8217;t take much time.</p>
<p>I&#8217;ve noticed that we use <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html">StringTokenizers</a> here and there, mostly to parse quite big text files, while the javadoc for the StringTokenizer states:</p>
<blockquote><p> StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split  method of String or the java.util.regex package instead.</p></blockquote>
<p>OK, I thought, let&#8217;s switch to regular expressions and see, they are indeed more powerful and convenient to use. Well, yes, except for the fact that the new code runs typically 4-5 times slower than the original one. Come to think of it, the regexp-based version indeed should have been much slower when compared to very simple and straightforward Tokenizer.</p>
<p>So, the lesson for me here is to use String.split() for non-performance sensitive operations and to keep using good old StringTokenizer when the performance is critical. Oh, and not to trust everything I read in javadocs. <img src='http://blog.emptyway.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.emptyway.com/2007/04/04/dont-always-trust-javadocs/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Integrated NetBeans Profiler</title>
		<link>http://blog.emptyway.com/2007/04/02/integrated-netbeans-profiler/</link>
		<comments>http://blog.emptyway.com/2007/04/02/integrated-netbeans-profiler/#comments</comments>
		<pubDate>Mon, 02 Apr 2007 10:25:47 +0000</pubDate>
		<dc:creator>Vladimir Sizikov</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.emptyway.com/2007/04/02/integrated-netbeans-profiler/</guid>
		<description><![CDATA[A great move, now NetBeans Profiler is integrated into the latest Netbeans 6.0 milestone 8, and comes directly with NetBeans, no need to download and install it separately. I&#8217;ve been using the Profiler for months now, and can honestly say that the strong desire to buy a commercial profiler is no more. The latest NetBeans [...]]]></description>
			<content:encoded><![CDATA[<p>A great <a href="http://jroller.com/page/nbprofiler?entry=netbeans_profiler_6_0_milestone4">move</a>, now <a href="http://profiler.netbeans.org/">NetBeans Profiler </a>is integrated into the latest Netbeans 6.0 milestone 8, and comes directly with NetBeans, no need to download and install it separately. <img src='http://blog.emptyway.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I&#8217;ve been using the Profiler for months now, and can honestly say that the strong desire to buy a commercial profiler is no more. <img src='http://blog.emptyway.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  The latest NetBeans Profiler does all or almost all that I need, including performance analysis, memory analysis, heap dumps, compatibility with jhat, partial class instrumentation (no need to instrument the whole application).</p>
<p>The two missing in Profiler 5.5 features are now implemented, and work:</p>
<ol>
<li><a href="http://profiler.netbeans.org/docs/help/6.0/heapwalker.html">Heap Walker</a></li>
<li><a href="http://profiler.netbeans.org/docs/help/6.0/compare_snapshots.html">Compare Memory Snapshots</a></li>
</ol>
<p>I&#8217;ve been also quite surprised by performance of instrumented code. Yes, it&#8217;s slower than non-instrumented code, but the difference is not that big, so I can execute my apps under profiler and they run without major performance degradation.</p>
<p>Overall, the memory and performance analysis in Java has become much more easy and fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.emptyway.com/2007/04/02/integrated-netbeans-profiler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding Memory Leaks, Part 2: Links</title>
		<link>http://blog.emptyway.com/2007/04/02/finding-memory-leaks-part-2-links/</link>
		<comments>http://blog.emptyway.com/2007/04/02/finding-memory-leaks-part-2-links/#comments</comments>
		<pubDate>Mon, 02 Apr 2007 09:30:43 +0000</pubDate>
		<dc:creator>Vladimir Sizikov</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.emptyway.com/2007/04/02/finding-memory-leaks-part-2-links/</guid>
		<description><![CDATA[In addition to my previously posted small HOWTO on Finding Memory Leaks in Java, here&#8217;s a collection of various articles and blogs with some additional info on how to use standard JDK tools to detect and analyze memory leaks: Tracking Down Memory Leaks by Greg Sporar Plugging memory leaks with weak references by Brian Goetz [...]]]></description>
			<content:encoded><![CDATA[<p>In addition to my previously posted small <a href="http://blog.emptyway.com/2007/04/02/finding-memory-leaks-in-java-apps/">HOWTO on Finding Memory Leaks in Java</a>, here&#8217;s a collection of various  articles and blogs with some additional info on how to use standard JDK tools to detect and analyze memory leaks:</p>
<ul>
<li> <a href="http://weblogs.java.net/blog/gsporar/archive/2007/04/tracking_down_m.html">Tracking Down Memory Leaks</a> by Greg Sporar</li>
<li>
<a href="http://www-128.ibm.com/developerworks/java/library/j-jtp11225/index.html?ca=dgr-btw01PlugLeak">Plugging memory leaks with weak references</a> by Brian Goetz
</li>
<li> <a href="http://weblogs.java.net/blog/jfarcand/archive/2006/02/using_mustangs.html">Using Mustang&#8217;s jmap/jhat to profile GlassFish</a> by Jean-Francois Arcand</li>
<li> <a href="http://weblogs.java.net/blog/kellyohair/archive/2005/09/heap_dump_snaps.html">Heap Dump Snapshots</a> by Kelly O&#8217;Hair</li>
<li> <a href="http://blogs.sun.com/sundararajan/entry/what_s_in_my_java">What&#8217;s in my Java heap?</a> by A. Sundararajan</li>
<li><a href="http://blogs.sun.com/alanb/entry/heap_dumps_are_back_with">Heap dumps are back with a vengeance!</a> by Alan Bateman</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.emptyway.com/2007/04/02/finding-memory-leaks-part-2-links/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Finding Memory Leaks in Java Apps</title>
		<link>http://blog.emptyway.com/2007/04/02/finding-memory-leaks-in-java-apps/</link>
		<comments>http://blog.emptyway.com/2007/04/02/finding-memory-leaks-in-java-apps/#comments</comments>
		<pubDate>Mon, 02 Apr 2007 08:56:39 +0000</pubDate>
		<dc:creator>Vladimir Sizikov</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.emptyway.com/2007/04/02/finding-memory-leaks-in-java-apps/</guid>
		<description><![CDATA[Here is a small HOWTO on how to find memory leaks with Java SE. I&#8217;ve written it while trying to find memory leaks in our testing tools: JTHarness and ME Framework, and then wanted to share the HOWTO with the world, but I didn&#8217;t have my blog at that time, so I posted this info [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a small HOWTO on how to find memory leaks with Java SE.</p>
<p>I&#8217;ve written it while trying to find memory leaks in our testing tools: <a href="https://jtharness.dev.java.net/">JTHarness </a>and <a href="https://cqme.dev.java.net/framework.html">ME Framework</a>, and then wanted to share the HOWTO with the world, but I didn&#8217;t have my blog at that time, so I posted  this info as a comment to a relevant entry in the excellent Adam Bien&#8217;s <a href="http://www.adam-bien.com/roller/page/abien?entry=how_to_find_memory_leaks">blog</a>.</p>
<p><strong>Note:</strong> Use the latest JDK 6, because it has the latest tools, with lots of bug fixes and improvements. All the later examples assume that JDK6&#8242;s bin directory is in the PATH.</p>
<h3>Step 1. Start the application.</h3>
<p>Start the application as you usually do:</p>
<blockquote><p> java -jar java_app.jar</p></blockquote>
<p>Alternatively, you could start java with hprof agent. Java will run slower, but the huge benefit of this approach is that the stack traces for created objects will be available which improves memory leak analysis greatly:</p>
<pre> java
  -Dcom.sun.management.jmxremote
  -Dcom.sun.management.jmxremote.port=9000
  -Dcom.sun.management.jmxremote.authenticate=false
  -Dcom.sun.management.jmxremote.ssl=false
  -agentlib:hprof=heap=dump,file=/tmp/hprof.bin,
   format=b,depth=10
  -jar java_app.jar</pre>
<p>When the application is up, perform various actions that you think might lead to memory leaks.</p>
<p>For example, if you open some documents in your app, the memory graph could rapidly go up. If closing the docs and invocation of full garbage collection did not bring the memory back to normal level, there is probably a leak somewhere.You might use <code>jconsole</code> from JDK 6 to see the memory consumption graph to have a clue whether memory leak is present or not:</p>
<blockquote><p> jconsole</p></blockquote>
<p>It will pop up a dialog with a list of Java applications to connect to. Find the one with java_app.jar and connect. Also, jconsole allows to invoke full GC providing nice button just for that.</p>
<h3>Step 2. Find the application pid.</h3>
<p>Find out the application&#8217;s process id via:</p>
<blockquote><p> jps</p></blockquote>
<p>It will print something like:</p>
<blockquote><p> 15976 java_app.jar<br />
7586 startup.jar<br />
22476 Jps<br />
12248 Main<br />
5437 Bootstrap</p></blockquote>
<p>In our case the pid is 15976.</p>
<h3>Step 3. Dump the heap into file.</h3>
<p>Dump heap into the file:</p>
<blockquote><p> jmap -dump:format=b,file=/tmp/java_app-heap.bin 15976</p></blockquote>
<p>We just told jmap to dump the heap into /tmp/java_app-heap.bin file, in binary from (which is optimized to work with large heaps). The third parameter is the pid we found in Step 2.</p>
<p>Alternatively, if you started java with hprof agent, you could just use Ctrl-\ on Solaris/Linux or Ctrl-Break on Windows to dump heap into the file, specified in hprof agent arguments.</p>
<h3>Step 4. Visualize the heap.</h3>
<p>Use jhat tool to visualize the heap:</p>
<blockquote><p> jhat -J-Xmx326m /tmp/java_app-heap.bin</p></blockquote>
<p>Jhat will parse the heap dump and start a web server at port 7000. Connect to Jhat server by pointing your browser to:</p>
<blockquote><p> http://localhost:7000</p></blockquote>
<p>And start investigating. <img src='http://blog.emptyway.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Jhat allows you to see what objects are present in the heap, who has references to those objects, etc.</p>
<p>Here are some tips:</p>
<ul>
<li>Investigate _instances_, not _classes_.</li>
<li>Use the following URL to see the instances: http://localhost:7000/showInstanceCounts/</li>
<li>Use &#8220;Reference Chains from Rootset&#8221; (Exclude weak refs!!!) to see who&#8217;s holding the instance.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.emptyway.com/2007/04/02/finding-memory-leaks-in-java-apps/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>
