preface

Text has been included to my lot warehouse, welcome Star:https://github.com/bin392328206/six-finger plant a tree is the best time ten years ago, the second is writing a blog on the way now encourage everyone in technology

omg

Garbage First(G1) is the latest in a long line of Garbage collection and HotSpot’s push for a Garbage collector on the JVM that replaces CMS. If you are using Java 8/9, you will most likely want to evaluate the G1 collector. This article begins with a detailed summary of the JVM’s other garbage collectors and a brief comparison with G1. Then, the working mechanism of G1 is introduced through the memory model and activity cycle of G1. At the same time, it also describes the optimization points that may need attention. The blogger hopes that this article will enable readers with a certain JVM foundation to grasp the knowledge of G1 as soon as possible.

A review and comparison of JVM GC collectors

Garbage collector up to JDK1.8 is shown below


In fact, one of their iterative processes is along with the evolution of technology

Serial collector combination Serial + Serial Old

Enable options: -xx :+SerialGC

The serial collector is the most basic, oldest, and proven garbage collector, and is the default collector configuration in client mode.

The serial collector collects in a single-threaded stop-the-world fashion. When the memory is low, the serial GC sets the pause flag. When all threads enter the Safepoint, the application thread pauses and the serial GC begins to reclaim space and declutter the memory in a single-thread manner. Single threading also means less complexity and less memory footprint, but it also means you can’t take advantage of multiple cores. In fact, the serial collector is particularly well suited for applications with low heap memory, single – or even dual-core cpus.

Parallel collector combine the Parallel Avenge + Parallel Old

Enable options: -xx :+UseParallelGC or -xx :+UseParallelOldGC(mutually active)

The Parallel collector is a garbage collector that focuses on throughput and is the default collector configuration in server mode. This focus on throughput is primarily reflected in the younger generation of the Parallel Collector.

The parallel collector works in a stop-the-world fashion similar to the serial collector, except that the garbage is collected in parallel when paused. The young generation uses the copy algorithm, the old generation uses the mark-collation, in the collection at the same time will compress the memory. The focus on throughput is primarily on the younger generation of the Parallel Scavenge collector, which adjusts the size of the Cengen insane using two target parameters -XX:MaxGCPauseMills and -xx :GCTimeRatio to reduce the frequency of GC triggering. The parallel collector is suitable for scenarios where throughput requirements are much higher than latency requirements, and will provide the best throughput when the worst latency is met.

Concurrent marker clearing collector combination ParNew + CMS + Serial Old

Enable options: -xx :+UseConcMarkSweepGC

Concurrent mark clearing (CMS) is a very excellent garbage collection algorithm with the goal of focusing on delay. After it is enabled, the young generation uses STW-type parallel collection, while the old generation uses CMS for garbage collection. The attention on delay is mainly reflected in the old generation CMS.

The younger generation of ParNew is similar to the parallel collector, whereas the older CMS went through each collection cycle: initial marking, concurrent marking, re-marking, and concurrent cleaning. Where the initial tag marks all root objects as STW; The concurrent mark is parallel with the application thread to mark the reachable path of the root object. Before garbage collection, CMS re-marks reachable objects with a STW that may have been missed due to modification by the mutator thread. The resulting unreachable objects are recycled in the concurrent cleanup phase. It is worth noting that both initial tagging and re-tagging have been optimized for multi-threaded execution. CMS is ideal for server-side applications with large heap memory and CPU cores, and was the preferred collector for large applications prior to G1.

But CMS is not perfect, it has the following disadvantages:

  1. Due to concurrently, CMS threads at the same time will increase in the collection and application of heap memory footprint, that is to say, the CMS must be completed before the old s heap memory used up garbage collection, or CMS recovery fails, will trigger the guarantee mechanism, serial old s collector will be a in the form of STW GC, resulting in larger pause time;
  2. The marker clearing algorithm cannot clean up space debris, and the old chronospace will be gradually exhausted with the application time, and finally the heap memory will have to be compressed by guarantee mechanism. CMS also provides parameters – XX: CMSFullGCsBeForeCompaction (the default is 0, that is, every time memory consolidation) to specify how many times the CMS after collection, compression on a Full GC.

Garbage First (G1)

Enable options: -xx :+UseG1GC

The previous sets of garbage collector combinations have several things in common:

  • The young generation and the old generation are independent and continuous memory blocks;
  • Young generation collection uses single Eden and double survivor for replication algorithm;
  • Old age collection must scan the entire old age area;
  • Both are designed to execute GC as little as possible as quickly as possible.

The G1 garbage collector is also a latency focused, server-side application garbage collector that the HotSpot team has been tasked with replacing CMS and is also a garbage collector with great tuning potential. Although G1 has similar collection actions to CMS: initial tagging, concurrent tagging, relabelling, cleaning, and transfer collection, and also uses a serial collector as a guarantee mechanism, it is not appropriate to simply describe the process as similar to the first three. In fact, the G1 collection is quite different from the previous three sets of collectors:

  • The G1 was designed to “collect as much Garbage as possible First.” Therefore, G1 does not wait until memory runs out (serial, parallel) or is running out of memory (CMS) to start garbage collection, but rather internally – using a heuristic algorithm to find partitions with high collection benefits in old times. At the same time, G1 can automatically adjust the size of the young generation and the total heap according to the pause time-interval target set by the user. The shorter the pause target, the smaller the space of the young generation and the larger the total space.
  • G1 uses the Region concept to divide memory into memory partitions of equal size. When reclaiming memory, the system reclaims the memory in line based on the partition. Surviving objects are copied to another free partition. Since all operations are performed in units of equal size partitions, G1 day – is of course a compression scheme (local compression);
  • G1 is also a generational collector, but there is no physical distinction between young generation and old generation for the entire memory partition, nor does it require a completely separate -survivor (to space) heap to prepare for replication. G1 has only logical generational concepts, or the possibility that each partition can switch back and forth between different generations as G1 runs;
  • The collection of G1 is all STW, but the collection boundary between the young generation and the old generation is blurred, and a mixed collection method is adopted. That is, a per-collection can either collect only young generation partitions (young generation collection) or include part of old generation partitions (hybrid-collection) along with the young generation, so that even when heap memory is large, the collection scope can be limited and pauses can be reduced

G1 memory model

Partitioning concepts

As shown below:


partition

Partition Region

G1 uses the concept of regions to divide the entire heap space into several memory regions of equal size. Each allocation of object space uses memory segment by segment. Therefore, G1 does not require the storage of objects to be physically contiguous, just logically contiguous, in the use of the heap. Each partition also does not definitively serve a generation and can switch between the young generation and the old generation as needed. During startup, use -xx :G1HeapRegionSize=n to specify the partition size (1MB to 32MB, which must be a power of 2). The heap is divided into 2048 partitions by default.

card

Card Card

Each partition is divided into a number of 512 Byte cards, which indicate the minimum available granularity of heap memory. The cards of all partitions will be recorded in the Global Card Table, and the allocated objects will occupy a number of physically contiguous cards. When a reference to an object within a partition is found, the reference object is found through the record card (see RSet). Each memory reclamation is to process the cards in the specified partition.

The heap

Heap Heap

G1 can also specify the heap size with -xms / -xmx. When a young or mixed collection occurs, the heap size is automatically adjusted by calculating the elapsed time ratio between GC and application. If the GC frequency is too high, the GC frequency is reduced by increasing the heap size, and the time consumed by GC is correspondingly reduced. The target parameter -xx :GCTimeRatio is the ratio of GC to application time, which defaults to 9 for G1 and 99 for CMS because CMS is designed to spend as little time on GC as possible. In addition, when there is insufficient space, such as object space allocation or transfer failure, G1 will first attempt to increase heap space, and if capacity expansion fails, it will initiate a guaranteed Full GC. After Full GC, the heap size calculation results will also adjust the heap space.

Generational model


generational

Generational Generation

Generational garbage collection can focus on the most recently allocated objects without whole heap scanning, avoiding copies of long-lived objects, and independent collection helps reduce response times. While partitioning eliminates the need for compact memory allocation, the G1 still uses the idea of generations. Like other garbage collectors, G1 logically divides memory into young and old generations, with the young generation in turn divided into Eden space and Survivor space. However, the young space is not fixed, and when the existing young partition is full, the JVM allocates a new free partition to the young space.

The entire young generation memory dynamically changes between the initial space -xx :G1NewSizePercent(default heap 5%) and the maximum space -xx :G1MaxNewSizePercent(default 60%). It is calculated by the target pause time -xx :MaxGCPauseMillis(default: 200ms), the size to be expanded and the memory set (RSet) of the partition. Of course, G1 can still set a fixed young generation size (parameters -xx :NewRatio, -xmn), but pausing the target also makes no sense.

conclusion

In general, the G1 can let you to set up a recycling system, the influence of his own by split the memory for a large number of small Region, as well as the track can be recycled in each Region object size and estimate time, finally in garbage collection, as far as possible put the rubbish recycling the effects on the system control in your specified range, At the same time, try to recycle as many garbage objects as possible in the limited time

At the end

In fact, this is also a simple introduction, next we will talk about the online environment G1 garbage collector specific use and parameter Settings

I have a goal to write two or three articles a week. I hope I can keep it up for a year. I hope you can give me more suggestions so that I can learn more and make progress together.

Daily for praise

Ok, everybody, that’s all for this article, you can see people here, they are real fans.

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see in the next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!