原文地址 https://javapapers.com/java/t...
In this tutorial we will go through the various type of Java garbage collectors available. Garbage collection is an automatic process in Java which relieves the programmer of object memory allocation and de-allocation chores. This is the third part in the garbage collection tutorial series. In the previous part 2 we saw about how garbage collection works in Java, it is an interesting read and I recommend you to go through it. In the part 1 introduction to Java garbage collection, we saw about the JVM architecture, heap memory model and surrounding Java terminologies.
本教程将完整介绍一遍各种不同的 Java 垃圾回收器类型。Java 中的垃圾回收是一种自动处理的机制,它帮助开发人员从手工分配和释放内存的苦差事中解脱出来。本文是系列教程的第三篇,在前文中我们了解了垃圾回收的运行机理,推荐感兴趣的读者先行阅读。而在第一篇垃圾回收介绍中,我们已经了解了 JVM 的架构、堆内存模型及相关的术语概念。
Java has four types of garbage collectors,
Java 拥有四种类型的垃圾回收器(Garbage Collector):
Each of these four types has its own advantages and disadvantages. Most importantly, we the programmers can choose the type of garbage collector to be used by the JVM. We can choose them by passing the choice as JVM argument. Each of these types differ largely and can provide completely different application performance. It is critical to understand each of these types of garbage collectors and use it rightly based on the application.
它们有着各自的优缺点,作为编程者我们可以自由选择让 JVM 使用哪种,选择的方式是通过 JVM 参数。不同的垃圾回收器处理方式差别很大,也会令应用的性能表现完全不同。因此理解每种类型的垃圾回收器是十分关键的,这样你才能为你的应用选择正确的垃圾回收器类型。
Serial garbage collector works by holding all the application threads. It is designed for the single-threaded environments. It uses just a single thread for garbage collection. The way it works by freezing all the application threads while doing garbage collection may not be suitable for a server environment. It is best suited for simple command-line programs.
序列垃圾回收器会阻塞应用的所有线程,其设计就是针对单线程应用的。垃圾回收本身也只有一个线程。这种在垃圾回收过程中阻塞应用全部线程的方式,可能不适合服务器环境,它更适合简单的命令行程序。
Turn on the -XX:+UseSerialGC JVM argument to use the serial garbage collector.
你可以通过 JVM 参数 -XX:+UseSerialGC
启用序列垃圾回收器。
Parallel garbage collector is also called as throughput collector. It is the default garbage collector of the JVM. Unlike serial garbage collector, this uses multiple threads for garbage collection. Similar to serial garbage collector this also freezes all the application threads while performing garbage collection.
平行垃圾回收器也称作吞吐量回收器,它是 JVM 的默认垃圾回收器(因此无需参数来启用)。与上面的序列垃圾回收器不同地方在于它使用多个线程来执行垃圾回收,而相同的地方是当执行垃圾回收时它也会冻结应用的所有线程。
Concurrent Mark Sweep (CMS) garbage collector uses multiple threads to scan the heap memory to mark instances for eviction and then sweep the marked instances. CMS garbage collector holds all the application threads in the following two scenarios only,
并发标记扫除(CMS)垃圾回收器使用多个线程来扫描堆内存,标记可回收的对象实例,然后将它们回收。CMS 垃圾回收器仅当处于下面两个场景时才会阻塞所有线程:
In comparison with parallel garbage collector, CMS collector uses more CPU to ensure better application throughput. If we can allocate more CPU for better performance then CMS garbage collector is the preferred choice over the parallel collector.
与平行垃圾回收器相比,CMS 回收器会消耗更多 CPU 以便保持更高的应用处理性能。如果我们有条件分配更多的 CPU 来改善性能,那么 CMS 垃圾收集器比起平行收集器来说是更好的选择。
Turn on the XX:+USeParNewGC JVM argument to use the CMS garbage collector.
你可以通过 JVM 参数 XX:+UseParNewGC
启用 CMS 垃圾回收器。
G1 garbage collector is used for large heap memory areas. It separates the heap memory into regions and does collection within them in parallel. G1 also does compacts the free heap space on the go just after reclaiming the memory. But CMS garbage collector compacts the memory on stop the world (STW) situations. G1 collector prioritizes the region based on most garbage first.
G1 垃圾回收器是针对大堆内存区设计的。它将堆内存拆分成多个区域,然后并发地执行垃圾回收。G1 在收回内存完成的同时就合并了内存区域当中的碎片,相比之下 CMS 垃圾回收器的合并过程则是需要 STW(即挂起所有线程)的。G1 回收器会根据垃圾最多的数量对区域进行优先级排序。
Turn on the –XX:+UseG1GC JVM argument to use the G1 garbage collector.
你可以通过 JVM 参数 –XX:+UseG1GC
启用 CMS 垃圾回收器。
Turn on the -XX:+UseStringDeduplication JVM argument while using G1 garbage collector. This optimizes the heap memory by removing duplicate String values to a single char[] array. This option is introduced in Java 8 u 20.
当使用 G1 垃圾收集器时,打开 -XX:+UseStringDeduplication
选项,能够通过删除重复字符串,只留下一个 char[] 数组的方式,来优化堆内存。该优化是从 java 8 u 20 引进的。
Given all the above four types of Java garbage collectors, which one to use depends on the application scenario, hardware available and the throughput requirements.
以上就是四种垃圾收集器类型的全部介绍,选择哪种取决于你的应用场景、硬件能力和吞吐量需求。
Following are the key JVM options that are related to Java garbage collection.
下面总结一下与 Java 垃圾收集有关的关键参数:
Option | Description |
---|---|
-XX:+UseSerialGC | Serial Garbage Collector |
-XX:+UseParallelGC | Parallel Garbage Collector |
-XX:+UseConcMarkSweepGC | CMS Garbage Collector |
-XX:ParallelCMSThreads= | CMS Collector – number of threads to use |
-XX:+UseG1GC | G1 Gargbage Collector |
Option | Description |
---|---|
-Xms | Initial heap memory size |
-Xmx | Maximum heap memory size |
-Xmn | Size of Young Generation |
-XX:PermSize | Initial Permanent Generation size |
-XX:MaxPermSize | Maximum Permanent Generation size |
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseSerialGC -jar java-application.jar
In the next part of this Java garbage collection tutorial series, we will see about how to monitor and analyze the garbage collection with an example Java application.
在接下来的部分,我们将通过一个例子,来了解如何监控和分析垃圾收集。