1. Introduction
1.绪论
Java 11 introduced a No-Op Garbage Collector called Epsilon, which promises the lowest GC overhead possible.
Java 11 引入了名为 Epsilon 的No-Op 垃圾收集器,它承诺将 GC 的开销降至最低。
In this short tutorial, we’ll explore how Epsilon works, and we’ll mention the common use cases.
在这个简短的教程中,我们将探讨Epsilon是如何工作的,我们将提到常见的使用案例。
2. Quick Hands-On
2.快速动手能力
Let’s start with getting our hands dirty, and take Epsilon GC for a spin!
让我们从动手做起,带着Epsilon GC去转一转吧!
We’ll first need an application, which creates garbage:
我们首先需要一个应用程序,它可以创建垃圾。
class MemoryPolluter {
static final int MEGABYTE_IN_BYTES = 1024 * 1024;
static final int ITERATION_COUNT = 1024 * 10;
static void main(String[] args) {
System.out.println("Starting pollution");
for (int i = 0; i < ITERATION_COUNT; i++) {
byte[] array = new byte[MEGABYTE_IN_BYTES];
}
System.out.println("Terminating");
}
}
This code creates one-megabyte-arrays in a loop. Since we repeat the loop 10240 times, it means we allocate 10 gigabytes of memory, which is probably higher than the available maximum heap size.
这段代码在一个循环中创建了一百万字节的数组。由于我们重复循环10240次,这意味着我们分配了10千兆字节的内存,这可能高于可用的最大堆大小。
We also provided some helper prints to see when the application terminates.
我们还提供了一些辅助打印,以查看应用程序何时终止。
To enable Epsilon GC, we need to pass the following VM arguments:
为了启用Epsilon GC,我们需要传递以下虚拟机参数:。
-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
And when we run the application, we get the following error:
而当我们运行该应用程序时,我们得到以下错误。
Starting pollution
Terminating due to java.lang.OutOfMemoryError: Java heap space
However, when we run the same application with the standard VM options, it completes fine:
然而,当我们用标准的虚拟机选项运行同一个应用程序时,它完成得很好。
Starting pollution
Terminating
Why did the first run fail? It seems like even the most basic garbage collectors could clean up the child’s play that we just demonstrated!
为什么第一次运行会失败?似乎即使是最基本的垃圾收集器也能清理掉我们刚刚演示的儿戏!。
So, let’s take a look at the concepts behind Epsilon GC to understand what just happened.
因此,让我们看看Epsilon GC背后的概念,以了解刚才发生的事情。
3. How Epsilon GC Works
3.Epsilon GC如何工作
Epsilon is a no-op garbage collector.
Epsilon是一个无操作的垃圾收集器。
JEP 318 explains that “[Epsilon] … handles memory allocation but does not implement any actual memory reclamation mechanism. Once the available Java heap is exhausted, the JVM will shut down.”
JEP 318解释说:”[Epsilon] …处理内存分配,但不实现任何实际的内存回收机制。一旦可用的Java堆被耗尽,JVM将关闭。“
So, this explains why our application terminated with an OutOfMemoryError.
因此,这就解释了为什么我们的应用程序以OutOfMemoryError.终止。
But, it raises the question: Why do we need to have a garbage collector, that doesn’t collect any garbage?
但是,这提出了一个问题。为什么我们需要有一个不收集任何垃圾的垃圾收集器?
There are some cases when we know that the available heap will be enough, so we don’t want the JVM to use resources to run GC tasks.
有些情况下,我们知道可用的堆将是足够的,所以我们不希望JVM使用资源来运行GC任务。
Some examples of such cases (also from the related JEP):
这种情况的一些例子(也来自相关的JEP)。
- Performance testing
- Memory pressure testing
- VM interface testing
- Extremely short lived jobs
- Last-drop latency improvements
- Last-drop throughput improvements
4. Conclusion
4.总结
In this short article, we learned about Epsilon, a no-op GC available in Java 11. We learned about the implications of using it and reviewed some cases where it may be handy.
在这篇短文中,我们了解了Epsilon,一个在Java 11中可用的no-op GC。我们了解了使用它的意义,并回顾了一些它可能很方便的情况。
As usual, the examples are available over on GitHub.
像往常一样,这些例子可以在GitHub上找到over。