5 Available Collectors
The discussion to this point has been about the serial collector. The Java HotSpot VM includes other types of collectors, each with different performance characteristics.
Serial Collector
The serial collector uses a single thread to perform all garbage collection work, which makes it relatively efficient because there is no communication overhead between threads.
It's best-suited to single processor machines because it can't take advantage of multiprocessor hardware, although it can be useful on multiprocessors for applications with small data sets (up to approximately 100 MB). The serial collector can be explicitly enabled with the option -XX:+UseSerialGC.
Parallel Collector
The parallel collector is also known as throughput collector, it's a generational collector similar to the serial collector. The primary difference between the serial and parallel collectors is that the parallel collector has multiple threads that are used to speed up garbage collection.
The parallel collector is intended for applications with medium-sized to large-sized data sets that are run on multiprocessor or multithreaded hardware. You can enable it by using the -XX:+UseParallelGC option.
Garbage-First (G1) Garbage Collector
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 computers to large multiprocessor computers 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. It can be explicitly enabled using -XX:+UseG1GC .
The Z Garbage Collector
ZGC provides max pause times under a millisecond, 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 works well for heap sizes from a few hundred megabytes
to 16TB To enable this, use the -XX:+UseZGC
option.
Selecting a Collector
Unless your application has rather strict pause-time requirements, first run your application with the default G1 collector.
If necessary, adjust the heap size to improve performance. If the performance still doesn't meet your goals, then use the following guidelines as a starting point for selecting a collector:
-
Constrained environments (like a single CPU or less than a few hundred MB of physical memory) may benefit from using the Serial collector. The G1 collector selected by default typically performs as well as the Serial collector.
-
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 the VM 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.
These guidelines provide only a starting point for selecting a collector because performance is dependent on the size of the heap, the amount of live data maintained by the application, and the number and speed of available processors.
If the recommended collector doesn't achieve the desired performance, then first attempt to adjust the heap and generation sizes to meet the desired goals. If performance is still inadequate, then try a different collector: Use the concurrent collector to reduce pause-time, and use the parallel collector to increase overall throughput on multiprocessor hardware.