Home > Java > javaTutorial > Java Virtual Machine Learning - Garbage Collector

Java Virtual Machine Learning - Garbage Collector

黄舟
Release: 2017-02-17 10:42:17
Original
1396 people have browsed it

HotSpot JVM Collector

There are 7 of the collectors above, divided into two pieces, the above is the new generation collector, and the following is the older collector. If there is a connection between two collectors, they can be used together.

Serial (serial GC) collector

The Serial collector is a new generation collector , single-threaded execution, using a copy algorithm. It must suspend all other worker threads (user threads) while performing garbage collection. It is the default new generation collector in Jvm client mode. For an environment limited to a single CPU, the Serial collector has no thread interaction overhead, so it can naturally achieve the highest single-thread collection efficiency by focusing on garbage collection.

ParNew (parallel GC) collector

ParNew collector is actually serial collection A multi-threaded version of the collector that behaves the same as the Serial collector except that it uses multiple threads for garbage collection.

Parallel Scavenge (parallel recycling GC) collector

Parallel Scavenge collector too A new generation collector, which is also a collector using a copy algorithm and a parallel multi-threaded collector. The characteristic of the parallel Scavenge collector is that its focus is different from other collectors. The focus of collectors such as CMS is to shorten the pause time of the user thread during garbage collection as much as possible, while the goal of the parallel Scavenge collector is to achieve a feasible Controlled throughput. Throughput = program running time/(program running time + garbage collection time), the virtual machine ran for a total of 100 minutes. Among them, garbage collection takes 1 minute, and the throughput is 99%.

##Serial Old (Serial GC) Collector


Serial Old is the old generation version of the Serial collector. It also uses a single thread to perform collection and uses the "mark-sort" algorithm. Mainly used in virtual machines in Client mode.

##Parallel Old (Parallel GC) Collector

Parallel Old is the old generation version of the Parallel Scavenge collector, using multi-threading and the "mark-sort" algorithm.

##CMS (Concurrent GC) Collector

CMS (Concurrent Mark Sweep) collector is a method to obtain the shortest collection The pause time is the target collector. The CMS collector is implemented based on the "mark-clear" algorithm. The entire collection process is roughly divided into 4 steps:

①.Initial mark (CMS initial mark)

##②.Concurrent mark (CMS concurrenr mark)

③.Remark (CMS remark)

④.Concurrent sweep (CMS concurrent sweep)

The two steps of initial marking and re-marking still require pausing other user threads. The initial marking only marks objects that GC ROOTS can directly associate with, which is very fast. The concurrent marking stage is the stage of GC ROOTS root search algorithm, which will determine whether the object is alive. The re-marking phase is to correct the marking records of that part of the objects that have changed due to the continued running of the user program during the concurrent marking period. The pause time in this phase will be slightly longer than the initial marking phase, but shorter than the concurrent marking phase. .

Due to the longest time-consuming concurrent marking and concurrent clearing processes in the entire process, the collector thread can work together with the user thread, so overall, the memory of the CMS collector The recycling process is executed concurrently with the user thread.

Advantages of the CMS collector: concurrent collection, low pauses, but CMS is far from perfect. The collector mainly has three significant shortcomings:

CMS collector is very sensitive to CPU resources. . In the concurrent phase, although it will not cause the user thread to pause, it will occupy CPU resources and cause the reference program to slow down and the total throughput to decrease. The number of recycling threads started by CMS by default is: (number of CPUs + 3) / 4.

CMS collector cannot handle floating garbage, and "Concurrent Mode Failure" may occur, which may lead to another Full GC after failure. Since the user thread is still running during the concurrent cleanup phase of CMS, new garbage will continue to be generated as the program runs and heats up. This part of garbage appears after the marking process. CMS cannot process it in this collection and has to wait for the next GC. Clean it up. This part of garbage is called "floating garbage". It is also because the user thread still needs to run during the garbage collection phase,
that is, sufficient memory space needs to be reserved for the user thread to use, so the CMS collector cannot wait until the old generation is almost completely filled like other collectors before collecting. , a portion of memory space needs to be reserved for program operation during concurrent collection. Under default settings, the CMS collector will be activated when 68% of the space in the old generation is used. You can also provide a trigger percentage through the value of the parameter -XX:CMSInitiatingOccupancyFraction to reduce the number of memory recycling times and improve performance. If the memory reserved during CMS operation cannot meet the needs of other threads of the program, a "Concurrent Mode Failure" failure will occur. At this time, the virtual machine will start a backup plan: temporarily enable the Serial Old collector to re-collect garbage in the old generation, so that The pause is very long. Therefore, setting the parameter -XX:CMSInitiatingOccupancyFraction too high will easily lead to "Concurrent Mode Failure" failure and reduce performance.

The last disadvantage is that CMS is a collector based on the "mark-clear" algorithm. After collecting using the "mark-clear" algorithm, a large amount of fragments will be generated. When there are too many space fragments, it will bring a lot of trouble to object allocation. For example, for large objects, the memory space cannot find contiguous space to allocate and has to trigger a Full GC in advance. In order to solve this problem, the CMS collector provides a -XX:UseCMSCompactAtFullCollection switch parameter, which is used to add a defragmentation process after Full GC. You can also use the -XX:CMSFullGCBeforeCompaction parameter to set how many times to perform uncompressed Full GC, followed by Do a defragmentation process.


##G1 Collector

The G1 (Garbage First) collector is a new collector provided by JDK1.7. The G1 collector is based on the "mark-complement" algorithm, which means that it will not generate memory fragmentation. Another feature is that the previous collectors collected the entire new generation or the old generation, while G1 collected the entire Java heap (including the new generation and the old generation).


Summary of garbage collector parameters

-XX:+

##-XX:-

-XX:

##-XX:
Dynamicly adjust the size of each area in the java heap and the age of entering the old generationWhether the -Parallel Scavenge collector-Parallel Scavenge collector-XX:CMSInitiatingOccupancyFraction##--XX:+CMSFullGCBeforeCompaction
Parameter Description
-XX:+

UseSerialGC

The default value for Jvm to run in Client mode. After turning on this switch, the collector combination of Serial + Serial Old is used for memory recycling
-XX:+UseParNewGC After turning on this switch, use the ParNew + Serial Old collector for garbage collection
-XX:+UseConcMarkSweepGC Use ParNew + CMS + Serial Old collector combination for memory recycling, Serial Old appears as CMS "Concurrent The fallback collector is used after Mode Failure" failure.
##-XX:+UseParallelGC The default value for Jvm to run in Server mode. After turning on this switch, use Parallel Scavenge + Serial Old collector combination for recycling
##-XX:+UseParallelOldGC Use Parallel Scavenge + Parallel Old collector combination for recycling
-XX:SurvivorRatio The capacity ratio of the Eden area to the Survivor area in the new generation, the default is 8, which means Eden:Subrvivor = 8:1
##-XX:PretenureSizeThreshold is directly promoted to the size of the old generation object. After setting this parameter, objects larger than this parameter will be directly Old generation allocation
-XX:MaxTenuringThreshold Promotes to the age of objects in the old generation, after each Minor GC , the age is increased by 1, and when it exceeds the value of this parameter, it enters the old generation
##-XX:UseAdaptiveSizePolicy
-XX :+HandlePromotionFailure new generation collection guarantee is allowed, after a minor gc is performed, when another block of Survivor space is insufficient, Will be retained directly in the old generation
#-XX:ParallelGCThreadsSet the number of threads for parallel GC for memory recycling
XX:GCTimeRatioThe ratio of GC time to the total time. The default value is 99, which means 1 is allowed. % of GC time, only valid when using the
XX:MaxGCPauseMillis Set the maximum pause time of GC, valid under
Set the CMS collector to start garbage collection after the old generation space is used. The default value is 68%, only in Valid when using CMS collector, -XX:CMSInitiatingOccupancyFraction=70
XX:+UseCMSCompactAtFullCollection
Since the CMS collector will generate fragments, this parameter sets whether a memory defragmentation process is required after the garbage collector. It is only valid when the CMS collector is used.

Set the CMS collector to perform a memory defragmentation process after several garbage collections. Typically used with the
UseCMSCompactAtFullCollection parameter
##-XX:+UseFastAccessorMethods
Primitive type optimization
-XX:+DisableExplicitGC
Whether to turn off manual System.gc
-XX:+CMSParallelRemarkEnabled
Reduce mark pause
-XX:LargePageSizeInBytes The size of the memory page cannot be set too large, which will affect the size of Perm, -XX:LargePageSizeInBytes=128m


Client and Server mode default GC




Server


Sun/oracle JDK GC combination method




##New generation GC method Old generation and persistentGeneration GC method
Client

Serial Serial GC Serial Old Serial GC
Parallel Scavenge Parallel recycling GC Parallel Old Parallel GC
##-XX:+UseConcMarkSweepGCWhen #-XX:+UseParNewGCParNew -XX:+Parallel Old Parallel GC uses Serial Old
##New generation GC method Old generation and persistenceGeneration GC method

-XX:+UseSerialGC

Serial Serial GC Serial Old Serial GC
-XX:+UseParallelGC Parallel Scavenge Parallel recycling GC Serial Old Parallel GC
ParNew Parallel GCCMS Concurrent GC "Concurrent Mode Failure" appears
Use Serial Old
Serial GC
Parallel GCSerial Old Serial GC
UseParallelOldGC Parallel Scavenge Parallel GC
#-XX:+UseConcMarkSweepGC-XX:+UseParNewGC

##Serial Serial GC
##CMS Concurrent GC When "Concurrent Mode Failure appears "When Serial GC

## The above is the content of Java virtual machine learning - garbage collector. For more related content, please pay attention to the PHP Chinese website (www.php.cn) !

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template