Garbage collection is one of the key concepts of Java programming and up to now the JDK ships with four different garbage collectors that all have their advantages and disadvantages. In this article, we will look at a new, the fifth garbage collector which plans to become the one and the only one we need.
In JDK 12 a new garbage collector, the Shenandoah Garbage Collector is being introduced as experimental. Shenandoah will introduce a more reliable and predictable short garbage collection pause which is independent of the heap size.
For JDK 12 it won’t be provided in the Oracle OpenJDK distribution. Most likely because it is experimental and Oracle wants to have a smaller test segment.
However, I did get the opportunity to chat with one of the garbage collection developers at Oracle at a conference lately, and he was optimistic that from JDK 13, and onwards the plan is that it will become the default garbage collector. For the experimental release in JDK 12, there are currently no known bugs at all.
Why Shenandoah is so efficient
Shenandoah is extremely good at performing work concurrently. The new garbage collector does the bulk of the work concurrently, which also includes the concurrent compaction, and this is how Shenandoah can achieve very short garbage collection pauses. One of the major benefits of performing most of the work concurrently is that Shenandoah is as quick and predictable even if the heap size is of 2GB or 200GB.
If you are interested in reading more in detail about how Shenandoah is implemented, then I recommend reading about it here.
How to enable Shenandoah
If you do use a build of the JDK which does include the new garbage collector Shenandoah, then you can enable it with the following argument.
$ java -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC
If you do this on a build which doesn’t include it, you will get the following error.
$ java -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC Error occurred during initialization of VM Option -XX:+UseShenandoahGC not supported
Future of Java Garbage Collection
To date, four different garbage collectors are shipped in the default binaries.
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
All of these different garbage collectors have their own advantages and disadvantages but it has become a nightmare for the JDK developers to maintain all of these. A developer on the garbage collection team literally said that no one wanted to touch the CMS code as it was a mess. And therefore, it doesn’t receive much attention anymore.
The Oracle garbage collection team plan is to eventually deprecate and remove the older garbage collector as Shenandoah is good enough to replace all of them. Of course, this won’t be done today or tomorrow as Shenandoah is only experimental. But it is understandable that Oracle doesn’t want to support all these old different garbage collectors if they can deliver something that is better for most people in most cases.