Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Java 12 Released with Experimental Switch Expressions and Shenandoah GC

Java 12 Released with Experimental Switch Expressions and Shenandoah GC

This item in japanese

Lire ce contenu en français

Java 12, the latest feature release of Java, was released on schedule, on March 19th. With the new release comes a number of new and noteworthy features and refinements. Specifically, Java 12 includes a new language feature called Switch Expressions (as a preview feature), a new low-pause garbage collector called Shenandoah (experimental), and improvements to the default G1 garbage collector.

As per Oracle's new naming scheme and rapid-release cycle, this version arrives just six months after Java 11 and it is not considered a long-term support (LTS) release, so will be supported for only six months.

Switch Expressions

Switch Expressions are a new language feature that builds upon and improves the existing switch statement. They allow for a more concise and less verbose way of expressing a multi-way conditional. They’re written using a bit of newly introduced syntax:

int value = switch (number) {
    case ONE   -> 1;
    case TWO   -> 2;
    case THREE -> 3;

Notice the use of the arrow (->) instead of a colon (:), and the lack of break statements. Switch expressions do not have fall through semantics. Instead, each label must yield a value, and each possible value for the variable being tested must correspond to a branch in the switch.

Given this fact, most switch expressions will require a default label. However, the default label can be omitted when using a switch expression with an enum type, where the branches cover all possible cases. Full details of switch expressions were provided in a recent InfoQ article.

Note that switch expressions are still considered a preview feature as of Java 12, and so to use them in your programs, the command-line switch --enable-preview must be passed to the javac compiler.

A New Garbage Collector: Shenandoah

Java 12 includes a new garbage collector, Shenandoah, that strives to be "a low-pause-time garbage collector". It works by attempting to run more concurrently with application threads in a Java program to perform its garbage collection duties (evacuation, marking, compacting, etc). By doing this, remaining work, which is not run concurrently, should result in only short pauses.

Applications which require responsiveness and predictable pauses are good candidates for using Shenandoah. It’s also worth noting that the goal for the Red Hat team that contributed Shenandoah is that "pause times with Shenandoah are independent of heap size, meaning you will have the same consistent pause times whether your heap is 200 MB or 200 GB", but actual performance will depend on the actual heap size and workload.

Shenandoah is currently marked as an experimental project and must be enabled with the -XX:+UnlockExperimentalVMOptions. Red Hat is credited with the initial implementation and will continue to support it for both the aarch64 architecture and the amd64 architecture.

Existing Garbage Collector Improvements

Not only did Java 12 come with a new garbage collector, but improvements were made to the existing G1 garbage collector. The enhancement proposals JEP 344 and JEP 346 were both included in the release.

The first of the two, JEP 344, improves on the way the G1 garbage collector meets its time goals for collection pauses. The problem it solves is when the G1 garbage collector mistakenly selects an amount of work (via application heuristics) that is not achievable within the defined pause time. In these cases, exceeding the pause time goal cannot be avoided.

To improve on this, the G1 garbage collector now detects if it repeatedly selects an incorrect amount of work and adjusts. It does this by breaking up the work into mandatory and optional collections and allows G1 to stop its work at any point while performing work on the optional collection.

The second improvement, JEP 346, improves the memory usage of the G1 garbage collector by returning unused Java heap memory to the operating system (OS) during periods of inactivity.

Prior to this improvement, the G1 collector rarely returned Java heap memory to the OS because it only does so during a full garbage collection. To achieve this, the G1 collector now makes better use of its idle time to return unused Java heap memory to the OS.

Additional Enhancements

In addition to the two features presented above, six other enhancements have been included in the Java 12 release:

The build of Oracle JDK 12 and the Oracle OpenJDK 12 are available now, with other providers following very soon.

Rate this Article