While You Were Sleeping: The Top New Java 8 Additions
What are some of the most interesting additions to Java 8 since it was launched?
Java 8 recently celebrated its first birthday, with the main release coming just over a year ago now. That’s certainly worthy of a celebratory cupcake.
Since the initial Java 8 version came out, six updates have been released. Some of the elements of these updates are minor, but there are a few interesting additions that may have slipped by you if you weren’t paying attention. Here are five that you should check out:
New Post: While You Were Sleeping: The Top New Java 8 Additions http://t.co/4bGHVwzo7q pic.twitter.com/lZz2Aq2XTA
— OverOps (@overopshq) March 30, 2015
1. Nashorn Performance Enhancements
One of the additions that Java 8 brought about was Nashorn. If you’re unfamiliar, “Nashorn’s goal is to implement a lightweight high-performance JavaScript runtime in Java with a native JVM.” In the 8u40 release, Oracle released enhancements to the Nashorn scripting engine to improve the working relationship between Java and JavaScript. Despite being completely different languages, there would be a sense of satisfaction to having both of them work well together, given their names. Like a cat on a catamaran, or something.
With the update, Nashorn has been rewritten as an optimistic type system to better handle some of the challenges of implementing JavaScript in a Java environment (namely around outputting a dynamic language like JavaScript as Java bytecode). With optimistic typing, Nashorn makes optimistic assumptions for local variable and expression types that can’t be inferred statically. It assumes any type is int and adjusts these assumptions on the fly if they turn out to be wrong at runtime.
This enhancement leads to much speedier execution code, albeit at a slight cost to warmup time that’s caused by the adjustments. As reported by the Nashorn team, warmup time issues are one of the focuses of Java 9, so if that’s a big issue for you, good news may be coming on the horizon. Obviously you’re going to have to decide if the warmup costs are worth it for you here, but the benchmarks Oracle has run for these Nashorn enhancements are very encouraging. They’re reporting an orders of magnitude improvement and near native performance in some cases when compared to V8.
Another potentially awesome benefit of these enhancements is that Nashorn is in a position to be extended to other dynamic languages beyond JavaScript. Some work still needs to be done on that front, but the foundation is there now with the optimistic typing system.
Oracle hasn’t set these enhancements as the default for Nashorn at this stage, so if you want to run Nashorn in this manner, use the argument –optimistic-types=true. If you want to read more about some of the details of this enhancement, check out Oracle’s blog on the matter here.
- Available since: Java 8 Update 40
2. Cooperative Memory Management (Commercial Feature)
Another addition Oracle has made to Java 8 is Cooperative Memory Management. This feature introduced the concept of “memory pressure” and is all about addressing situations where memory usage is reaching near maximum levels. Memory pressure is a representation of the available total memory of the system. The higher the memory pressure measurement, the closer the system is to running out of memory.
The JDK will track total memory usage and take steps to reduce it when the system gets close to running out of memory. Generally these steps will consist of reducing the Java heap size, which could have some effect on performance. This potential performance tradeoff is intentional in this case, for what that’s worth.
An important thing to note is that this is a commercial feature. If you’re a HotSpot or OpenJDK developer, this isn’t going to be available for you. You’re undoubtedly aware of some of the tensions between the commercial and open source sides of Oracle and Java, and this is another example of that. Cooperative Memory Management itself may be a commercial feature, but it drives and/or affects other enhancements that fall into the open source camp (such as the next one on the list that I’m talking about). Whether you see these additional changes as largely beneficial fallout of the commercial efforts or as hooks to try to pull open source users over to the commercial side, there are undeniably some opacity issues involved.
Oracle (understandably) doesn’t share their internal testing and benchmarks for their commercial features. But when these features spill over to changes made on the open source side and the answer to “why are these changes being made” is essentially “because we’re telling you it’s better, even though we won’t show you how,” you can see why some people get upset. It’s not that Oracle is being mustache-twirling devious or something, it’s just that the lack of transparency doesn’t jive with the open source world and hinders developers’ abilities to truly make informed decisions. Anyway, enough on that.
//Side rant over.
- Available since: Java 8 Update 40
3. MinHeapFreeRatio and MaxHeapFreeRatio flags made manageable
The addition of Cooperative Memory Management was a catalyst for other changes as well. One of those is making MaxHeapFreeRatio and MinHeapFreeRatio flags manageable by being able to adjust their values during runtime. With this update, you can control the values of these flags, which have changed their defaults from 70/40 to 100/0 respectively, in order for the JVM to better grow and shrink the Java heap. Unlike Cooperative Memory Management, managing these flags is not commercial and can be used separately. The commercial Cooperative Memory Management feature does make it easier to manage these across multiple JVM instances, but it is not required. Regardless, the changes to these flags should improve your experience managing your Java heap size in situations where having a dynamic or changing heap is necessary or beneficial.
Now, as with all manageable things, there is the risk of mismanagement leading to poor performance that doesn’t come when you have strictly set defaults. Gaining more control isn’t an ipso facto good thing. The tradeoff here is that being able to manage these flags can help you reduce your memory footprint at times of lesser use or scale it easier at times of higher use. That itself does provide a tangible benefit, even if you’re someone who’s inclined to question the widespread benefits of such commercially-driven changes (if you fall in this camp, I certainly don’t blame you for that). And if some trouble with these flags leads to errors or exceptions in your code, Takipi is here to find them for you!
- Available since: Java 8 Update 20
4. New JAXP processing limit property – maxElementDepth
The maxElementDepth property was added to JAXP (Java API for XML Processing) to address resource usage issues. It gives applications the ability to limit the maximum element depth for an xml file that they parse. When you set a limit, all JDK and JRE invocations will observe it.
This property can come in handy if you deal with occasionally gigantic xml files and don’t want to spend a lot of resources parsing the extra large ones that come around. It can also be used as a security or performance check to prevent your application from getting tied up by a monster file.
- Available since: Java 8 Update 11
5. Java Dependency Analysis Tool (jdeps)
The Java Dependency Analysis tool (jdeps) is a new command-line tool that developers can use to display the static dependencies of their applications and Java libraries. It provides a level of customization, so you can get a fairly extensive range of information from it. When you run it at default, it will show you the static dependencies of your application at the package level.
You can run jdeps on a more fine-grained or detailed level as well. For example, you can run it with a particular class specified in order to see all the static dependencies for that class. Or if you’re feeling frisky, you can use the -v or -verbose option to print all class-level dependencies. This naturally leads to a huge amount of text, so jdeps also provides a -dotoutput option for writing this all to a file.
Also of note is the -jdkinternals option, which shows you any class-level dependencies in JDK internal APIs.
See the jdeps page for more.
- Available since: Java 8 Update 11
Conclusion
Now there are of course more than these five additions in the six updates Oracle has released, and depending on the aspects of Java you’re using, I may have left out some big ones for you (such as the performance enhancements for JavaFX), but these are five pretty cool ones worth being aware of. I can’t say what’s coming down the pipe in future releases, but here’s to hoping for more cool things. If I missed any additions that you’ve found to be super useful, let me know.
Reference: | While You Were Sleeping: The Top New Java 8 Additions from our JCG partner Josh Dreyfuss at the Takipi blog. |