1.7.1 Determining the Death of an object

(There are too many statements in G1 garbage collection mechanism, this article is summarized for some blogs, there are personal understanding and mistakes, I am sorry)

Reference counting (mainly used in Redis)

Accessibility analysis algorithm

As an object of GC Roots:

  1. Virtual machine stack: An object referenced by the local variable table in a frame

  2. Objects referenced by native methods

  3. Objects referenced by static variables and constants in the method area

1.7.2 Strong Reference, Soft Reference, Weak Reference, and Virtual Reference

  1. Strong references are not recyclable
  2. Soft references are usually used as buffers and are not reclaimed if there is enough memory
  3. Weak references are reclaimed as soon as found (used in ThreadLoacal)
  4. Virtual references are used to track objects as they are garbage collected

1.7.2 Reclaiming a Class

  1. All instances are reclaimed
  2. The ClassLoader that loaded the class has been reclaimed
  3. The corresponding Class object is not used elsewhere

1.7.3 Garbage collection algorithm

  1. Mark clearing algorithm
  2. Replication algorithm
  3. Tag sorting algorithm
  4. Generational algorithm

1.7.4 Garbage collector

JDK8 ps + Po by default

1. Serial collector

concept

A collector that uses a single thread for garbage collection. The serial collector has only one worker thread at a time of collection. The serial collector’s focus and exclusivity tend to perform better on computers with less parallelism. Serial collectors can be used in the new generation and old age, according to the different heap space, divided into the new generation of serial collectors and old age collectors.

-xx :+UseSerialGC: Serial, Serial Old

Serial collector:

The Serial collector is a new generation collector that executes single-threaded and uses a replication algorithm. It must suspend all other worker threads (user threads) while garbage is collected. Is the default generation collector in Jvm Client mode. For environments that are limited to a single CPU, the Serial collector can achieve maximum single-threaded collection efficiency by concentrating on garbage collection because there is no overhead of thread interaction.

Serial Old collector

1. An older version of the Serial collector, which is also a single-threaded collector and uses a mark-tidy algorithm. 2. It is mainly used for virtual machines in Client mode. 3. In Server mode, there are two main uses: The Application is used in JDK 1.5 and earlier with the Parallel Scavenge collector and as a fallback to the CMS collector in the event of Concurrent Mode failures in Concurrent collections.

2. Parallel collector

concept

ParNew collector

The multithreaded version of the Serial collector

2. Single CPU is inferior to Serial because of the overhead of thread interaction

-xx :+UseParNewGC -xx :+UseParNewGC

-xx :ParallelGCThreads=n Sets the number of cpus used by the parallel collector while collecting. Collect the number of threads in parallel. It is generally best to match the CPU of the computer

Parallel avenge

-xx :+UseParallelGC The new generation uses the parallel collector, the old generation uses the serial collector

1. Throughput first collector

2, the new generation collector, copy algorithm, parallel multi-threaded collector

3. The goal is to achieve a controllable Throughput.

Throughput = run user code time/(run user code time + garbage collection time). If the virtual machine runs for 100 minutes and garbage collection takes 1 minute, then the throughput is 99%.

5. Two parameters are used to accurately control throughput:

-xx: MaxGCPauseMillis controls the maximum garbage collection pause time

-xx: GCTimeRatio Directly sets the throughput size

-xx: +UseAdaptiveSizePolicy Dynamically sets the size of the new generation, the ratio between Eden and Survivor area, and the promotion age of the old age object

Parallel: Multiple garbage collection threads work in Parallel while the user thread is still waiting.

Concurrent: The execution of both the user thread and the garbage collector thread at the same time (but not necessarily in parallel and may occur alternately), with the user program continuing to run while the garbage collector is running on another CPU.

Parallel Old collector

-xx :+UseParallelOldGC Both new generation and old generation use the parallel collection collector

An older version of the Parallel Avenge collector, which uses multithreading and a mark-and-collate algorithm.

The Parallel Avenge avenge and Parallel Old collector can be a priority in the insane and CPU-sensitive applications.

CMS collector

A collector whose goal is to obtain the shortest collection pause time.

2, very consistent with the Internet site or B/S system on the server, pay attention to the response speed of the service, hope the system pause time shortest application

3, based on ** “mark – clear” ** algorithm

4. The CMS collector’s memory collection process is executed concurrently with the user thread

5. Its operation process is divided into four steps, including:

The initial tag, “Stop The World,” simply marks objects that GC Roots can be directly associated with, which is fast

Concurrent marking, the concurrent marking stage is the process of GC RootsTracing

Re-marking, Stop The World, is to correct The marking record for that part of The object that was marked during The concurrent marking period because The user program continued to operate, but for a much shorter time than The concurrent marking period

CMS Concurrent sweep (CMS concurrent sweep)

Very sensitive to CPU resources. Floating garbage cannot be handled and a “Concurrent Mode Failure” may occur, resulting in another Full GC.


A collector based on the “mark – clear” algorithm

-xx :+UseConcMarkSweepGC Applies the CMS collector

-xx :ConcGCThreads Sets the number of concurrent threads

– XX: CMSInitiatingOccupancyFraction Settings when old s space utility rate of percentage for a CMS, the default value is 68, when the old s space utilization rate reached 68%, will perform a CMS

If memory usage is growing very quickly, in the process of CMS implementation, there are already out of memory, the CMS recycling will fail, the virtual machine will start the old s serial collector for recycling, this led to the suspension of the application, until after the garbage collection will work normally, the process of GC pause times may be longer, Therefore, set the value based on the actual situation.

– XX: + UseCMSCompactAtFullCollection set CMS in garbage collection after the completion of a memory defragmentation

– XX: CMSFullGCsBeforeCompaction set how many times in the CMS after recovery, a memory compression.

3. G1 (garbage-first) collector

1. One of the most cutting-edge achievements in the development of collector technology today

G1 is a garbage collector for server-side applications.

3. Advantages:

Parallel and concurrency: Take advantage of hardware in a multi-CPU, multi-core environment generational collection: Independently manages the entire GC heap space without the cooperation of other collectors Integration: ** Collectors implemented by the "mark-tidy" algorithm, locally based on the "copy" algorithm do not generate memory space fragmentation ** Predictable pauses: ** allows the user to explicitly specify that no more than N milliseconds ** should be spent on garbage collection within a time segment of M millisecondsCopy the code

4. The OPERATION of the G1 collector can be roughly divided into the following steps:

Initial tag: mark objects that GC Roots can be directly associated with, requiring a short pause of the thread. Concurrent tag: Perform a reachabability analysis of objects in the heap from GC Root to find viable objects. This stage takes longer, but the final tag can be executed concurrently with the user program: Fixed the part of the tag record that changes the tag as the user program continues to run during concurrent tagging: sorting the value and cost of the collection for each Region, and planning the collection based on the expected GC pause time of the userCopy the code

-xx :+UserG1Gc applies the G1 collector

-xx :MaxGCPauseMillis Specifies the maximum pause time

-xx :ParallelGCThreads Sets the number of threads to be recycled in parallel

3.1 Features of G1

1 Generation and partition

Each partition can be a young generation or an old generation, but can only belong to one generation at a time. Young generation, survival area, these concepts are old s, made the concept of logic, logic of generational framework before for easy reuse, do not need to be continuous in the physical, the additional benefits of the rubbish inside – some partition object much more special, some objects rarely rubbish inside partition, G1 will recycle object special priority number of partitions, It takes less time to recycle the garbage from those partitions, which is why G1 gets its name from the idea of collecting the most garbage first.

For the new generation, it is not the use of this algorithm, is still the new generation of full recycling, the whole new generation of objects, either recycling, or promotion, as for the new generation also adopted the partition mechanism, because it is consistent with the old strategy, convenient adjustment of generation size.

G1 is also a collector with compression, reclaiming older partitions by copying surviving objects from one partition to another, a process that achieves local compression. Each partition varies in size from 1M to 32M, but is 2 x.

2 Cset and Rset

A collection set (CSet) represents a series of target partitions that are reclaimed each time a GC pauses.

During any collection pause, all partitions of the CSet are freed, and internal surviving objects are moved to the allocated free partition. So whether young generation collection or mixed collection, the working mechanism is the same. The young generation collection CSet only contains the partitions of the young generation, while the mixed collection will select the partitions with the highest recycling returns from the candidate recycling partitions of the old generation through the heuristic algorithm and add them to the CSet.

Rsets record the relationships between objects in other regions and objects in this Region. Rsets belong to the points-into structure (who references my object).

The value of the RSet is that the garbage collector does not need to scan the entire heap to find out who references objects in the current partition, just scan the RSet.

If G1 is collecting objects in a Cset, it needs to know whether the objects in the Cset are referenced by anyone (if they are referenced, they can’t be recycled), so it needs to have a pointer that refers to me

As shown in the figure below, the objects in Region1 and Region3 both refer to objects in Region2, so both references are recorded in Region2’s RSet.

Region1 references region2, and records the card table information of Region1 to the card table class of Region2. You can directly locate a location of Region1 (the location corresponding to the entry), avoiding scanning of the whole Region1

A region corresponds to multiple entries

Cards in different regions reference each other. Objects in Cards in Region1 reference objects in Cards in Region2. The solid blue line shows the points-out relationship. In Region2’s RSet, the Card of Region1, represented by the red dotted line, is recorded, which is points-into. The reference relationships in rsets are maintained by post-write barriers and Concurrent refinement Threads.

The post-write barrier records cross-region reference updates, and the update log buffer records Cards that contain reference updates. Once the buffers are full, the post-write barrier is no longer in service and the buffer logs are processed by Concurrent Refinement Threads. How exactly does RSet assist GC? When doing YGC, you only need to select rsets of the Young Generation region as the root set. These Rsets record cross-generation references of old->young, avoiding scanning the whole old generation. However, in mixed GC, RSet of old->old is recorded in old generation, and reference of young->old is obtained by scanning all young generation regions, so it is not necessary to scan all old generation regions. So the introduction of Rsets greatly reduces the amount of GC work.

The object for GC Root can only be in two places when GC is performed: Regions in CSet and non-Cset. CSet is a collection of regions to be collected, but non-Cset regions do not need to be collected. If the GC Root object is in a Region in CSet, it traverses the reachable objects in sequence. If GC Root is in a non-Cset, references from non-Cset to CSet objects need to be traversed, that is, scanning all non-Cset regions. Time-consuming. If an RSet is available, you can scan the Rsets of all regions in the CSet to know the references of other regions that are not involved in the collection to objects in the CSet, avoiding global scanning for regions that are not involved in the collection

So Cset Rset CardTable

Cset represents the Region to be collected. Since G1 is garbage first, Cset can be understood as some regions with the highest cost performance for collection. During collection, because we need to know who references me, Rset exists to indicate who references me, avoiding scanning the whole heap space. The Rset records the position of the Cardtable, the entry to the area where an object is located. (Personal understanding, for the garbage collector, too much information is too miscellaneous, many errors included in it, many blogs are different, unless you look at the source code, basically can not understand are a bit of a problem)

3. The card table

A card page can contain multiple objects. As long as there is a cross-generation pointer in the field of an object, the element identifier of its corresponding card table becomes 1, indicating that the element is dirty, otherwise, it is 0.

When GC, simply filter the dirty elements from the card table in the collection area and add them to the GCRoots.

Maintenance of card form

Hotspot maintains card table state using write barriers.

In the G1 heap, there is data for a CardTable, which is implemented by an array of elements 1B, called cards/card pages. This CardTable maps to the entire heap, and each card corresponds to 512 bytes of heap space.

As shown in the figure below, under a heap size of 1GB, the length of CardTable is 2097151 (1GB / 512B); Each Region has a size of 1 MB, and each Region has 2048 Card pages.

4. STAB technology

Let’s introduce it together

5. Write barriers

When a reference is changed, the cards in the reference table are updated, making them dirty and pushing them into a queue for subsequent updates to the Rset

3.2 young GC

3.3 MixGC

  • Initial-mark, the phase in which the application undergoes STW, is usually followed by a new generation collection, in other words — since both phases require pauses in the application, the G1 GC reuses the new generation collection to do the initial marking. Initial tagging in the new generation garbage collection makes the pause time slightly longer and increases CPU overhead. What the initial tag does is set the values of the two TAMS variables (NTAMS and PTAMS), and all objects above TAMS are recognized as implicitly alive during this concurrent cycle;
  • Root-region-scan (ROOt-region-scan), this process does not need to suspend the application. Objects copied to survivor partitions in the initial tag or in the new generation collection need to be regarded as roots. At this stage, G1 scans survivor partitions. All objects referenced by survivor partitions are scanned and marked. Survivor partition is the root partition. Because of this, new generation collection cannot occur at this stage. If the space of the new generation happens to be used up when the root partition is scanned, garbage collection of the new generation must wait for the completion of the root partition scanning. If logs are found to alternate root partition scans and logs collected by the new generation, the current application needs to be tuned.
  • Concurrent-mark, the concurrent-mark stage is multi-threaded and we can pass it-XX:ConcGCThreadsBy default, the G1 garbage collector sets this total to the number of concurrent garbage threads (-XX:ParallelGCThreadsA quarter of); The concurrent tag uses the trace algorithm to find all living objects and records them in a bitmap. Since objects above TAMS are considered alive implicitly, we only need to traverse those below TAMS; The SATB idea is to set a snapshot at the beginning, and then trace it based on the snapshot, assuming that the snapshot does not change. At this time, if the reference of an object changes, Pre-write barrier logs to record the object’s old values in a SATB buffer. If the buffer is full, it is added to a global list that G1 has concurrent tagged threads periodically processing.
  • Remarking phase, the final marking phase in which the entire application is paused, the G1 garbage collector processes the remaining SATB log buffers and any updated references, and the G1 garbage collector finds any unmarked living objects. This stage is also responsible for reference processing and so on.
  • Cleanup phase. The actual amount of memory reclaimed during the cleanup phase is very small, and up to this point, the G1 garbage collector mainly marks which older generations partitions can be reclaimed, sorting older generations from smallest to largest in liVENESS. This process also does several things: identifies all free partitions, combs through rsets, offloads unused classes from Metaspace, recyls giant objects, and so on. One advantage of identifying objects alive in each partition is that when a completely free partition is encountered, its RSet can be immediately cleaned up, and the partition can be immediately reclaimed and released into the idle queue, rather than being put into a CSet for collection in the mixed collection phase. Combing through the RSet helps you find unwanted references.

3.4 Specific Process

Note that youngGC is sent during the Mix GC

3.5 Giant Objects

Giant Objects: In G1, an Object is defined as a Humongous Object if it is more than half the size of the partition. If the size of an object exceeds the size of one partition, two contiguous partitions are directly allocated to store the object in the old partition. Large partitions must be contiguous and cannot be moved after allocation – no good.

Due to the presence of giant objects, partitions in G1’s heap are divided into three types: Cenozoic, old age, and giant, as shown below:

1.7.5 Three-color marking method

The three-color marking method is mainly used for CMS and G1 to determine whether the object is alive or not in the concurrent marking stage

White: indicates that the object has not been accessed by the garbage collector. Obviously, at the beginning of the reachability analysis, all objects are white, and if at the end of the analysis, the objects are still white, that is, they are unreachable.

Black: Indicates that the object has been accessed by the garbage collector and that all references to the object have been scanned. The black object indicates that it has been scanned, and it is safe to live. If there are other object references to the black object, there is no need to scan again. A black object cannot point directly to a white object (without passing the gray object).

Gray: Indicates that an object has been accessed by the garbage collector, but at least one reference to the object has not been scanned.

1. Main problems of concurrent markup

As shown in the figure above, the B — >C connection breaks, which creates A — C connection. In this case, C will be garbage collected when C is not

This leads to two scenarios

2. Incremental updates

When B- “C” is disconnected and A- “C” is connected, the black A becomes gray, and A needs to be rewritten to be scanned (CMS rescan phase solves this problem).

3. SATB

When B- “C” is disconnected and A- “C” is connected, change the black “C” to gray, so that C will not be recycled, if C is not used, then C is floating garbage, which will be recycled next time, but definitely not this time

4. Conclusion

Incremental update means that when a black object inserts a new reference to a white object, the newly inserted reference is recorded. After the concurrent scan, the black object in these recorded reference relationships is scanned again. This can be simplified to say that a black object becomes a gray object as soon as a new reference to a white object is inserted.

The original snapshot is to record the reference to be deleted when the gray object wants to delete the reference to the white object. After the concurrent scanning, the gray object in the recorded reference relationship is scanned again, so that the white object can be scanned. Mark white objects as black (so that they can survive the current gc cleanup and be rescan in the next GC, which may also be floating garbage)

SATB, which is to record the reference state at that time, so that the gray can retouch the white and the white to black live through the current GC

CMS is to change the black to gray ()

In G1, it turns white into black

5. Related questions

Why is G1 SATB? CMS with incremental updates?

SATB is more efficient than incremental update (of course, SATB may cause more floating garbage), because there is no need to deeply scan the deleted reference object again in the re-marking stage, while CMS will deeply scan the root object of incremental reference. G1 Because many objects are located in different regions, CMS is an old age region. The cost of G1 to rescan objects in depth is higher than that of CMS, so G1 chooses SATB non-depth scanning objects, simply marks them, and waits for the next GC to perform deep scanning. CMS has certain problems

supplement