oreillymedia
Published in

oreillymedia

Java Programming from a JVM Performance Perspective

How to create well functioning applications and keep costs down

Editor’s Note: Understanding Java performance is a key skill for developers who want to create well functioning applications and keep costs down. Java Champion and performance expert Monica Beckwith offers her tips for making the most of JVM performance in this piece from 97 Things Every Java Programmer Should Know.

Tip #1: Don’t Obsess Over Garbage

I find that sometimes Java developers obsess over the amount of garbage their applications produce. Very few cases warrant this sort of obsession. A garbage collector (GC) helps the Java Virtual Machine (JVM) in memory management. For OpenJDK HotSpot VM, the GC along with the dynamic just-in-time (JIT) tiered compiler (client (C1) + server class (C2)) and the interpreter make up its execution engine. There are a slew of optimizations that a dynamic compiler can perform on your behalf. For example, C2 can utilize dynamic branch prediction and have a probability (“always” or “never”) for code branches taken (or not). Similarly, C2 excels in optimizations related to constants, loops, copies, deoptimizations, and so on.

Trust the adaptive compiler, but when in doubt verify using “serviceability,” “observability,” logging, and all the other such tools that we have thanks to our rich ecosystem.

What matters to a GC is an object’s liveness/age, its “popularity,” the “live set size” for your application, the long-lived transients, allocation rate, marking overhead, your promotion rate (for the generational collector), and so forth.

Tip #2: Characterize and Validate Your Benchmarks

A peer of mine once brought in some observations of a benchmarking suite with various sub-benchmarks. One of these was characterized as a “start-up and related” benchmark. After taking a look at the performance numbers and the premise that was the comparison between OpenJDK 8u and OpenJDK 11u LTS releases, I realized that the difference in numbers could have been due to the default GC changing from Parallel GC to G1 GC. So, it seems that the (sub-)benchmark either was not properly characterized or wasn’t validated. Both are important benchmarking exercises and help identify and isolate the “unit of test” (UoT) from other components of the test system that could act as detractors.

Tip #3: Allocation Size and Rate Still Matter

In order to be able to get to the bottom of the issue discussed above, I asked to see the GC logs. Within minutes, it was clear that the (fixed) region size, which is based on the heap size of the application, was categorizing the “regular” objects as “humongous.” For the G1 GC, humongous objects are objects that span 50% or more of a G1 region. Such objects don’t follow the fast path for allocations and are allocated out of the old generation. Hence, allocation size matters for regionalized GCs.

A GC keeps up with the live object graph mutation and moves objects from the “From” space into the “To” space. If your application is allocating at a rate faster than your GC’s (concurrent) marking algorithm can keep up with, then that can become a problem. Also, a generational GC may prematurely promote short-lived objects or not age transients properly due to the influx of allocations. OpenJDK’s G1 GC is still working on not being dependent on its fallback, fail-safe, nonincremental, full heap traversing, (parallel) stop-the-world collector.

Tip #4: An Adaptive JVM Is Your Right and You Should Demand It

It’s great to see an adaptive JIT and all the advancements geared toward start-up, ramp-up, JIT availability, and footprint optimizations. Similarly, various GC-level algorithmic smartness is available. Those GCs that aren’t there yet should get there soon, but that won’t happen without our help. As Java developers, please provide feedback on your use case to the community and help drive innovation in this area. Also, do test out the features that are continually getting added to the JIT.

Learn faster. Dig deeper. See farther.

Join the O’Reilly online learning platform. Get a free trial today and find answers on the fly, or master something new and useful.

Learn more

Monica Beckwith is a Java Champion, First Lego League Coach, and coauthor of Java Performance Companion (Addison-Wesley). She is the sole author of the upcoming Java 11 LTS+ — A Performance Perspective. She is passionate about JVM performance at Microsoft.

--

--

--

O’Reilly Media on Medium is a space curated by O’Reilly editors, offering advice, insights, viewpoints and experiences on tech.

Recommended from Medium

Crazy 5 years of Software Development

10 reason why python development can benefit your business

Trigonometry —  Core Graphics —  Xcode iOS — Part-1

Why Code Review?

KYVE. Explanatory story. Testnet short guide.

Agile, and the failure of the Russian Waterfall battle management in Ukraine.

How to Backup Your WordPress Site Manually or Using a Plugin?

Backup the Source Code of Your Website

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
O'Reilly Media

O'Reilly Media

O'Reilly Media spreads the knowledge of innovators through its books, video training, webcasts, events, and research.

More from Medium

Java Reflection, part 1: primitives, arrays, classes

Common hashCode Issues Every Java Developer Faces

Learning about hashCode and equals.

Is Java dying ? .. maybe not!!