JDK12 垃圾收集

Garbage Collection in JDK 12 and onward
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.
垃圾收集是Java程序裏的一個關鍵技術點,目前jdk中有四種垃圾收集器,每一種都有自己的優點和缺點,我們將接觸一種新的垃圾收集器,第五中垃圾收集器,計劃變成我們唯一需要的一種。

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.
在jdk 12 中實驗性的引入了Shenandoah 垃圾收集器,新的垃圾收集器,更可靠,更好的預測垃圾收集的暫停 ,並且和堆的大小無關。

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.
對於jdk12 新的垃圾收集器不會在oracle 版本的中提供,很可能由於它具有實驗的性質。oracle需要更多的測試。

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 爲什麼如此不同

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.

原文:https://www.thecuriousdev.org/garbage-collection-in-jdk-12-and-onward/#more-974

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章