Some of the concepts

Parallel

Refers to multiple garbage collection threads working in parallel while the user thread is still waiting.

Concurrent

When the user thread executes simultaneously (but not necessarily in parallel, but alternately) with the garbage collector thread, the user program continues to run while the garbage collector is running on another CPU.

throughput

The ratio of CPU time spent running user code to total CPU consumption, i.e. Throughput = time spent running user code/(time spent running user code + garbage collection time). In total, the virtual machine ran for 100 minutes, and garbage collection took 1 minute, which gives a throughput of 99%.

Garbage collector for the HotSpot VIRTUAL machine

Serial

  • The most basic single-threaded garbage collector. Use a CPU or a collection thread to perform garbage collection.
  • When they workStop The World, suspends all user threads, causing a lag. This mode is suitable for VMS running in Client mode.
  • Used as a new generation collector, copy the algorithm.

ParNew

  • A multithreaded version of the Serial collector, the only difference from Serial is that multiple threads are used for garbage collection.
  • In addition to Serial, it is the only collector that can be used with CMS.
  • Used as a new generation collector, copy the algorithm.

Parallel Scavenge

  • Used as a new generation collector, copy the algorithm.
  • Focus on high throughput, can efficiently use THE CPU time, as soon as possible to complete the operation of the program, mainly suitable for the background operation without too much interaction tasks.
  • The Parallel Scavenge collector provides two parameters to control throughput precisely, one for maximum garbage collection pause times- XX: MaxGCPauseMillisParameters as well as directly set the throughput size- XX: GCTimeRatioParameters.

Serial Old

  • An older version of the Serial collector, single-threaded, mark-collation algorithm.
  • This mode is used for VMS in Client mode.
  • When a VM is in Server mode, it provides two functions: The Application is used in JDK 1.5 and earlier with the Parallel Scavenge collector, and as a fallback to the CMS collector when Concurrent Mode failures occur in Concurrent collections.

Parallel Old

  • An older version of the Parallel Avenge collector, which uses multithreading and mark-collation algorithms. It was made available in JDK 1.6.
  • Use with the Parallel Insane for throughput applications.

CMS (Concurrent Mark Sweep)

  • A collector whose goal is to obtain the shortest collection pause time. Suitable for programs that need to interact with users, good response speed can improve user experience.
  • Based on mark-clear algorithm. Suitable as an old age collector.
  • The collection process is divided into four steps:
    • CMS Initial mark: Just mark objects that GC Roots can be directly associated with. It’s fastStop The World.
    • CMS Concurrent Mark: The process of GC Roots Tracing.
    • CMS Remark: YesStop The World. In order to correct the marking record of the part of the object that the marking changed during the concurrent marking as the user program continued to operate, the pause time in this phase is generally slightly longer than the initial marking phase, but much shorter than the concurrent marking phase.
    • CMS Concurrent sweep: Reclaims memory.
  • The collector thread, which takes the longest concurrent markup, and the concurrent cleanup process, can work with the user thread, so they are executed concurrently.
  • Disadvantages:
    • The concurrent phase does not cause user threads to pause, but occupies some threads (CPU resources), slowing down applications and reducing throughput. The default number of collection threads started is (number of cpus +3) /4. That is, when there are more than four cpus, the concurrent collection of garbage collection threads is not less than 25% of the CPU resources, and it decreases as the number of cpus increases. But when there are fewer than four cpus (say, two), the CMS’s impact on user programs can become significant.
    • Unable to remove floating garbage. In the concurrent cleanup phase, the user thread is still running and new garbage is generated. This garbage will not be marked in this GC and will have to wait until the next GC is collected.
    • The mark-clear algorithm produces a large amount of discontinuous memory, causing insufficient memory to allocate large objects and triggering the Full GC prematurely.

G1

  • Advanced garbage collector available in JDK1.7.
  • Suitable for both the new generation and the old age.
  • Space consolidation: No fragmentation space is created using the mark-de-clutter algorithm.
  • The entire Java heap is divided into regions of the same size. The Cenozoic era and the old age are no longer physically isolated, but a collection of regions.
  • By default, the heap is evenly divided into 2048 regions. The minimum size is 1 MB and the maximum size is 32 MB. The value must be a power of 2-XX:G1HeapRegionSizeParameter specified. Regions are divided into four types:
    • E: Eden District, the New generation
    • S: Survivor zone, new generation
    • O: Old age
    • H: Humongous area, used to zoom in on objects. If the size of a new object exceeds half of the region size, the object is allocated to one or more consecutive regions and marked with H
  • Predictable pause times: Estimate the amount of garbage available for collection in each region and how long it takes to collect it (rule of thumb), recorded in a priority list. When collecting, the region with the highest value is collected first, rather than region-wide collection across the entire heap. This improves the efficiency of recycling, known as garbage-first.
  • G1 has two types of GC:
    • Young GC: Triggered when there is not enough space available in the Eden zone of the New generation. The surviving object is moved to survivor zone or promoted to old zone.
    • Mixed GC: When there are many old objects in the heap, the ratio of the old object space to the total heap space reaches the threshold (-XX:InitiatingHeapOccupancyPercentDefault 45%), which reclaims not only the young generation but also parts of the old generation (regions with high value).
  • Mixed GC collection steps:
    • Initial Marking: just Mark the objects that GC Roots can be directly associated with, and modify the value of TAMS (Next Top at Mark Start) so that when the Next user program runs concurrently, new objects can be created in the correct available Region. This phase requires a pause thread (STW), but it is very short and shares the pauses of YGC, so it usually occurs with YGC.
    • Concurrent Marking: Perform reachability analysis to identify viable objects, which is time-consuming but can be executed concurrently with user threads.
    • Final Marking: Corrects the change record caused by the user thread running during the concurrent Marking phase. STW, but can be executed in parallel, not for very long.
    • Live Data Counting and Evacuation: Collections are ordered by recovery value and recovery cost for each region, and collections begin at user-defined GC pause times.
  • When objects are allocated too fast for the mixed GC to collect, G1 will degenerate and trigger the Full GC, which uses the single-threaded Serial collector to collect, the entire process STW, to avoid this situation.
  • When memory is low (live objects take up a lot of space) and there is not enough space to copy objects, recycling fails. Objects that have been moved and objects that have not been moved are kept, and only references are adjusted. When a failure occurs, the collector determines that the live object has been moved and that there is enough space for the application to use, and the user thread continues to work, waiting for the next GC to fire. If you run out of memory, the Full GC is triggered.

See G1 for details