JVM 垃圾收集器分类
参考官网https://docs.oracle.com/en/java/javase/17/gctuning/introduction-garbage-collection-tuning.html。垃圾收集器可以分为以下四类:
1、Serial收集器
这个收集器是一个单线程工作的收集器, 但它的“单线 程”的意义并不仅仅是说明它只会使用一个处理器或一条收集线程去完成垃圾收集工作。更重要的是强调在它进行垃圾收集时, 必须暂停其他所有工作线程 直到它收集结束——这项工作是由虚拟机在后台自动发起和自动完成的。在用户不可知、 不可控的情况下把用户的正常工作的线程全部停掉, 这对很多应用来说都是不能接受的。
Serial收集器缺点明显,其优点也同样明——简单并且高效:对于单CPU环境来说,由于Serial收集器没有线程间的交互,专心做垃圾收集自然可以做获得最高的垃圾收集效率。
使用方式:-XX:+UseSerialGC。
所以其最适用的场景是:It's best-suited to single processor machines because it can't take advantage of multiprocessor hardware。
2、Parallel收集器
这个收集器同时也被认为是一个吞吐量收集器,其与serial 收集器的主要区别在于该收集器是多线程的。多个垃圾收集器线程并行工作,同样会暂停用户线程,适用于科学计算、大数据后台处理等多交互场景。
使用方式:- XX:+UseParallelGC
-XX:-UseParallelOldGC
以上两种使用方式的区别在于parallel compaction。第一种方式模式使用parallel compaction,第二种方式禁用parallel compaction。
Parallel compaction is a feature that enables the parallel collector to perform major collections in parallel. Without parallel compaction, major collections are performed using a single thread, which can significantly limit scalability. Parallel compaction is enabled by default if the option -XX:+UseParallelGC has been specified. You can disable it by using the -XX:-UseParallelOldGC option.
3、G1收集器
G1垃圾回收器将堆内存分割成不同的区域然后并发地对其进行垃圾回收。
G1 is a mostly concurrent collector. Mostly concurrent collectors perform some expensive work concurrently to the application. This collector is designed to scale from small machines to large multiprocessor machines with a large amount of memory. It provides the capability to meet a pause-time goal with high probability, while achieving high throughput.
G1 is selected by default on most hardware and operating system configurations, or can be explicitly enabled using -XX:+UseG1GC .
4、The Z收集器
The Z Garbage Collector (ZGC) is a scalable low latency garbage collector. ZGC performs all expensive work concurrently, without stopping the execution of application threads.
ZGC provides max pause times of a few milliseconds, but at the cost of some throughput. It is intended for applications, which require low latency. Pause times are independent of heap size that is being used. ZGC supports heap sizes from 8MB to 16TB. To enable this, use the -XX:+UseZGC option.
如何选择垃圾收集器呢?除非应用程序对暂停时间有相当严苛的要求,否则由VM自主选择收集器(Unless your application has rather strict pause-time requirements, first run your application and allow the VM to select a collector)。
如必要,可以调整通过heap size提高性能。如性能仍无法达到预期目标,可以采取如下指导方针作为选择收集器的出发点:
• If the application has a small data set (up to approximately 100 MB), then selectthe serial collector with the option -XX:+UseSerialGC.
• If the application will be run on a single processor and there are no pause-timerequirements, then select the serial collector with the option -XX:+UseSerialGC.
• If (a) peak application performance is the first priority and (b) there are no pause time requirements or pauses of one second or longer are acceptable, then let theVM select the collector or select the parallel collector with -XX:+UseParallelGC.
• If response time is more important than overall throughput and garbage collection pauses must be kept shorter, then select the mostly concurrent collector with -XX:+UseG1GC.
• If response time is a high priority, then select a fully concurrent collector with -XX:UseZGC.
关于更多关于收集器的高级选项参考https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html#advanced-garbage-collection-options-for-java