参考官网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.
标签:收集器,XX,线程,compaction,JVM,parallel,垃圾 From: https://www.cnblogs.com/ilovebath/p/18219633