Will your applications run faster with Java 9, 10, 11?


Posted on: February 28, 2019 by Ioannis Kolaxis

Are you still running your software applications on Java 8? Since Java 9, there have been several enhancements on how Strings are being handled, promising to improve the performance of your applications.

Storing Strings

If you examine the memory being used by your Java application, you will most likely notice that a significant amount of the heap is allocated by String objects.

Up to Java 8, each String is represented internally as a char[] (that is a char array).

The char data type is a single Unicode character that takes up 2 bytes of memory. It has a minimum value of '\u0000' (=0) and a maximum value of '\uffff' (=65535).

When we store a Unicode character, such as the Greek character 'α' (='\u03b1'), then we are truly making use of both bytes being allocated for that char.

However, when we store a Latin character, such as 'a' (='\u0061'), then the first byte allocated for that char is filled with zeros and is thus being effectively unused.

Wouldn't it be great if Java didn't allocate 2 bytes for each Latin character, but allocated only 1 byte instead?

In Java 9, the String class has been changed to store characters in a byte[] (instead of a char[]). This change in the underlying data structure of String allows a fine grained allocation of memory, depending on the type of characters:

  • for Strings containing Unicode characters, 2 bytes are being allocated for each character, as previously.
  • for Strings containing only Latin characters, 1 byte is allocated for each character, thus saving memory compared to previous releases of Java.

Concatenating Strings

Up to Java 8, the Java compiler recognizes any String concatenations in the source code, and translates them into chains of StringBuilder::append.

Since Java 9, each String concatenation is translated into a call to invokedynamic, as described in JDK Enhancement Proposal 280.

There are several String concatenation strategies available in Java 9, with each one of them employing a different technique to compute the size of the resulting/concatenated String. One of the strategies that stands out for its performance is MH_INLINE_SIZED_EXACT, which takes advantage of private Java APIs to perform the String concatenation.

Performance Improvement

The allocation rate is a key performance metric in Java applications; it is the amount of memory allocated per time unit, and it is typically measured in MB/sec.

Thanks to the reduced memory footprint of String objects since Java 9, your applications are expected to have a lower allocation rate; this means that the Garbage Collector will be called less frequently to clean the memory. Given that the Garbage Collector pauses your applications while it is cleaning the memory, you will likely experience a decrease in the frequency and/or the duration of pauses for your applications. It is therefore assumed that your applications will have a higher throughput, and lower latency in Java 9, 10, 11.

As stated here"footprint and performance improvements up to 10%" have been measured for several applications in Java 9.

How much faster will your applications run with Java 9, 10, 11? There is only one way to find out: download the latest version of Java and measure the performance of your applications!

Links

If you are interested in finding out more details about the String-related enhancements that were introduced in Java 9, please check the following resources:

  1. JEP 254: Compact Strings
  2. JEP 280: Indify String Concatenation
  3. Video: Aleksey Shipilëv presents the work done in JDK 9 for JEP 254, and JEP280
  4. Video: Aleksey Shipilëv demonstrates the performance of JDK 9 with JEP 254, and JEP280

Share this blog article


About Ioannis Kolaxis

Software Engineer, Atos Senior Digital Expert
Ioannis Kolaxis is a Software Engineer at Atos Greece, developing innovative software products for Unified Communication and Collaboration (UCC). He enjoys learning new stuff, and playing with new technologies, firmly embracing Socrate's quote: "I know one thing: that I know nothing". He is a speaker at local meetups, such as the Java Hellenic User Group (Athens, Greece), and Thessaloniki Java Meetup (Thessaloniki, Greece). He frequently writes articles on LinkedIn and Developer Zone, focusing on Software Engineering topics, like Java and Software Quality. He participates in the Internet Engineering Task Force (IETF). He is certified as a European Professional Engineer (EUR ING), and a Professional Scrum Master (PSM I). In 2017, he was awarded by Atos with the title of "Senior Digital Expert", as a recognition of his technical expertise and professional accomplishments.

Follow or contact Ioannis