<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ludvig Westerdahl on Medium]]></title>
        <description><![CDATA[Stories by Ludvig Westerdahl on Medium]]></description>
        <link>https://medium.com/@ludvigwesterdahl?source=rss-2c60fee49e55------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*I32K5AUUfyKYYIjFseAqog.jpeg</url>
            <title>Stories by Ludvig Westerdahl on Medium</title>
            <link>https://medium.com/@ludvigwesterdahl?source=rss-2c60fee49e55------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 23:01:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ludvigwesterdahl/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making Regex your friend in Java]]></title>
            <link>https://medium.com/javarevisited/making-regex-your-friend-in-java-ddc5bf7f9a66?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/ddc5bf7f9a66</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[regex]]></category>
            <category><![CDATA[regular-expressions]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Sun, 19 Sep 2021 14:34:17 GMT</pubDate>
            <atom:updated>2021-09-21T15:46:14.574Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever seen this meme and sort of agree with it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*FPnGZxVx3-_xLGV4uRHK5A.jpeg" /><figcaption>Regex meme</figcaption></figure><p>Well, then you’re like me. And I for one would like to change that. This article aims to solve that and make it into another tool available in your developer arsenal. I believe it can definitely be over-used, however, there are situations where it is called for.</p><p>Let’s dive into it.</p><h3>Regex constructs</h3><p>First, let’s talk about some constructs that are helpful in constructing your <a href="https://medium.com/javarevisited/7-best-regular-expression-courses-for-developers-to-learn-in-2021-9b8cb37bb3a5">regular expression</a>. Note that these relate to Java and I believe they can differ across languages.</p><h4>Brackets “[]” vs parenthesis “()”</h4><p>Brackets is called a <strong>character class</strong> while parenthesis is referred to as a <strong>capturing group </strong>[1].</p><p>A character class is a way to say which characters you want to match. For instance.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffc4c1a7e3e43195724127eac0c683e4/href">https://medium.com/media/ffc4c1a7e3e43195724127eac0c683e4/href</a></iframe><p>Here the output will be the following.</p><pre>true<br>true<br>false<br>false</pre><p>A capturing group is something completely different. This is a way for you to say that you want your <a href="https://javarevisited.blogspot.com/2016/10/how-to-check-if-string-is-numeric-in-Java.html">regex </a>to save what it found so that you can retrieve it later. In other words, you’re telling the regex to “match against my regex, but save these sub-results for me”. Let’s look at an example.</p><p>Here we are trying to match a string of the format: DIGIT+DIGIT=DIGIT. And we want to extract the digits.</p><p>The first pattern uses only character classes, “+” (which is escaped because it has another meaning in regex) and “=”. The second pattern uses unnamed capturing groups. The final and third pattern uses named capturing groups.</p><p>Note the syntax for named capturing groups: (?&lt;NAME&gt;pattern)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/612e2c7b583cfc42f291028ac135bd5c/href">https://medium.com/media/612e2c7b583cfc42f291028ac135bd5c/href</a></iframe><p>The output from this is the following.</p><pre>Number of groups: 0<br>true<br>Number of groups: 3<br>true<br>3+1=4<br>3<br>1<br>4<br>Number of groups: 3<br>true<br>3+1=4<br>3<br>1<br>4<br>3<br>1<br>4</pre><p>This is a good time to discuss the differences between <strong>matches</strong> and <strong>find</strong>.</p><h4>Matches vs Find</h4><p>Essentially there are two ways you can use your regex. One is to <strong>match</strong> against the whole string. And another use is to <strong>find</strong> sub-strings.</p><p>Let’s use the previous example of trying to get the digits from strings. But in this case, we want to simply get digits, we don’t really care about the general format of the string. In that case, we can use <strong>named capturing groups</strong> again but with the <strong>find</strong> method instead. Like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/deb229d40bc7ae5ca0226ba4b16ab99f/href">https://medium.com/media/deb229d40bc7ae5ca0226ba4b16ab99f/href</a></iframe><p>And the output from this is the following.</p><pre>Number of groups: 1<br>3<br>3<br>3<br>1<br>1<br>1<br>4<br>4<br>4</pre><p>We can note that the first group number 0 is always the whole match from the original pattern. And the number of groups is basically 1 + defined groups.</p><p>This example simply finds digits in a string with no other considerations basically. Therefore, a simple rule is to use <strong>find </strong>if you are looking for a specific substring and <strong>matches </strong>if you want to ensure a specific format of a string.</p><h4>Quantifiers</h4><p>So far I’ve only shown examples with matching single occurrences. But it’s helpful to sometimes be able to specify a repeat occurrence. There are actually three types of quantifiers, <strong>greedy</strong>, <strong>reluctant</strong> and <strong>possessive</strong>.</p><p>I will only touch on <strong>greedy </strong>in this article but feel free to check up on the other two if needed. Let’s say you are interested in finding digit sequences. Let’s say we want to generalise our previous example of “DIGIT+DIGIT=DIGIT” to allow for numbers such that “100+50=150” will also work.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/409e16b16bb5ccc0660e384828775122/href">https://medium.com/media/409e16b16bb5ccc0660e384828775122/href</a></iframe><p>I have also used a pre-defined character class “\d” which simply means a digit, you can find some more pre-defined classes <a href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/regex/Pattern.html">here</a> [1].</p><p>The meaning of “\\d{1,}” means that there should be at least one digit. You can give it another argument such that you allow for a maximum 3 for instance like so: “\\d{1,3}.</p><h4>“OR” operator</h4><p>This operator can be helpful if you need a conditional match. Let’s say we have strings like “Rating 5”, “Score 2”, “Scores 3”, and “Point 9”. And we need to make sure that the format is correct while extracting the digit. To do this we can use the <strong>or </strong>operator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f92c0672fb191f7c4a1c48eb7afb482/href">https://medium.com/media/9f92c0672fb191f7c4a1c48eb7afb482/href</a></iframe><p>The output here is as expected.</p><pre>true<br>Rating 5<br>5<br>true<br>Score 2<br>2<br>true<br>Scores 3<br>3<br>true<br>Point 9<br>9<br>false</pre><p>I added the “s{0,1}” to show that you can make the expressions inside the OR as complex as you like.</p><h4>Back reference</h4><p>Let’s say you get some strings like the following.</p><ul><li>Apple: 3 Apples</li><li>Orange: 4 Oranges</li><li>Banana: 1 Banana</li></ul><p>Essentially you want to match something like: “WORD: DIGIT WORD(s)”</p><p>For this you would need to be able to reference the match inside the pattern, this is done using <strong>back-references </strong>like so<strong>.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/390f982e584b795076ba147b804f2600/href">https://medium.com/media/390f982e584b795076ba147b804f2600/href</a></iframe><p>You can see that I have referenced the named capturing group “fruit” at the end of the pattern using the syntax “\\k&lt;NAME&gt;”.</p><p>That’s the final item for this article. I hope these tools can give you some more confidence when using <a href="https://javarevisited.blogspot.com/2020/04/top-5-courses-to-learn-regular-expression-regex.html">regular expressions in Java</a>. I want to end the article with a note on performance and a tip when using regex in the wild.</p><h3>Compiling Patterns</h3><p>Using the matches function on <a href="https://javarevisited.blogspot.com/2016/02/2-ways-to-split-string-with-dot-in-java-using-regular-expression.html">Pattern </a>(see the first example) actually compiles a regex and matches it in the background. The other examples used the compile method on Pattern explicitly. This function is quite expensive which I can demonstrate below.</p><p>So you want to reuse it across method calls, perhaps set it as a <a href="http://javarevisited.blogspot.sg/2017/01/how-public-static-final-variable-works.html#axzz51iekrZ00">static final field</a>.</p><p>I will reuse the last example with the fruits and do some <a href="https://medium.com/javarevisited/7-best-courses-to-learn-refactoring-and-clean-coding-in-java-47bea3c67006">refactoring</a> into methods. See the example below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c92bdbee2778ee35be0a58c62afd8222/href">https://medium.com/media/c92bdbee2778ee35be0a58c62afd8222/href</a></iframe><p>Here are the results, so on my machine the difference is substantial. You should probably get a similar results.</p><pre>+------------------------------------+-----------------------------+<br>|           compileAlways            |            787 ms           |<br>+------------------------------------+-----------------------------+<br>|            compileOnce             |            105 ms           |<br>+------------------------------------+-----------------------------+</pre><p>When doing microbenchmarks in Java you need to be careful because of how the compiler works and all the optimizations that it performs. Therefore it is usually recommended to do a warm-up as you can see that I’ve done in the first loop.</p><p>I have talked about this in previous articles such as when I compared <a href="https://www.java67.com/2012/12/difference-between-arraylist-vs-LinkedList-java.html">LinkedList to ArrayList</a> and my top tips when doing micro benchmarking in Java, see the links below.</p><ul><li><a href="https://medium.com/javarevisited/consider-linkedlist-in-java-2fed1b945b48">Some thoughts on using LinkedList in Java</a></li><li><a href="https://ludvigwesterdahl.medium.com/some-tips-for-microbenchmarking-in-java-46d6c9b669d9">Some tips for Microbenchmarking in Java</a></li></ul><h4>References</h4><p>[1] Pattern (Java SE 13 &amp; JDK 13)<br><a href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/regex/Pattern.html">https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/regex/Pattern.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ddc5bf7f9a66" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/making-regex-your-friend-in-java-ddc5bf7f9a66">Making Regex your friend in Java</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Succeed by avoiding being dumb]]></title>
            <link>https://ludvigwesterdahl.medium.com/succeed-by-avoiding-being-dumb-1b6e2a7493d7?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b6e2a7493d7</guid>
            <category><![CDATA[self-help]]></category>
            <category><![CDATA[success]]></category>
            <category><![CDATA[investing]]></category>
            <category><![CDATA[software]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Sat, 18 Sep 2021 15:21:56 GMT</pubDate>
            <atom:updated>2021-09-18T15:21:56.740Z</atom:updated>
            <content:encoded><![CDATA[<p>How do you become a better software engineer? A better investor? Spouse? Friend?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-b9P8f4fQxb1wqGJ" /><figcaption>Photo by <a href="https://unsplash.com/@davidgaviphoto?utm_source=medium&amp;utm_medium=referral">David Gavi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>These are difficult questions. And if you google them, I’m sure there are thousands of different tips. Like, find a mentor, improve your communication, do what the best and brightest does, etc.</p><p>Seems like a lot of work. So what’s the alternative?</p><p>Charlie Munger once said that;</p><blockquote>“All I want to know is where I’m going to die so I’ll never go there.“</blockquote><p>In other words, instead of trying to figure out how to live, avoiding death is easier. Similar to how some math proofs work. Sometimes proving that something leads to a contradiction, rather than proving the initial statement, is easier. In other words, instead of doing the difficult things, find and do the easy alternatives. It will take you pretty far.</p><p>He is also famous for another quote.</p><blockquote>“It is remarkable how how much long-term advantage people like us have gotten by trying to be consistently not stupid, instead of trying to be very intelligent”</blockquote><p>It has some similarities to the first quote in that they both look at a problem from the opposite angle. Let’s take the case of how to be a better spouse. Do you know? I know I couldn’t answer that. But, how can you be a really shitty one? You cheat. Lie. Humiliate. Being dismissive. Then, what if your personal rule or guide was to simply avoid that?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*81Yref7WJYbb2ESqF_HKkA.jpeg" /><figcaption>Charlie Munger and Warren Buffett.</figcaption></figure><p>I don’t think it’s a coincidence that doctors generally abide by the principle of “do no harm”. And not something like “maximise life”. Obviously one would hope the doctor did more than simply “no harm”. But, maybe not the most difficult thing.</p><p>In addition, relating this to software where, in a nutshell, it is known that juniors write complex code and seniors write dumb code. In a way, these two principles of doing the easy thing and avoiding the dumb thing has been applied here.</p><p>So how do you write good software? I’m not sure I know.</p><p>But, maybe, we can start by not writing bad software.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b6e2a7493d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When constructors aren’t doing it for you, use a Builder]]></title>
            <link>https://medium.com/javarevisited/when-constructors-arent-doing-it-for-you-use-a-builder-11559fa41e39?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/11559fa41e39</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[java-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Sat, 04 Sep 2021 15:36:00 GMT</pubDate>
            <atom:updated>2021-09-28T15:56:03.927Z</atom:updated>
            <content:encoded><![CDATA[<p>I believe it was around 2016 when I first read the book <a href="https://www.oreilly.com/library/view/effective-java/9780134686097/">Effective Java</a> and I was hooked [1]. It also happened to be one of the first programming books I read and I haven’t found one that provide as much value as that one did ever since. In this article I wanted to talk about one technique from the book that I find very useful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jZegOe8Xl2XpBtMn" /><figcaption>Photo by <a href="https://unsplash.com/@toluobde?utm_source=medium&amp;utm_medium=referral">Tolu Olubode</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>TL;DR<br></strong>Consider using the <a href="http://javarevisited.blogspot.sg/2012/06/builder-design-pattern-in-java-example.html">builder pattern</a> when you feel the need to create multiple or long constructors. It will simplify your code, increase readability and allow for immutability.</p><h4>When to use it?</h4><p>If you need to create an object that has many possible parameters, combinations, and so forth, then this pattern is quite useful. Read on to find out how it works.</p><h4>Basic idea</h4><p>Imagine we want to model a <strong>tax return statement</strong>. Just think about the number of fields we can have here. The straightforward way would be to create a constructor, see below for the example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/097375ea1c0a6cfb0095206a659c244e/href">https://medium.com/media/097375ea1c0a6cfb0095206a659c244e/href</a></iframe><p>So here we have a long constructor and when instantiated would be hard to read and understand which argument corresponds to what value. It’s also problematic if we want to add a new field later on. Or if we want to model different combinations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*Ut1b3xHrB_KcD1aFtJSmZA.jpeg" /></figure><p>There are obviously other ways to do this, perhaps using multiple constructors, or setters, or container objects. But let me show you how this can be done using a <strong>builder</strong>.</p><h4>Determine mandatory fields</h4><p>One thing we can note about this example is that most people will probably just need to specify income from labor and their age. So let’s say that those two fields are mandatory and that the rest are optional. This means that we want labor income and a person&#39;s age in the constructor, but the other fields should be specified after.</p><h4>Immutability gain</h4><p>Now, we don’t want to use setters, why? Because that means that we can have objects that are not yet fully constructed (semantically) which is problematic when it comes to <a href="https://medium.com/javarevisited/6-multithreading-and-concurrency-books-every-java-programmer-should-read-b6a08d2aae54">multi-threading</a> for instance. Therefore we want to enforce <em>immutability</em> for our tax return.</p><h4>Using the Builder pattern</h4><p>The builder in this case would look something like this.</p><p>I have removed some fields for brevity, but keep in mind the problem we are solving here as mentioned above.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38f43ff7a5420e2ef720dddaea2cd96a/href">https://medium.com/media/38f43ff7a5420e2ef720dddaea2cd96a/href</a></iframe><p>What can you note about this example?</p><ul><li>The tax return has only final fields where the optional values have been specified in the builder.</li><li>It is very easy to create any type of combination of complicated tax returns.</li><li>We can also add new fields without breaking current users.</li><li>The tax return cannot be created without using a builder due to having a private constructor.</li></ul><p>It also makes the code easy to read since all the special fields now have names.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcb5f431c5a0ac5070fc5178013df45e/href">https://medium.com/media/bcb5f431c5a0ac5070fc5178013df45e/href</a></iframe><p>I considered ending the article here but decided to talk a little bit about another simple yet quite powerful technique.</p><h4>Static factory method</h4><p>As previously mentioned, constructors have some disadvantages such as not having names. Using a <a href="http://javarevisited.blogspot.sg/2017/02/5-difference-between-constructor-and-factory-method-in-java.html#axzz4tUeeQOAU"><em>static factory method</em></a> we can work around this and make our code even more readable.</p><p>Given our assumption that most people only need to report their labor income and age we could use a static factory to help like the following example.</p><figure><a href="http://javarevisited.blogspot.sg/2017/02/5-difference-between-constructor-and-factory-method-in-java.html#axzz4tUeeQOAU"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XN5XBnhaPgihFq7Nd0mxcg.png" /></a><figcaption>Static factory method example.</figcaption></figure><p>This is very contrived but it is useful because it allows to to essentially name a constructor. It also allows you to have <a href="https://javarevisited.blogspot.com/2012/12/what-is-constructor-in-java-example-chainning-overloading.html">multiple constructors with the same type argument</a>. In other words, the following example won’t compile.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec1e834487d56e3000842ef0bc7db5f9/href">https://medium.com/media/ec1e834487d56e3000842ef0bc7db5f9/href</a></iframe><p>But this will.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51b27eb120a088776bd0767a32e72d33/href">https://medium.com/media/51b27eb120a088776bd0767a32e72d33/href</a></iframe><p>We can also return the same instance over and over or perhaps a subclass. I believe they are more flexible compared to constructors, however, they might not be as easy to find in the code due to them being a method so that’s something to think about when naming them.</p><p><strong>References</strong></p><p>[1] Effective Java by Joshua Bloch<br><a href="https://www.oreilly.com/library/view/effective-java/9780134686097/">https://www.oreilly.com/library/view/effective-java/9780134686097/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11559fa41e39" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/when-constructors-arent-doing-it-for-you-use-a-builder-11559fa41e39">When constructors aren’t doing it for you, use a Builder</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Back to the basics of Java — Part 2: The JAR]]></title>
            <link>https://medium.com/javarevisited/back-to-the-basics-of-java-part-2-the-jar-6e923685d571?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e923685d571</guid>
            <category><![CDATA[java-jar]]></category>
            <category><![CDATA[classpath]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[jar]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Mon, 30 Aug 2021 12:37:07 GMT</pubDate>
            <atom:updated>2021-09-01T10:17:16.410Z</atom:updated>
            <content:encoded><![CDATA[<h3>Back to the basics of Java — Part 2: The JAR</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*3goR3TZ7H9rnpt6eCHjscA.png" /></figure><p>In the first part I introduced a simple example explaining how to use the classpath correctly. I will use this example for this part as well, so if you did not create that yet you can copy it from the first part before proceeding if you want to follow along.</p><p><a href="https://ludvigwesterdahl.medium.com/back-to-the-basics-of-java-part-1-classpath-47cf3f834ff">Back to the basics of Java — Part 1: Classpath</a></p><h3>JAR</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*lMRJNWcivkRL3jbA3ed9Zg.jpeg" /></figure><p>A jar file is simply a type of archive used to package<a href="https://javarevisited.blogspot.com/2012/05/10-points-about-class-file-in-java.html"> java class files</a> and associated resources for distribution. If we remember from the last article, our file structure was like this.</p><pre>ClasspathProject/<br>    |<br>    ---&gt; src/java/myprogram/<br>        |<br>        ---&gt; Main.java<br>        |<br>        ---&gt; utils/<br>            |<br>            ---&gt; Util.java<br>    |    <br>    ---&gt; bin/myprogram/<br>        |<br>        ---&gt; Main.class<br>        |<br>        ---&gt; utils/<br>            |<br>            ---&gt; Util.class</pre><p>Let’s <a href="https://javarevisited.blogspot.com/2012/03/how-to-create-and-execute-jar-file-in.html">create an executable jar</a> containing the class files. First we navigate to the root directory.</p><h4>First attempt — the messy way</h4><p>Then we run this command [1].</p><pre>&gt; jar --create --file project.jar bin/myprogram/Main.class bin/myprogram/utils/Util.class</pre><ul><li><strong><em>--create (or -c)</em></strong>: create a new archive</li><li><strong><em>--file (or -f)</em></strong>: filename of the archive</li><li><strong><em>file(s)</em></strong>: the files to include in the archive</li></ul><p>Let’s run it.</p><pre>&gt; java -jar project.jar<br>no main manifest attribute, in project.jar</pre><p>It seems like it cannot find a <a href="https://javarevisited.blogspot.com/2013/01/how-to-fix-failed-to-load-main-class-manifest-attribute-jar-java-eclipse.html">manifest</a>. So let’s inspect what the contents are. Here are three commands that give you the exact same output so I will show all of them but will from now on use the last one.</p><pre>&gt; jar --list --file project.jar (alternative 1)<br>&gt; jar -t -f project.jar         (alternative 2)<br>&gt; jar -tf project.jar           (alternative 3)<br>META-INF/<br>META-INF/MANIFEST.MF<br>bin/myprogram/Main.class<br>bin/myprogram/utils/Util.class</pre><ul><li><strong><em>--list (or -t)</em></strong>: list the contents of a jar</li><li><strong><em>--file (or -f)</em></strong>: the jar file</li></ul><p>It doesn’t look very good. Let’s find out what the manifest contains.</p><pre>&gt; jar --extract --file project.jar (alternative 1)<br>&gt; jar -xf project.jar              (alternative 2)</pre><ul><li><strong><em>--extract (or -x)</em></strong>: extract the content from a jar</li></ul><p>Then print the manifest.</p><pre>&gt; cat META-INF/MANIFEST.MF<br>Manifest-Version: 1.0<br>Created-By: 13.0.2 (Oracle Corporation)</pre><p>Is this fixable? Sure, I’ll quickly show the fix. The minimum fix is to update the manifest with a classpath.</p><pre>&gt; echo &quot;Class-Path: bin/&quot; &gt; manifest.txt<br>&gt; jar --update --file project.jar --manifest manifest.txt</pre><ul><li><strong><em>--update (or -u)</em></strong>: update a jar file</li><li><strong><em>--manifest (or -m)</em></strong>: manifest file to include information from</li></ul><p>Let’s run it again.</p><pre>&gt; java -jar project.jar<br>no main manifest attribute, in project.jar</pre><p>Okey same problem because it is not an executable jar, there is <a href="https://javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html">no main class in the jar manifest</a>. However, we can run it by specifying the jar in the classpath.</p><pre>&gt; java -cp project.jar myprogram.Main<br>Here is 1337</pre><p>Let’s update our manifest file by adding a main class attribute and update the jar.</p><pre>&gt; echo &quot;Main-Class: myprogram.Main&quot; &gt;&gt; manifest.txt<br>&gt; jar -u -f project.jar -m manifest.txt<br>Aug 30, 2021 11:52:26 AM java.util.jar.Attributes read<br>WARNING: Duplicate name in Manifest: Class-Path.<br>Ensure that the manifest does not have duplicate entries, and that blank lines separate individual sections in both your manifest and in the META-INF/MANIFEST.MF entry in the jar file.</pre><p>You can ignore this warning because if we inspect the MANIFEST.MF file as shown above you will see that it looks good.</p><pre>&gt; jar -xf project.jar<br>&gt; cat META-INF/MANIFEST.MF<br>Manifest-Version: 1.0<br>Created-By: 13.0.2 (Oracle Corporation)<br>Class-Path: bin/<br>Main-Class: myprogram.Main</pre><p>But if you wanted, you could have overridden the manifest.txt file with just one new line containing the main-class attribute and it would be changed in the MANIFEST.MF file in the jar.</p><p>Alternatively, you can actually specify the main-class attribute on the command line to update the jar, which is a quick way of changing the attribute.</p><pre>&gt; jar -u -f project.jar --main-class myprogram.Main<br>&gt; java -jar project.jar<br>Here is 1337</pre><ul><li><strong><em>--main-class (or -e)</em></strong>: changes the Main-Class attribute in the manifest</li></ul><p>Now we can run it like normally.</p><pre>&gt; java -jar project.jar<br>Here is 1337</pre><p>Okey, so this was messy. Let’s try and simplify things. First, let’s clear the directory of the files to start over.</p><pre>&gt; rm -rf META-INF/ manifest.txt project.jar</pre><h4>Second attempt —the simpler way</h4><p>Now I’ll try to make this a bit more succinct.</p><pre>&gt; jar -c -e myprogram.Main -f project.jar -C bin/ myprogram/Main.class -C bin/ myprogram/utils/Util.class</pre><ul><li><strong>-C</strong>: changes directory <strong>and</strong> includes the provided file (2 argument flag, note the space)</li></ul><p>Let’s inspect the content.</p><pre>&gt; jar -tf project.jar<br>META-INF/<br>META-INF/MANIFEST.MF<br>myprogram/Main.class<br>myprogram/utils/Util.class</pre><p>Already here you can see that it looks a lot better, no <em>bin</em> directory anywhere. We should be able to run this directly.</p><pre>&gt; java -jar project.jar<br>Here is 1337</pre><p>Alright, great. However, we can actually simplify the command a little bit.</p><pre>&gt; jar -cfe project.jar myprogram.Main -C bin/ .<br>&gt; java -jar project.jar<br>Here is 1337</pre><p>Here I specified the directory and a wildcard using the dot (.) which takes everything inside the <em>bin</em> directory.</p><h4>Side note</h4><p>The ordering of the arguments here is important. If you for instance did this instead:</p><pre>&gt; jar -cfe myprogram.Main project.jar -C bin/ .</pre><p>The command would succeed, but you would end up with a file called myprogram.Main with a main class attribute of project.jar.</p><pre>&gt; ls<br>bin  lib  myprogram.Main src</pre><pre>&gt; java -jar myprogram.Main<br>Error: Could not find or load main class project.jar<br>Caused by: java.lang.ClassNotFoundException: project.jar</pre><h4>Library jar</h4><p>For this part, I’m going to move out Util.class into its own jar file similarly to what I did in the first part of this series when explaining the <a href="https://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html#axzz6uq12fuKh">classpath</a>.</p><p>So the new file structure will look like this.</p><pre>ClasspathProject/<br>    |<br>    ---&gt; src/java/myprogram/<br>        |<br>        ---&gt; Main.java<br>        |<br>        ---&gt; utils/<br>            |<br>            ---&gt; Util.java<br>    |    <br>    ---&gt; bin/myprogram/<br>        |<br>        ---&gt; Main.class<br>    |<br>    ---&gt; lib/myprogram/<br>        |<br>        ---&gt; utils/<br>            |<br>            ---&gt; Util.class</pre><p>And we will run the following commands to generate our jar files.</p><pre>&gt; jar -cfe project.jar myprogram.Main -C bin/ .<br>&gt; jar -cf projectutils.jar -C lib/ .</pre><p>Now if we try and run the project.jar we will obviously get an error because it cannot find the Util class.</p><pre>&gt; java -jar project.jar<br>Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: myprogram/utils/Util<br>    at myprogram.Main.main(Main.java:8)<br>Caused by: java.lang.ClassNotFoundException: myprogram.utils.Util<br>    at   java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)<br>    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)<br>    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)<br>... 1 more</pre><figure><a href="https://javarevisited.blogspot.com/2018/02/how-to-fix-exception-in-thread-main.html"><img alt="" src="https://cdn-images-1.medium.com/max/261/1*yh7D_4LWLJsVJAplTmVKeA.jpeg" /></a></figure><p>So how do we run this? If we remember from the classpath article we can <a href="https://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html">specify the jars on the classpath</a> like this.</p><pre>&gt; java -cp project.jar:projectutils.jar myprogram.Main<br>Here is 1337</pre><p>But what if we want to make project.jar an executable jar file so that the command that failed above works? Well, then we need to use the manifest like so.</p><pre>&gt; echo &quot;Main-Class: myprogram.Main&quot; &gt; manifest.txt<br>&gt; echo &quot;Class-Path: projectutils.jar&quot; &gt;&gt; manifest.txt<br>&gt; jar -cfm project.jar manifest.txt -C bin/ .<br>&gt; java -jar project.jar<br>Here is 1337</pre><p>Note that the Class-Path attribute is a relative path to the created jar file.</p><p>I hope this learning process-like article has helped your understanding of creating jars. Thanks for reading and please give me a like or comment if there is something incorrectly written or it can be improved in any way!</p><p><strong>References</strong></p><p>[1] The jar Command<br><a href="https://docs.oracle.com/en/java/javase/16/docs/specs/man/jar.html">https://docs.oracle.com/en/java/javase/16/docs/specs/man/jar.html</a></p><p>[2] Working with Manifest Files: The Basics<br><a href="https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html">https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e923685d571" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/back-to-the-basics-of-java-part-2-the-jar-6e923685d571">Back to the basics of Java — Part 2: The JAR</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Back to the basics of Java — Part 1: Classpath]]></title>
            <link>https://medium.com/javarevisited/back-to-the-basics-of-java-part-1-classpath-47cf3f834ff?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/47cf3f834ff</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[classpath]]></category>
            <category><![CDATA[java-class]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Mon, 30 Aug 2021 12:36:30 GMT</pubDate>
            <atom:updated>2022-05-15T20:07:49.979Z</atom:updated>
            <content:encoded><![CDATA[<h3>Back to the basics of Java — Part 1: Classpath</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*fz7Am6vX_nku9m5kdm0TNA.png" /></figure><p>My favourite programming language has always been Java, coincidentally it was also my first language I ever used. If you’re like me, or just wants to learn more about the awesome language &amp; technology that is <a href="https://medium.com/javarevisited/why-java-is-the-best-programming-language-to-learn-coding-for-beginners-cba79aed1271">Java</a>, then this two part series is for you.</p><p>In these short series, you will learn about the following.</p><ul><li>Part 1: Classpath</li><li>Part 2: The JAR</li></ul><p>So, why did I decide to write these articles? Well, there was some aspects of Java I did not fully comprehend (such as the classpath) and I realized the reason for this is because I have been using an <a href="https://medium.com/javarevisited/7-best-courses-to-learn-intellij-idea-for-beginners-and-experienced-java-programmers-2e9aa9bb0c05">IDE </a>and <a href="https://medium.com/javarevisited/5-best-gradle-courses-and-books-to-learn-in-2021-93f49ce8ff8e">build tools</a>.</p><p>The issue with an IDE is that it hides a lot of stuff which hinders a fully comprehensive understanding. In other words, I thought it was time to get a little bit more comfortable with the java cli tools and going back to basics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*YgRaUJRRUc7naIQOwmMccQ.jpeg" /></figure><p>Now let’s get down to it. In this part I will explain <a href="http://www.java67.com/2012/08/what-is-path-and-classpath-in-java-difference.html">what the <strong>classpath</strong> is</a> and how to use it correctly (and incorrectly).</p><h3>Classpath</h3><p>The classpath is simply a list of directories, JAR files, and ZIP archives to search for class files [1]. The runtime needs to know where to find your compiled classes so that’s what you’re providing here.</p><p>There are some things that you should be aware of though regarding how you specify the paths. To explain this I will create a dummy project, see below for the structure.</p><pre>ClasspathProject/src/java/myprogram/<br>    |<br>    ---&gt; Main.java<br>    |<br>    ---&gt; utils/<br>        |<br>        ---&gt; Util.java</pre><p>And here are the source files just so you know what we are dealing with.</p><p><strong>Main.java</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/366fcbb5ca409a581c9010a5a3e95fb1/href">https://medium.com/media/366fcbb5ca409a581c9010a5a3e95fb1/href</a></iframe><p><strong>Util.java</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9008ab03fac0f9426ae7bcec5019929d/href">https://medium.com/media/9008ab03fac0f9426ae7bcec5019929d/href</a></iframe><p>I will be using this structure for the following parts as well so If you want to follow along I suggest you create it as well before proceeding.</p><h4>A short note on packages.</h4><p>If you are new to java it can be helpful to know that packages are generally suppose to be mirrored by the file structure. Take the Utils.java file for instance, it has the package myprogram.utils because it is in that folder.</p><p>The structure as provided is not really required by the compiler. However, it is required by the java command to run.</p><p>Let’s compile this little project.</p><p>First we create a <em>bin</em> folder to hold the compiled stuff in the root project folder then compile the two files [2].</p><pre>&gt; mkdir bin<br>&gt; javac src/java/myprogram/Main.java src/java/myprogram/utils/Util.java -d bin/</pre><p>You should now have the following file structure.</p><pre>ClasspathProject/<br>    |<br>    ---&gt; src/java/myprogram/<br>        |<br>        ---&gt; Main.java<br>        |<br>        ---&gt; utils/<br>            |<br>            ---&gt; Util.java<br>    |    <br>    ---&gt; bin/myprogram/<br>        |<br>        ---&gt; Main.class<br>        |<br>        ---&gt; utils/<br>            |<br>            ---&gt; Util.class</pre><p>If we now move to the <em>bin</em> folder we can run the main class.</p><pre>&gt; java myprogram.Main<br>Here is 1337</pre><p>Great, so why does this work? Because the <strong>default</strong> value for the classpath is the <strong>current directory</strong>. It needs to find two class files; Main.class and Utils.class, how and where does it look? Given the current class path of <em>bin</em> and our main class myprogram.Main it will look in this directory to try and find the following:</p><ul><li>myprogram/Main.class</li><li>myprogram/utils/Util.class</li></ul><p>Which we know exists, so the program runs successfully.</p><p>Now let’s change it up a little bit. Let’s <strong>add</strong> a new folder <em>lib</em> on the same level as <em>bin</em> and move the Utils.class so that it now looks like this.</p><pre>ClasspathProject/<br>    |<br>    ---&gt; src/java/myprogram/<br>        |<br>        ---&gt; Main.java<br>        |<br>        ---&gt; utils/<br>            |<br>            ---&gt; Util.java<br>    |    <br>    ---&gt; bin/myprogram/<br>        |<br>        ---&gt; Main.class<br>    |<br>    ---&gt; lib/myprogram/<br>        |<br>        ---&gt; utils/<br>            |<br>            ---&gt; Util.class</pre><h4>Error 1— No classpath specified</h4><p>If we now move to the <em>bin</em> folder and try and run the same command we will get an error because it cant find the Util.class file.</p><pre>&gt; java myprogram.Main<br>Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: myprogram/utils/Util<br>    at myprogram.Main.main(Main.java:8)<br>Caused by: java.lang.ClassNotFoundException: myprogram.utils.Util<br>    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)<br>    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)<br>    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)<br>    ... 1 more</pre><h4>Error 2 — Adding a class file to the classpath</h4><p>We will try and solve this by adding the class file to the <a href="https://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html#axzz6uq12fuKh">classpath </a>(we are running all these commands from the <em>bin</em> folder).</p><pre>&gt; java -classpath ../lib/myprogram/utils/Util.class myprogram.Main<br>Error: Could not find or load main class myprogram.Main<br>Caused by: java.lang.ClassNotFoundException: myprogram.Main</pre><p>Ouch, now it cannot find the Main.class file? Because as previously explained, it will try and find the files in the specified directories provided in the classpath. And we did not provide a directory. So let’s do that instead.</p><h4>Error 3 — Adding one directory to the classpath</h4><pre>&gt; java -classpath ../lib/myprogram/utils/ myprogram.Main<br>Error: Could not find or load main class myprogram.Main<br>Caused by: java.lang.ClassNotFoundException: myprogram.Main</pre><p>Okay, same error. What happened here? Since we provided a classpath argument that overrides the default one (which is the current directory) it cannot find our Main.class file anywhere. Let’s add that. <em>On Unix systems you separate with a colon (:) and on Windows, you use a semi-colon (;).</em></p><h4>Error 4 — Adding more paths to the classpath</h4><pre>&gt; java -classpath ../lib/myprogram/utils/:. myprogram.Main<br>Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: myprogram/utils/Util<br>    at myprogram.Main.main(Main.java:8)<br>Caused by: java.lang.ClassNotFoundException: myprogram.utils.Util<br>    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)<br>    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)<br>    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)<br>    ... 1 more</pre><p>Okey, now it seems that it can find the Main.class file. But it <strong>still cannot find</strong> the Util.class file. As previously explained it will try and find the following files using any of the <a href="https://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html">classpaths</a>.</p><ul><li>myprogram/Main.class</li><li>myprogram/utils/Util.class</li></ul><p>In this case we have two classpaths.</p><ul><li>../lib/myprogram/utils/</li><li>.</li></ul><p>So simply put, it will try and append the classpaths to the required files to see if they exist, I image the process would look something like this.</p><pre>Cp1 = ../lib/myprogram/utils/<br>Cp2 = ./<br>File1 = myprogram/Main.class<br>File2 = myprogram/utils/Util.class</pre><pre>Trying to find File1</pre><pre>Cp1 + File1 = ../lib/myprogram/utils/myprogram/Main.class<br>NOT FOUND<br>Cp2 + File1 = ./myprogram/Main.class<br>FOUND</pre><pre>Trying to find File2</pre><pre>Cp1 + File2 = ../lib/myprogram/utils/myprogram/utils/Util.class<br>NOT FOUND<br>Cp2 + File2 = ./myprogram/utils/Util.class<br>NOT FOUND</pre><pre>---&gt; ERROR</pre><h4>Final solution</h4><p>Can we solve this by adding those subfolders under <em>lib</em>? Sure we can, which will work. However, instead of adding those folders let’s modify the classpath argument instead so that it looks like this.</p><pre>&gt; java -classpath ../lib/:. myprogram.Main<br>Here is 1337</pre><p>This works because it will now be able to find Utils.class file using the first path.</p><pre>../lib/ + myprogram/utils/Util.class<br>FOUND</pre><p>I hope that some of the errors here have helped your understanding of the class path. It seems that a lot of tutorials only present the correct way of doing things but I find that seeing the incorrect way of doing things helps my learning a lot more.</p><p><a href="https://ludvigwesterdahl.medium.com/back-to-the-basics-of-java-part-2-the-jar-6e923685d571">Back to the basics of Java — Part 2: The JAR</a></p><p><strong>References</strong></p><p>[1] java options<br><a href="https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html">https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html</a></p><p>[2] javac — Java programming language compiler<br><a href="https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html">https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47cf3f834ff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/back-to-the-basics-of-java-part-1-classpath-47cf3f834ff">Back to the basics of Java — Part 1: Classpath</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Some tips for Microbenchmarking in Java]]></title>
            <link>https://ludvigwesterdahl.medium.com/some-tips-for-microbenchmarking-in-java-46d6c9b669d9?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/46d6c9b669d9</guid>
            <category><![CDATA[java-performance]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[microbenchmark]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Fri, 20 Aug 2021 14:32:27 GMT</pubDate>
            <atom:updated>2021-08-31T19:45:09.986Z</atom:updated>
            <content:encoded><![CDATA[<p>Microbenchmarking is one way of performance testing a small part of your product. Perhaps you wan’t to compare the difference between two similar algorithm implementations. The goal however is to make sure that you test the right thing.</p><p>So what can affect your output and what can you do about it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*zLkC2xINaViP_tnLvmAD4A.png" /></figure><p>For one, the JVM options of the production environment vs local environment. Another issue that can come up is GC. But the biggest culprit is most likely the compiler.</p><p>Assuming you start your program with the same options then here are my three things I try and remember when microbenchmarking in order to give me a good enough picture of the performance.</p><p>Here are my three tips.</p><h4>Number 1: Warm up</h4><p>Warm up simply means that you run your method(s) some number of times before actually measuring the execution time. This is done to allow the code to be compiled so that you aren’t measuring the compilation time. It also allows other optimizations (there are plenty) to be performed that makes the output more likely to match the execution time in production. One of those optimizations is method inlining which I’ve written about in another article.</p><p><a href="https://ludvigwesterdahl.medium.com/method-inlining-in-java-84caec9b3e18">Method inlining in Java</a></p><h4>Number 2: Average</h4><p>Run the method, let’s say 3 times and look at those numbers and perhaps take the average. This is just to ensure you don’t see a spike due to a minor/major GC.</p><h4><strong>Number 3: “Use” the result</strong></h4><p>Finally, ensure that you use the result in some way so that the compiler does not make your code into a no-op. Depending on your test setup, one way to do that is to write the result to a <em>volatile </em>instance variable. Just make sure you aren’t writing too often or that will end up affecting the time of your test.</p><p>So these are my main things I think about when microbenchmarking. Hope you get some value out of this as well!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46d6c9b669d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s talk about Java/JVM flags]]></title>
            <link>https://medium.com/codex/lets-talk-about-java-jvm-flags-23fe0f826bc2?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/23fe0f826bc2</guid>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[java-performance]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java-performance-tuning]]></category>
            <category><![CDATA[java-tips]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Thu, 19 Aug 2021 16:02:34 GMT</pubDate>
            <atom:updated>2021-09-01T11:29:26.855Z</atom:updated>
            <content:encoded><![CDATA[<p>Why would you want to know this? Well, in order to be able to tune an application and identify performance issues it’s important to understand what flags are actually being used by your application and possibly why.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*lUoprudhyMDkcG2R6BZ0fA.jpeg" /></figure><p><strong>TL;DR<br></strong>You can view the flags used in your java application in multiple ways. Either by specifying a flag on the command line (-XX:+PrintFlagsFinal) or by using <strong>jcmd</strong> or <strong>jinfo</strong> on an active VM.</p><p>I’m running OpenJDK 13, in case you get different results please consult your specific JVM implementations documentation. Most of what is written here applies to other java versions as well.</p><h4>Process ID</h4><p>Most commands to monitor an active JVM requires that you identify the process id (PID) of the application. A simple way to retrieve the PID is using JPS [1] but you can also retrieve it from code if needed. Here I have started a simple program that loops forever called JavaFlags.</p><pre>&gt; jps<br>...<br>1966 JavaFlags<br>...</pre><p>If you want to retrieve it by code take a loop at the example below which illustrates how to get the PID of the current process and other processes started with the java command.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/339b9b5dead8336263398700cf5c0a23/href">https://medium.com/media/339b9b5dead8336263398700cf5c0a23/href</a></iframe><p>Which provides the following output.</p><pre>current process PID: 2016<br>PID        name      <br>---------- ----------<br>2016       PidFromCode<br>...<br>1966       JavaFlags</pre><p>Now that we got the PID we are able to use other java tools to get more information about the running JVM. This article will talk about how to figure out what arguments was provided to the JVM and from where. Future articles will talk about how to actually monitor and investigate possible issues and tune the application.</p><h3>View flags</h3><p>There are in fact multiple ways you can retrieve the flags of an active JVM.</p><p><strong><em>Side note<br></em></strong>It is possible to tell the JVM to print the flags it uses on startup using a jvm flag but here we assume that this flag was not provided. I have included it below for those who are interested.</p><pre>&gt; java -XX:+PrintFlagsFinal ...</pre><p>To get information about this JVM and its flags you can run the following command.</p><pre>&gt; java -XX:+PrintFlagsFinal -version</pre><h4><strong>Show all</strong></h4><p>The following command prints the same information as the aforementioned flag using <strong>jcmd</strong> of an active VM [2].</p><pre>&gt; jcmd {PID} VM.flags -all<br>2636:<br>[Global flags]<br>...<br>bool BranchOnRegister = false              {C2 product} {default}<br>bool C1OptimizeVirtualCallProfiling = true {C1 product} {default}<br>...<br>uintx InitialCodeCacheSize = 2555904       {pd product} {default}<br>size_t InitialHeapSize = 402653184         {product} {ergonomic}<br>...<br>uintx MaxHeapFreeRatio = 70                {manageable} {default}<br>size_t MaxHeapSize = 10737418240           {product} {command line}<br>...<br>size_t SoftMaxHeapSize = 6442450944        {manageable} {ergonomic}</pre><p>The <strong>first</strong> brackets can contain lots of different values, below are some of the most common.</p><ul><li><strong><em>product</em></strong>: The default value of this flag is the same across all platforms, in other words, it’s a product build default value.</li><li><strong><em>pd product</em></strong>: The default value of this flag is platform dependent (e.g. linux-x64)</li><li><strong><em>manageable</em></strong>: The value of this flag can be set during runtime. For instance using <strong>jinfo</strong> as explained below [3].</li><li><strong><em>C1|C2 product</em></strong>: The default value of this flag is specific to C1 (client) and C2 (server) JIT compiler. The difference between these compilers is out of scope of this article. Shortly, client compiler runs faster but produces less optimized code than C2. Which one is used depends on your platform but can be overridden in some instances, on most machines today both are used due to tiered compilation being the default since java 8.</li><li><strong><em>C1|C2 diagnostic</em></strong>: The flag provides diagnostic info and is not used for tuning.</li><li><strong><em>ARCH product</em></strong>: The flag only exists for some architectures and constant for others.</li><li><strong><em>lp64_product</em></strong>: The flag only exists for 64-bit JVM and is constant for 32-bit versions.</li><li><strong><em>commercial</em></strong>: The flag is only available for commercial version.</li><li><strong><em>experimental</em></strong>: The flag is available if experimental mode is specified using -XX:+UnlockExperimentalVMOptions</li></ul><p>The <strong>second</strong> brackets can be one of the following. Note that in previous versions, this bracket was not there and instead it was specified if the flag had a non-default value with a colon before the equal sign (“:=”).</p><ul><li><strong><em>{default}</em></strong>: The value for this flag is a default value for this JVM version.</li><li><strong><em>{ergonomic}</em></strong>: The value was set to an optimal value according to the JVM taking into account the specific machine it is running on (e.g. OS, 32/64-bit or number of CPU:s).</li><li><strong><em>{command line}</em></strong>: The flag was set on the command line which always takes precedence (if a flag is specified multiple times, the last occurrence is used).</li><li><strong><em>{attach}</em></strong>: The flag was changed during runtime, only applicable for manageable flags.</li></ul><h4>Show specific flag</h4><p>You can also view the current value of a flag using jinfo like this.</p><pre>&gt; jinfo -flag SoftMaxHeapSize {PID}<br>-XX:SoftMaxHeapSize=2000</pre><pre>&gt; jinfo -flag HeapDumpBeforeFullGC {PID}<br>-XX:+HeapDumpBeforeFullGC</pre><h3>Setting flags</h3><p>The most common way of setting flags is on the command line. The syntax differs depending on if it’s a boolean or a value flag.</p><p><strong>Boolean examples</strong></p><pre>&gt; java -XX:+BranchOnRegister (enables)</pre><pre>&gt; java -XX:-BranchOnRegister (disables)</pre><p><strong>Value examples</strong></p><pre>&gt; java -XX:SoftMaxHeapSize=2G (2GB)</pre><pre>&gt; java -XX:SoftMaxHeapSize=2M (2MB)</pre><pre>&gt; java -XX:SoftMaxHeapSize=2K (2KB)</pre><h4>Setting a flag during runtime</h4><p>You can also set some flags (if they are specified as manageable) during runtime. First get the PID as explained above then you can run the following command. If you try and set a non-manageable flag you will get an error (on earlier versions I believe this was silently ignored).</p><p><strong>Boolean examples</strong></p><pre>&gt; jinfo -flag +HeapDumpBeforeFullGC {PID} (enables)</pre><pre>&gt; jinfo -flag -HeapDumpBeforeFullGC {PID} (disables)</pre><p><strong>Value examples</strong><br>Note here that you cannot use a unit such as K, M, or G but you have to specify the exact value.</p><pre>&gt; jinfo -flag SoftMaxHeapSize=2048 {PID} (sets it to 2M)</pre><p>Finally, some flags have aliases and can be specified by either or. For instance setting the maximum heap size.</p><pre>&gt; java -Xmx3G -XX:+PrintFlagsFinal -version | grep MaxHeapSize<br>size_t MaxHeapSize = 3221225472 {product} {command line}<br>...</pre><pre>&gt; java -XX:MaxHeapSize=3G -XX:+PrintFlagsFinal -version | grep MaxHeapSize<br>size_t MaxHeapSize = 3221225472 {product} {command line}<br>...</pre><p>The only issue with this is that jinfo will not recognize one of the alises, for the case above using Xmx you will get an not found error.</p><pre>&gt; jinfo -flag Xmx {PID}<br>no such flag &#39;Xmx&#39;</pre><p>More information about this and other flags can be found <a href="https://docs.oracle.com/en/java/javase/13/docs/specs/man/java.html">here</a> and <a href="https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html">here</a> [4, 5].</p><p><strong>References</strong></p><p>[1] Java Virtual Machine Process Status Tool (JPS)<br><a href="https://docs.oracle.com/javase/7/docs/technotes/tools/share/jps.html">https://docs.oracle.com/javase/7/docs/technotes/tools/share/jps.html</a></p><p>[2] The jcmd Command<br><a href="https://docs.oracle.com/en/java/javase/13/docs/specs/man/jcmd.html">https://docs.oracle.com/en/java/javase/13/docs/specs/man/jcmd.html</a></p><p>[3] jinfo add reference here</p><p>[4] The java Command<br><a href="https://docs.oracle.com/en/java/javase/13/docs/specs/man/java.html">https://docs.oracle.com/en/java/javase/13/docs/specs/man/java.html</a></p><p>[5] Java HotSpot VM Options<br><a href="https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html">https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23fe0f826bc2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/lets-talk-about-java-jvm-flags-23fe0f826bc2">Let’s talk about Java/JVM flags</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Some thoughts on using LinkedList in Java]]></title>
            <link>https://medium.com/javarevisited/consider-linkedlist-in-java-2fed1b945b48?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/2fed1b945b48</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[java-performance]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Wed, 18 Aug 2021 14:32:08 GMT</pubDate>
            <atom:updated>2021-09-19T15:46:59.665Z</atom:updated>
            <content:encoded><![CDATA[<p>This is going to be a different article as my assumption after testing some on my machine turned out to be slightly wrong. Here&#39;s what I found.</p><p><strong>TL;DR<br></strong>Use <a href="https://javarevisited.blogspot.com/2011/05/example-of-arraylist-in-java-tutorial.html#axzz6qVaG06bu">ArrayList</a> in most standard cases where you simply add to the list and later on iterate over it. It will save you both time and memory.</p><p><strong>Note</strong> that this article does not discuss the complexities of some of their operations where we know one or the other would perform substantially better (e.g. adding elements in the beginning of the list or random access). The use case discussed here is simply appending items in order to iterate over it at some later point.</p><p>My hypothesis was that <a href="https://www.java67.com/2012/12/difference-between-arraylist-vs-LinkedList-java.html"><strong>LinkedList</strong> </a>would perform better compared to <strong>ArrayList</strong> when simply adding elements to the end of the list in all cases. And I would then recommend that we should turn away from by default typing</p><pre>List&lt;Object&gt; list = new ArrayList&lt;&gt;();</pre><p>and instead, write</p><pre>List&lt;Object&gt; list = new LinkedList&lt;&gt;();</pre><p><strong><em>Why?</em></strong></p><p>Because <a href="https://javarevisited.blogspot.com/2015/07/java-arraylist-tutorial.html">ArrayList </a>has to perform resizing operations once the inner container fills up while the <a href="http://www.java67.com/2016/02/how-to-sort-linkedlist-in-java-example.html">LinkedList </a>simply has to append a new node (the same operation regardless the size).</p><h4>The test</h4><p>So I set up a little test of adding 10M elements to each type of list, see below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a29eca54bdf594ee83207308f549ab7c/href">https://medium.com/media/a29eca54bdf594ee83207308f549ab7c/href</a></iframe><p>This is a dummy example but similar setups are not unlikely to be seen in production. My results were that they performed very similarly.</p><p>The test was run with the following<a href="https://www.java67.com/2016/08/10-jvm-options-for-java-production-application.html"> VM option</a>s to remove any issues with adaptive heap sizing or GC.</p><pre>-Xms5G -Xmx5G -XX:+UnlockExperimentalVMOptions -XX:+AlwaysPreTouch -XX:+UseEpsilonGC</pre><figure><a href="https://javarevisited.blogspot.com/2011/11/hotspot-jvm-options-java-examples.html#axzz54oJiRW8k"><img alt="" src="https://cdn-images-1.medium.com/max/596/1*iQDOebOp2ByZluSiWy2x6Q.png" /></a><figcaption>First test result with epsilon GC.</figcaption></figure><p>Now obviously this is pretty far from reality so I decided to modify the test with just specifying the <a href="https://medium.com/javarevisited/7-best-courses-to-learn-jvm-garbage-collection-and-performance-tuning-for-experienced-java-331705180686?source=---------8------------------">G1 garbage collector</a>.</p><pre>-XX:+UseG1GC</pre><p>See below for the new test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c3666437ee6a7a13ca80ae63b0eccf4/href">https://medium.com/media/2c3666437ee6a7a13ca80ae63b0eccf4/href</a></iframe><p>The difference here is that I’ve averaged the times which introduces some extra warmup so the compiler has a bit more time to optimize and we achieve a more realistic result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*gmxHTbOnRDHCECUCXoPgCw.png" /><figcaption>More realistic results.</figcaption></figure><p><strong><em>What happened?</em></strong></p><p>It’s difficult to say if the difference between ArrayList and LinkedList in the final example is due to compiler optimizations being more effective for the ArrayList, effects of GC or as a result of the <a href="https://www.java67.com/2020/07/how-to-get-first-and-last-element-of-linkedlist-in-java.html">LinkedList </a>requiring more memory. To figure that out one would have to dig into the compiler &amp; GC logs.</p><p>But for now, we can conclude that due to either of those effects <a href="https://javarevisited.blogspot.com/2012/01/how-to-sort-arraylist-in-java-example.html">ArrayList </a>seems to perform substantially better than LinkedList using a more realistic setup with an actual GC running.</p><h4>Finally some memory considerations</h4><p>I performed the following test and noted the memory differences.</p><figure><a href="https://www.java67.com/2012/08/how-to-sort-arraylist-in-java-list.html"><img alt="" src="https://cdn-images-1.medium.com/max/976/1*PUHsWvce7ObHCaB-yJ1NUA.png" /></a><figcaption>Memory test for 100 elements.</figcaption></figure><p>Following table illustrates the results in a retained heap when doing the aforementioned test with 100 and 1000 Integer elements (which takes 16B since int takes 4 bytes and header info takes 12 [1]) each as reported by MAT [2].</p><pre>+--------------+----------------+-----------------+----------------+<br>|     Type     |  100 elements  |  1000 elements  |  Overhead (*)  |<br>+--------------+----------------+-----------------+----------------+<br>|  LinkedList  |     4032B      |      40032B     |      24B       |<br>|  ArrayList   |     2040B      |      20040B     |       4B       |<br>+--------------+----------------+-----------------+----------------+</pre><p>Overhead here is simply the retained heap per item minus 16B as provided by MAT less some header info of the actual list. As expected the LinkedList takes up roughly double memory compared to the <a href="https://javarevisited.blogspot.com/2014/01/ow-to-remove-objects-from-collection-arraylist-java-iterator-traversing.html">ArrayList</a>. However, this is given a small element, with bigger elements the overhead will be negligible.</p><h4>Takeaways</h4><p>My takeaway from this is to <strong>prefer ArrayList over LinkedList in most standard</strong> cases where the use case is to simply add to the end and iterate over the list.</p><p><strong>References</strong></p><p>[1] How to calculate the memory usage of Java objects<br><a href="https://www.javamex.com/tutorials/memory/object_memory_usage.shtml">https://www.javamex.com/tutorials/memory/object_memory_usage.shtml</a></p><p>[2] Memory Analyzer (MAT)<br><a href="https://www.eclipse.org/mat/">https://www.eclipse.org/mat/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2fed1b945b48" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/consider-linkedlist-in-java-2fed1b945b48">Some thoughts on using LinkedList in Java</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Method inlining in Java]]></title>
            <link>https://medium.com/codex/method-inlining-in-java-84caec9b3e18?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/84caec9b3e18</guid>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java-compiler]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Tue, 17 Aug 2021 13:01:10 GMT</pubDate>
            <atom:updated>2021-09-01T11:29:24.279Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*fa5-2FrzJHSpckWYx__mJg.png" /><figcaption>Method inlining example.</figcaption></figure><p>This article aims to introduce method inlining and demonstrate the effects.</p><p><strong>TL;DR<br></strong>Method inlining is enabled by default and should not be disabled. It is a compiler optimization that essentially replaces a method call by its content.</p><p>Let’s talk a little about compiler optimizations in Java. The HotSpot JVM just-in-time (JIT) compiler can perform a plethora of optimizations such as but not limited to;</p><ul><li>When to use a register over main memory to store values.</li><li>Skipping method lookups.</li><li>Escape analysis (-XX:+DoEscapeAnalysis, -XX:-DoEscapeAnalysis)</li><li>Inlining (-XX:+Inline, -XX:-Inline)</li></ul><h4>Method inlining</h4><p>It is activated/disabled using the following flag (activated by default). Most java true/false flags use this syntax of -XX:[+,-]FLAG to set to true or false [1].</p><pre>-XX:+Inline (enables)<br>-XX:-Inline (disables)</pre><p>Inlining is an optimization where the compiler has determined that a method is not needed and has replaced the method call with simple statements. The compiler basically performs the same optimization as I have done manually in the <strong>example below</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3855b15c560bce2a24d14290e71e0f3e/href">https://medium.com/media/3855b15c560bce2a24d14290e71e0f3e/href</a></iframe><p>In the early versions of Java, performing this manual inlining used to be a common way to achieve better performance. Obviously we shouldn’t do that anymore as the compiler handles it for us.</p><p>So, how big of an improvement can this yield? I have constructed an extreme example to demonstrate the effects, see below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4ef4006a9b52be01cbbd3750c2dc589/href">https://medium.com/media/c4ef4006a9b52be01cbbd3750c2dc589/href</a></iframe><p>The code basically performs a lot of getting and setting, mostly setting values to the Point (note the loop inside Point#setX and Point#setY).</p><p><strong>Inlining enabled</strong></p><p>If we run the example without any argument (-XX:+Inline is enabled by default), we can see that the execution time is close to 0.</p><pre>&gt; javac MethodInlining.java</pre><pre>&gt; java MethodInlining</pre><pre>Running pid: 41301<br>Elapsed time (ms): 0<br>Point[x=49995000, y=49995000]</pre><p><strong>Inlining disabled</strong></p><p>Let’s compare it with disabling inlining.</p><pre>&gt; javac MethodInlining.java</pre><pre>&gt; java -XX:-Inline MethodInlining</pre><pre>Running pid: 41303<br>Elapsed time (ms): 9754<br>Point[x=49995000, y=49995000]</pre><p>The test now took close to 10 seconds (!). Here we have an extreme case where method invocation overhead takes up most of the time. Obviously you won’t see this in the wild, however, you might see a performance reduction of somewhere between 30–50% if inlining is disabled [2].</p><h4>Logging inlining in the JVM</h4><p>There are some flags you can use in order to get some insight into the JVM and when it inlines methods.</p><pre>&gt; javac MethodInlining.java</pre><pre>&gt; java -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining MethodInlining</pre><pre>...<br>@ 22   MethodInlining$Point::setX (19 bytes)   inline (hot)<br>  @ 9   MethodInlining$Point::setXInside (6 bytes)   accessor<br>...<br>@ 32   MethodInlining$Point::setY (19 bytes)   inline (hot)<br>  @ 9   MethodInlining$Point::setYInside (6 bytes)   accessor<br>...</pre><p>This tells you that the compiler has inlined the method. Running the example without inlining we can note the difference.</p><pre>&gt; javac MethodInlining.java</pre><pre>&gt; java -XX:-Inline -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining MethodInlining</pre><pre>...<br>@ 9   MethodInlining$Point::setXInside (6 bytes)   not inlineable<br>@ 9   MethodInlining$Point::setYInside (6 bytes)   not inlineable<br>...</pre><p>Finally, when do methods get inlined? Well, it depends on how hot the method is (how often it is called) and it’s size. If a method is determined hot and eligible for inlining it will be inlined if it is smaller than the flag below (in bytes).</p><pre>&gt; java -XX:+PrintFlagsFinal -version | grep FreqInlineSize</pre><pre>intx FreqInlineSize = 325 {pd product} {default}</pre><pre>openjdk version &quot;13.0.2&quot; 2020-01-14<br>OpenJDK Runtime Environment (build 13.0.2+8)<br>OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)</pre><p>Alternatively, it is eligible for inlining only if it is smaller than the flag below (in bytes).</p><pre>&gt; java -XX:+PrintFlagsFinal -version | grep MaxInlineSize</pre><pre>intx MaxInlineSize = 35 {product} {default}</pre><pre>openjdk version &quot;13.0.2&quot; 2020-01-14<br>OpenJDK Runtime Environment (build 13.0.2+8)<br>OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)</pre><p><strong>References</strong></p><p>[1] Java HotSpot VM Options<br><a href="https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html">https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html</a></p><p>[2] Java Performance — The Definitive Guide by Scott Oaks</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84caec9b3e18" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/method-inlining-in-java-84caec9b3e18">Method inlining in Java</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[String Deduplication in Java]]></title>
            <link>https://medium.com/codex/string-deduplication-in-java-2dc1e1893a74?source=rss-2c60fee49e55------2</link>
            <guid isPermaLink="false">https://medium.com/p/2dc1e1893a74</guid>
            <category><![CDATA[string-deduplication]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[garbage-collection]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[java-strings]]></category>
            <dc:creator><![CDATA[Ludvig Westerdahl]]></dc:creator>
            <pubDate>Sun, 20 Jun 2021 19:30:05 GMT</pubDate>
            <atom:updated>2021-09-20T01:19:54.928Z</atom:updated>
            <content:encoded><![CDATA[<p>This article aims to explain and demonstrate what String Deduplication in Java is.</p><p><strong>TL;DR<br></strong>String Deduplication allows multiple Strings to share the same underlying character array. You can activate it as follows.</p><pre>-XX:+UseG1GC -XX:+UseStringDeduplication</pre><p>I’m using OpenJDK 13, as long as your using java version 9 or above you will be able to follow along and reproduce the results presented here.</p><p>First, let’s explain the basics of the String type. The String contains a field called value which holds the actual content (the character).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e39d068528b10ab3e0280bdb6bc3bd3/href">https://medium.com/media/0e39d068528b10ab3e0280bdb6bc3bd3/href</a></iframe><p>You probably know that it is probably bad practice to create new String objects instead of using something called a “String literal”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2063a7399be0a286e626aa2242dab618/href">https://medium.com/media/2063a7399be0a286e626aa2242dab618/href</a></iframe><p>What’s the problem with the code above? Let’s analyze it. I’m going to use <a href="https://visualvm.github.io/">VisualVM</a>, but there are other options available. Run the following commands (I’m using a “-” sign to deactivate String Deduplication for clarity, but it is deactivated by default).</p><pre>&gt; javac StringDeduplication.java<br>&gt; java -XX:+UseG1GC -XX:-UseStringDeduplication StringDeduplication</pre><p>Then open up another console window to extract the heap dump (replace {PID} with your pid).</p><pre>&gt; jcmd {PID} GC.heap_dump -all ~/Desktop/stringdeduplication-1.hprof</pre><p>Open this dump in VisualVM, select “Objects” then “GC Roots” and navigate to the ArrayList, you will find that the ArrayList contains 4 elements as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*28ATbjkWZv7_DAHGrMaZxw.png" /><figcaption>The ArrayList “strings” in StringDeduplication.java from VisualVM.</figcaption></figure><p>Notice here that the first 4 strings are different references, while the last two are the same. Further, expanding the elements, you can note that the first two strings will also point to two different byte arrays (the <strong>value</strong> field in String).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o_Cn2m3tH20TLCadKOLrRQ.png" /><figcaption>Expanded the “worst” strings.</figcaption></figure><p>The reason the “bad” string will contain the same <strong>value</strong> field is due to the String literal being used in the String constructor which takes it from the String pool. In this case it’s unnecessary to use the String constructor, there are some special cases where it can be useful, not discussed here.</p><p>What to do? Here is where String Deduplication comes into the picture. (Note that String Deduplication only works for the G1 garbage collector.) Run the following commands to start the application.</p><pre>&gt; javac StringDeduplication.java<br>&gt; java -XX:+UseG1GC -XX:+UseStringDeduplication -Xlog:gc*=info StringDeduplication</pre><p>Then open up another console window to force a GC then extract the heap dump (replace {PID} with your pid).</p><pre>&gt; jcmd {PID} GC.run<br>&gt; jcmd {PID} GC.heap_dump -all ~/Desktop/stringdeduplication-2.hprof</pre><p>The reason we do a GC first is because this is when the String Deduplication happens, which can result in slightly longer pause times. But hopefully this should make other phases more efficient as fewer objects needs to be moved around.</p><p>Now open up VisualVM and note the difference in the underlying <strong>value</strong> field of the two first strings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8ZzAahUGE7486dXpPUprpw.png" /><figcaption>Expanded the “worst” strings with String Deduplication.</figcaption></figure><p>Voila, they now share the same underlying <strong>value</strong> field.</p><p><strong>References</strong></p><p>[1] VisualVM<br><a href="https://visualvm.github.io/">https://visualvm.github.io/</a></p><p>[2] Java® Development Kit Version 13 Tool Specifications<br><a href="https://docs.oracle.com/en/java/javase/13/docs/specs/man/index.html">https://docs.oracle.com/en/java/javase/13/docs/specs/man/index.html</a></p><p>[3] The java Command<br><a href="https://docs.oracle.com/en/java/javase/13/docs/specs/man/java.html">https://docs.oracle.com/en/java/javase/13/docs/specs/man/java.html</a></p><p>[4] Java HotSpot VM Options<br><a href="https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html">https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html</a></p><p>[5] The jcmd Command<br><a href="https://docs.oracle.com/en/java/javase/13/docs/specs/man/jcmd.html">https://docs.oracle.com/en/java/javase/13/docs/specs/man/jcmd.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2dc1e1893a74" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/string-deduplication-in-java-2dc1e1893a74">String Deduplication in Java</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>