Introduction to HotSpot algorithm and garbage collector in java

Source: Internet
Author: User
Tags data structures garbage collection sleep


Basic idea of the garbage collection algorithm:

1. Enumerate root nodes (GC Roots)
During garbage collection, we need to find out which objects are alive. Generally, some objects called GC Root are selected and enumerated from these objects. During enumeration, all objects must be stopped, that is, the "Stop the world" called by everyone ". All algorithm implementations stop Virtual Machines. Otherwise, the accuracy of the analysis results cannot be guaranteed. After the execution of the system pauses, the virtual machine does not need to traverse all the root nodes and context to determine the GC Roots, but has an OopMap data structure to achieve this goal. When the class is loaded, the VM calculates the offset of the class and the type of data. During JIT compilation, the location of the reference in the register and stack is also recorded in a specific position. GC can directly obtain the information during scanning.

2. Security points
Because it is safe to stop a program when it is running (for example, when the calculation is half done, the data value is a dirty data ), A security point is a safe point that all threads reach when they Stop the world. Because the objects in the heap are huge, if an OopMap data structure is generated for each object, it takes a lot of space. Therefore, HotSpot only generates these data structures on the "security point. At the same time, the program can be stopped not at all locations, but only at security points. Therefore, "security points" also affect the timeliness of GC ." The security point "cannot be too many when selected, resulting in a small amount of space expenditure, so that GC can wait for a long time.
Another issue that needs to be considered for "security points" is that when GC occurs, how to make all threads run to the nearest "security point" is generally two solutions. Preemptive interruption: the virtual machine stops all threads and checks whether security points have been reached one by one. If security points have not been reached, the recovery thread will make them to the nearest "security point ", this method is rarely used by virtual machines. The idea of active interruption is that when the interruption occurs, the virtual machine sets a flag on all threads, the thread checks the flag on its own, and then enters the "security point ". The inspection mark is located at the same place as the security point.

3. Security zone
The problem not solved above is that when a thread is in sleep or no CPU clock is allocated, such as sleep or blocked status, it cannot go to the security point to suspend itself. In this case, we need to solve the problem through the security zone. The security zone is a region where a program does not change its own reference range. GC is safe to start anywhere in this region, it can be considered an extended security point. When the thread executes the SafeRegion code, it will mark itself as Safe Region. GC will not care about these threads. When he is about to leave the security zone, he will check the current status. If it is in GC, he will wait for the signal to exit Safe Region, otherwise, he can continue running.

II. Introduction to the garbage collector algorithm
JDK1.7 contains the following garbage collection processors:
A
The figure above shows the garbage collector graph in jdk1.7. The two are connected to indicate that they can be used in combination. The Recycler also applies to the young and old generations because the available regions are divided into the young and old generations. In addition, there are no advantages or disadvantages of the garbage collection processor, and only applicable and not applicable.
Before introducing the garbage collector, describe two concepts.
Parallel (Parallel): it refers to the Parallel operation of multiple garbage collection processors, but the user thread is paused at this time.
Concurrent: indicates that the user thread and the garbage collection thread are executed simultaneously (or alternately), and the user thread may run on this CPU, the garbage collection thread runs on another CPU.

Garbage collector of young generation

1. Serial collector (old generation single-thread collector)

A single-threaded collector will pause all working threads during collection until the collection ends.
The replication algorithm is used when the Serial collection is young. Pause all user threads
Note: it is the default new generation collector in the client mode, because the memory managed in the desktop mode is relatively small, dozens of MB, and the single thread is less interactive than multi-thread collection, higher efficiency. Therefore, single-threaded mode is a good choice.

2. ParNew collector (the young generation multithreading collector)

ParNew is the multi-threaded version of Serial. The new generation of collection uses the replication algorithm, and all user threads are suspended during collection. He is the default garbage collection processor of the young generation in server mode. One important reason is that he can only work with the CMS processor of the old generation.
In a single CPU environment, the ParNew processor never performs better than Serial. Because there is a thread interaction cost, but with the increase in the number of CPUs, the use is still very good.

3. Parllel Scavenge collector

The Parllel Scavenge collector is also a parallel multi-thread collector. It seems that there is no essential difference with the ParNew collector, but its goal is different. His goal is to achieve a controllable throughput. Throughput = running code Time/(running code Time + garbage collection time ). In fact, high throughput refers to the high utilization of CPU. But it does not mean that the user experience must be the best, because a single pause may be very long. It has two parameters to control:

MaxGCPauseMillis: the expected maximum GC pause time. When this time is low, it reduces the pause time by tuning the new generation (collecting M is faster than collecting m workers, however, this will cause garbage collection to happen more frequently. It was once every 10 seconds and stopped for 100 ms at a time. Now it stops every 5 seconds and stops for 70 s at a time ), but it will reduce the overall throughput.
GCTimeRatio: the ratio of user time to GC time is configured. For example, 19 indicates that the ratio of GC time to user time is. The maximum GC time allowed is 1/(1 + 19), that is, 5%.
-XX: + UseAdaptiveSizePolicy: Another parameter that can be configured for this collector. The virtual opportunity automatically adjusts the size of the young and old generations according to the running conditions, to provide the most suitable pause time or maximum throughput. This is called GC adaptive adjustment policy.
That is to say, you only need to set the throughput target and the managed maximum heap size for the virtual machine, and then let the adaptive adjustment policy automatically optimize it. This is an important difference with the ParNew collector.

Garbage collector of the old generation

1. Serial Old

Serial Old is a single-threaded collector of the Old generation. During collection, the "mark-organize" algorithm is used to suspend all user threads. The main significance of his existence is that it is used by virtual machines in Client mode, because it is the same as the default collector in client mode in serial collector.
In Server mode, there are two other users. One is used with the Parallel Scavenge collector. The other is the backup plan for the CMS collector, which is used in the Concurrent Mode Failure of CMS.

2. Parallel Old collector

Parallel Old is an Old version of the Parallel Scavenge collector. It uses a multi-threaded "mark-arrangement" algorithm. They are suitable for supporting use. Since Parallel Scavenge cannot be used with CMS, it can only be used with Serial Old. Therefore, the Parallel Old collector introduced in jdk1.6 fills this gap.

3. CMS (Concurrent Mark Sweep) Concurrent Mark cleaning collector

CMS is a collector designed to obtain the minimum pause time. Most Internet applications tend to provide users with a better interactive experience, so they hope to have a short pause to bring a better experience. Its implementation is more complex, divided into the following steps

Stop the world)
Concurrent tag
Stop the world)
Concurrent cleanup
Except step 1 and Step 1 are paused, and other steps are concurrent. The initial tag is mainly the object directly associated with the marked GC Root. The concurrent mark is the GC Root Tracing process. Retag is used to modify the changes caused by the concurrent running of user threads during the concurrent tag, which takes a long time, but is far shorter than the concurrent tag. In the whole process, the time-consuming concurrent mark and concurrent purge stages are both concurrent with the user process, so it reduces the pause time slightly.
In general, CMS is a concurrent and low-pause collector, but it is far from perfect. He has the following disadvantages:

CPU is sensitive and takes up system CPU resources. Generally, four threads (CPU + 3) and four threads are enabled during concurrent recovery, resulting in reduced total throughput.
CMS cannot process floating garbage. A Full GC may occur due to a "Concurrent Mode Failure" Failure. Floating garbage is the newly generated garbage during the recycling process, which is only waiting for the next cleaning. In addition, because the user thread runs concurrently, it also needs memory to support it, so the collector will start to work at a low usage, so that there is space for concurrent use of the program during garbage collection. Use-XX: CMSInitiatingOccupancyFraction to modify the trigger ratio. The JDK1.6 setting ratio is 92%. If the memory reserved by CMS during running cannot meet the program usage, "Concurrent Mode Failure" will appear, and an alternative Serial Old will be started for garbage collection, this will take a longer time, so a suitable ratio will increase the throughput.
Using the "mark-Sweep" algorithm generates fragments. In this case, all user threads need to be paused for sorting, which consumes more time.
4. G1 Collector

G1 is a server-oriented garbage collector and is regarded as a successor of the CMS collector. It has the following features.

Concurrency and concurrency. Make full use of multiple CPUs to shorten the pause time for garbage collection. The JAVA program can continue to run in concurrency mode if the pause is required.
Generation-based collection. G1 does not need to be used with other collectors. Different recycling algorithms are used internally based on the survival length of objects.
Spatial Integration. In general, it is based on the "tag-sorting" algorithm, and in part, it is based on the "copy" algorithm. This method reduces the generation of fragments.
Predictable pause. In addition to achieving the goal of minimizing pauses as much as possible, a predictable pause model is also established.
Next let's take a look at how G1.
The internal memory of the G1 Collector is quite different from that of the previous collector. It divides the entire heap into multiple equal independent regions (Region ). Although there are still concepts of the new generation and the old generation, there is no physical isolation between them. They are a set of Region (which does not need to be continuous.
Why can G1 predict the pause time of garbage collection in a planned manner. He will scan various regions to analyze the value of Garbage collection and maintain a priority list in the background. Only the areas with high recycling value can be started. This is also the origin of Garbage-First name.
Overall, the idea of G1. However, its implementation is complicated because Region is referenced from each other, and retrieving a Region needs to traverse other Region contents.
Use Remebered Set in Region in G1 Collector to avoid full heap scanning. When a VM discovers that it writes the referenced data, it will generate a Write Barrier to temporarily interrupt the Write operation and check whether it references different Region, if yes, use CardTable to record the relevant information in the Rember Set of the Region to which the referenced object belongs. When garbage collection is performed, increasing the Rember Set in the GC Root enumerated range will not be omitted. If you do not remember the maintenance operations on the Remembered Set, the collection steps are roughly divided into the following steps:

Initial tag
Concurrent tag
Final mark
Filter and recycle
Initial tag

In this phase, only the objects associated with GC Roots are marked and the value of TAMS (Next Top at Mark Start) is modified. Allows concurrent users in the next stage to create new objects on the correct Region. This process requires thread suspension, but takes a short time.
Concurrent tag
Traverse the GC Roots associated objects, determine the accessibility, find the surviving objects, and be concurrent.
Final mark
Changes made during the concurrent flag phase are recorded in the Remembered Set Log. Merge the Log data into the Remembered Set.
Filter and recycle
This stage is the stage for evaluating the recovery value. The user thread will be paused during collection, because it is recycled for a single sub-Region. Due to a small amount of time controllable, stopping the thread will improve the collection efficiency. Concurrency may be possible in the future.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.