The introduction

Previously, we briefly looked at the classification and combinations of garbage collectors. Before we begin our discussion, we need to mention that the JVM runs in two modes: client mode and server mode. By default, we use client mode. We can use the -server command to force server mode on. The biggest difference is that the JVM does a lot of optimization in server mode. Now let’s take a closer look at the various garbage collector running scenarios and their characteristics.

Various collector features

        Serial Garbage Collector

Features: Single thread is used to reclaim memory. Single thread also means lower complexity and occupies less memory, and it also means that the advantages of multi-core cannot be effectively taken advantage of.

Application scenario: The serial collector is suitable for scenarios with low heap memory and single-core or even multi-core cpus.

Enable parameters: -xx :+UseSerialGC (default value in client mode)

        Serial Old Garbage Collector

Features: This Collector uses a mark-collation algorithm for collecting old-age regions, but other features of the Serial Garbage Collector are not covered here.

        ParNew Garbage Collector

Features: This collection is a parallel collector, a garbage collector that focuses on throughput and is the preferred collector for the new generation of virtual machines in Server mode.

Application scenario: On a medium-large heap with at least one processor.

Enable parameter: -xx :+UseParNewGC

        Parallel Scavenge Garbage Collector

Features: The Collector is almost identical to the ParNew Garbage Collector, except that the parameters can be set differently. It provides more precise control over GC pause times and throughput. The Parallel Exploiture collector provides an insane argument ** -xx :+UseAdaptiveSizePolicy**. There is no need to manually specify the size of the Cenozoic (Xmn), Eden and Survivor area ratio (- XX: SurvivorRatio), promotion of age old s (- XX: PretenureSizeThreshold) detail parameters, such as, The virtual machine collects performance monitoring information based on the current system performance and adjusts these parameters dynamically to provide the most appropriate pause times or maximum throughput. This approach is called GC Ergonomics.

        Parallel Old Garbage Collector

Features: It is the only older collector other than Serial Old that can work with the Parallel Insane, and to avoid The Serial Old insane reputation for controlled throughput, The Parallel Old act as the Parallel Avenge.

Turn on the parameter: use the -xx: -useparalleloldgc parameter to turn on the parallel avenge avenge. After JDK6, it is also the default generation collector after the parallel avenge avenge.

        Concurrent Mark Sweep Garbage Collector

Features: it is the only collector that adopts the mark/clear algorithm in the aged generation. It is very suitable for the server application with large heap memory and many CPU cores, concurrent collection and low pause.

Disadvantages: Very CPU resource sensitive, unable to handle floating garbage, space debris caused by mark-sweep algorithms.

        Garbage-First

* * * * features: parallel and concurrent (can make full use of the CPU, hardware multicore environment advantage), generational collection, spatial integration (won’t produce memory space debris), predictable pauses (can be explicitly specified in a length of M segment within milliseconds, time spent in GC shall not exceed the N milliseconds), across the entire heap memory, avoid whole heap of scanning.

conclusion

Let’s summarize their characteristics with a picture below:

The collector Serial, parallel or concurrent New generation/old age algorithm The target Applicable scenario
Serial serial The new generation Replication algorithm Speed of response priority Client mode in a single-CPU environment
Serial Old serial The old s Mark-tidy Speed of response priority Client mode in single CPU environment, backup scheme of CMS
ParNew parallel The new generation Replication algorithm Speed of response priority In a multi-CPU environment, it works with the CMS in Server mode
Parallel Scavenge parallel The new generation Replication algorithm Throughput priority Computations in the background without much interaction
Parallel Old parallel The old s Mark-tidy Throughput priority Computations in the background without much interaction
CMS concurrent The old s Mark-clear Speed of response priority Java applications concentrated on Internet sites or B/S system servers
G1 concurrent both Mark-tidy + copy algorithm Speed of response priority Server oriented applications, replacing CMS in the future

conclusion

This concludes the chapter, and in the next chapter we’ll take a look at what parameters the garbage collector has and the common tuning configurations for those parameters.