Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand garbage collector

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand the garbage collector". The content in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to understand the garbage collector".

(1) Overview

If the garbage collection algorithm is the theory of memory collection, then the garbage collector is the concrete implementation of memory collection.

At present, there are many garbage collectors, but still no collector is omnipotent, so we can only choose the one that is most suitable for application.

Here are seven kinds of garbage collectors used in mainstream Java virtual machines: Serial, parNew, ParallelScavenge, SerialOld, ParallelOld, CMS, G1. Some of these garbage collectors are suitable for the new generation, some are suitable for the old age, and some are suitable for both the new generation and the old age. As shown in the following figure, the connection means that it can be used together.

(2) Serial

Serial is a single-threaded collector, and the characteristic of Serial is that it must "Stop the World" when it does garbage collection, which means that when the garbage collector starts working, all other worker threads must be stopped. It may sound unreliable, but for scenarios that limit a single CPU, this approach is simple and efficient. For simple desktop applications, the memory allocated to virtual machines will not be very large, and for the new generation of one or two hundred megabytes, the garbage collection time of Serial can be controlled within one hundred milliseconds, which is basically insignificant to users.

The Serial collector uses replication algorithms in the new generation.

(3) ParNew

The ParNew garbage collector is a multithreaded version of Serial that uses multiple threads for garbage collection. In addition, basically the same as Serial, ParNew still needs * * "Stop the World" * * when collecting garbage with multiple threads. ParNew can use the-XX:ParallelGCThreads parameter to limit the number of threads for garbage collection.

ParNew collectors use replication algorithms in the new generation

(4) Parallel Scavenge

Parallel Scavenge is also a new generation collector as well as a multithreaded collector, but unlike ParNew, Parallel Scavenge collectors focus on a controllable throughput (Throughput). The so-called throughput refers to the proportion of the time CPU spent running code to the total time spent by CPU.

Throughput = time to run code / (time to run code + garbage collection time)

In theory, the higher the throughput, the less the user will feel the pause time.

Parallel Scavenge provides two parameters to control throughput:-XX:MaxGCPauseMillis and * *-XX:GCTimeRatio**

-XX:MaxGCPauseMillis sets the maximum millisecond time spent on memory collection, but don't blindly think that garbage collection will be faster as long as the value is set very low. This pause time is bought at the expense of throughput and new generation space.

-XX:GCTimeRatio represents garbage collection time as a percentage of total time, (1 percent 100), that is, the reciprocal of throughput. The default value is 99, which allows a maximum of 1% of the junk phone time (1 / (1 / 99)).

There is another parameter * *-XX:+UseAdaptiveSizePolicy**, when this parameter is turned on, there is no need to set the parameters such as the size of the new generation and the age of the older object, so the Parallel Scavenge collector is also called the throughput first garbage collector.

Parallel Scavenge adopts replication algorithm.

(5) Serial Old

As soon as you hear the name, you can tell that this is an old version of the Serial collector, a single-threaded collector, using the tag-collation algorithm, and the rest are basically the same as the new Serial.

(VI) Parallel Old

The older version of the Parallel Scavenge collector, the multithreaded collector, uses the mark-collation algorithm and is also a throughput priority.

(7) CMS collector

CMS (Concurrent Mark Sweep) collector is a kind of collector whose goal is to obtain the shortest recovery pause time. CMS is an old-fashioned garbage collector based on tag-cleanup algorithm. CMS is the most widely used old-fashioned garbage collector at present.

1. Initial tag: marks objects to which GC Roots can be directly associated, with a high speed (stop the world)

2. Concurrent tagging: the process of root search algorithm

3. Re-tagging: in order to correct the object that the tag changes due to the running of the program during the concurrent marking period. (stop the world)

4. Concurrent cleanup: garbage removal

The longest time consuming in this process is the process of concurrent marking and concurrent cleanup, but it does not stop the world, and the initial identification and relabeling are very fast, and even stop the world does not take much time.

Its advantages are concurrent collection, concurrent cleanup, and low pause.

But it has three obvious disadvantages:

1. Very sensitive to CPU resources, because concurrent marking and concurrent cleanup are run at the same time as the program, which will take up CPU and cause the application to slow down.

2. Floating garbage cannot be processed. Floating garbage is the newly generated garbage in the process of concurrent cleaning. This part of garbage CMS cannot be cleaned up this time, and an Concurrent Mode Failed error may occur. Therefore, a certain amount of memory space needs to be reserved and cannot be cleared until the old age is almost full. By default, CMS is activated after 68% use in the old years. You can set-XX:CMSInitiatingOccupancyFraction to set this value.

3. To generate space debris, because the mark-removal algorithm is used, the problem of space debris can not be avoided, which will bring difficulties to the allocation of large objects.

(8) G1

The above garbage collectors are basically distinguished by the new generation and the old age, but G1 is different.

Reactor structure

The heap structure of G1 is to divide a whole memory area into several fixed-size blocks. JVM generally divides the heap into 2000 region, and then each region ranges from 1m to 32m.

Allocation of memory

All region is divided into Eden, Survivor, Old, and Humongous, and the understanding of Eden, Survivor, and Old is understood by other garbage collectors. Here is a new type, Humongous, which is mainly used to store objects 50 percent or more larger than standard blocks.

YGC in G1

On the first YGC, the surviving objects in the Eden block are transferred to one or more survivor blocks, and the survival time reaches the threshold, and these objects are promoted to the old age. The younger generation of GC is carried out in parallel through multiple threads.

At this point, there will be a stop the world pause, accounting for the Eden size and survivor size for the next young GC. The statistics are saved to assist in the calculation of size. Indicators such as pause time will also be taken into account.

Once a Cenozoic recovery occurs, the entire Cenozoic generation will be recycled (based on the predicted pause time, the size of the Cenozoic generation may change dynamically)

Old-fashioned garbage collection in G1

Old-age recycling will not recycle all the old space, but will only choose some of the Region with the highest returns. When recycling, it will usually hitchhike-- put the old Region to be recycled together with all the new generation Region for recycling. This process is generally called Mixed GC.

The old garbage collection in G1 is very similar to the CMS collector.

1. Initial tag: attach to the normal YGC process to mark all the roots. (stop the world)

2. Scan the root area: scan the references and referenced objects marked by the initial tag pointing to the old age in the Survivor Regions. This stage is executed concurrently, but must be completed before the occurrence of the younger generation of GC. (stop the world)

3. Concurrent tags: look for living elements throughout the heap, which can be interrupted by YGC

4. Re-tagging: a CMS-like relabeling that deals with new object references generated during the concurrent tagging phase, which uses the SATB (snapshot-at-the-beginning) algorithm, which is much faster than the one used in CMS. (stop the world)

5. Cleanup phase: G1 GC will identify completely idle areas and areas available for mixed garbage collection for cleaning. (stop the world)

As you can see, there are four stages that require stop the world. In order to reduce the time of stop the world, G1 uses RSet (Remembered Set) to record the reference relationships between different generations.

RSet

RSet records "who quoted me", and RSet records the following two citations:

1. Citations between Region in the old days

2. The reference from the old Region to the new Region, which is directly added to the GC Roots when Young GC.

How RSet works is that when doing Young GC, you choose the Region of the new generation as the GC Roots. The RSet in these Region records the cross-generation references of the old age-> the new generation ("who quoted me"), thus avoiding scanning the whole old age. When doing Mixed GC, the reference between "Old Age-> Old Age" can be obtained through the RSet record in the Region to be recycled, and the reference between "Cenozoic-> Old Age" is obtained by scanning all the new generations (as mentioned earlier, Mixed GC will hitchhike with Young GC), and there is no need to scan all the old ages. In short, with the introduction of RSet, the heap scan range of GC is greatly reduced.

Thank you for your reading. the above is the content of "how to understand the garbage collector". After the study of this article, I believe you have a deeper understanding of how to understand the garbage collector, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report