In-depth understanding of Java Virtual Machine learning Note 4--java virtual machine garbage collector

Source: Internet
Author: User

Java heap memory is divided into two parts of the new generation and the old generation, the new generation mainly uses the replication and labeling-clearing garbage collection algorithm, the old generation mainly use tag-sorting garbage collection algorithm, so in Java virtual for the new generation and the old generation of different garbage collectors, JDK1.6 in sun The garbage collector for the hotspot virtual machine is as follows:

If the two garbage collectors have direct connections, these two garbage collectors can be used with each other.

(1). Serial garbage collector:

Serial is the most basic and oldest garbage collector, using a replication algorithm that was once the only new generation of garbage collectors before JDK1.3.1.

Serial is a single-threaded collector that does not only use one CPU or one thread to complete garbage collection, but also suspends all other worker threads while garbage collection is done, until the end of garbage collection.

Although the serial garbage collector needs to suspend all other worker threads during the garbage collection process, it is simple and efficient, and without the overhead of thread interaction for a single CPU environment, the highest single-thread garbage collection efficiency can be achieved. Therefore, the serial garbage collector is still the default new generation garbage collector running in client mode for Java virtual machines.

(2). Parnew garbage Collector:

The Parnew garbage collector is actually a multithreaded version of the serial collector, and also uses a copy algorithm that, in addition to using multithreading for garbage collection, behaves like the serial collector, and the parnew garbage collector also pauses all other worker threads during the garbage collection process.

The Parnew collector defaults on the number of threads with the same number of CPUs, and the-xx:parallelgcthreads parameter can be used to limit the number of threads in the garbage collector.

Although Parnew is almost exactly the same as the serial collector except for multithreading, the Parnew garbage collector is the default garbage collector for many Java virtual machines running in the new generation of server mode.

(3). Parallel Scavenge Collector:

The Parallel scavenge collector is also a new generation garbage collector, also using the replication algorithm, also a multi-threaded garbage collector, it focuses on the program to achieve a controllable throughput (Thoughput,cpu used to run user code time/CPU total elapsed time, That is, throughput = Run user code time/(run user code time + garbage Collection Time)), high throughput can be the most efficient use of CPU time, as soon as possible to complete the operation of the program tasks, mainly for in the background operation and do not need too much interaction tasks.

The Parallel scavenge collector provides two parameters for precise control of throughput:

A.-xx:maxgcpausemillis: Controls the maximum garbage collection pause time, which is a number of milliseconds greater than 0.

B.-xx:gctimeration: Directly sets the throughput size, which is an integer greater than 0 and less than 100, which is the ratio of the program run time to the total time, and the default value is 99, which is garbage collection time of up to 1% (1/(1+99)) of garbage collection runs.

The Parallel scavenge is a throughput-first garbage collector, which also provides a parameter:-xx:+useadaptivesizepolicy, which is a switch parameter that is not required to manually specify the Cenozoic size (-xmn) after opening. Details such as the ratio of Eden to Survivor (-xx:survivorration) and the new generation to age (-xx:pretenuresizethreshold), virtual opportunity collects performance monitoring information based on current system operation. Adjusting these parameters dynamically to achieve maximum throughput, which is called GC Adaptive Throttling Strategy, is also an important difference between the Parallelscavenge collector and the Parnew collector.

(4). Serial Old collector:

Serial old is an older version of the Serial garbage collector, which is also a single-threaded collector, using the tag-collation algorithm, which is also primarily run on the default old generation garbage collector of the client's default Java virtual machine.

In server mode, there are two main uses:

A. Used in conjunction with the new generation of parallel scavenge collectors in previous versions of JDK1.5.

B. As a backup garbage collection scheme using the CMS collector in older generations.

The new generation serial and the old generation of serial with garbage collection process diagram:

The Cenozoic parallel scavenge collector works similarly to the Parnew collector, which is a multi-threaded collector that uses a replication algorithm that pauses all worker threads during the garbage collection process.

The new generation parallel scavenge/parnew and the old generation of serial and the garbage collection process diagram:

(5). Parallel Old collector:

The Parallel old collector is an older version of the Parallel scavenge, which is only available at JDK1.6, using a multi-threaded tagging-finishing algorithm.

Before JDK1.6, the Cenozoic use of the Parallelscavenge collector can only be paired with an older generation of serial old collectors, only to ensure that the new generation of throughput priority, unable to guarantee the overall throughput, Parallel Old is to provide the same throughput-first garbage collector in the older generation, and if the system has higher throughput requirements, the collocation strategy of the Cenozoic parallel scavenge and the older generation parallel old collectors can be given priority.

New generation parallel scavenge and old generation parallel older collectors with running process diagram:

(6). CMS Collector:

The Concurrent Mark Sweep (CMS) collector is an old generation garbage collector whose primary goal is to get the shortest garbage collection pause time, unlike other older generations using the tagging-grooming algorithm, which uses multi-threaded markup-cleanup algorithms.

The shortest garbage collection pause time can improve the user experience for highly interactive programs, and the CMS collector is the first truly concurrent garbage collector in the Sun Hotspot virtual machine, which for the first time enables the garbage collection thread and the user thread to work simultaneously.

CMS working mechanism is more complex than other garbage collectors, the whole process is divided into the following 4 stages:

A. Initial tag: Just mark the object that GC roots can directly relate to, and it will still need to pause all worker threads.

B. Concurrency token: The process of GC roots tracing, working with the user thread, without pausing the worker thread.

C. Re-tagging: In order to fix the tag record of the part of the object that caused the tag to change while the user program continues to run during concurrent tagging, all worker threads still need to be paused.

D. Concurrent purge: Clears the GC roots object, works with the user thread, and does not need to pause the worker thread.

Because the garbage collection thread can work concurrently with the user now, because of the longest time-consuming concurrency token and the concurrent cleanup process, the memory reclamation of the CMS collector and the user thread are performed concurrently in a holistic manner.

CMS Collector Working Process:

The CMS collector has the following three deficiencies:

The A.cms collector is very sensitive to CPU resources, the number of collection threads that are started by default = (number of CPUs +3)/4, and if the user program is already high CPU load, if you want to separate the CPU resources to run the garbage collector thread, it will make the CPU load heavier.

B.CMS cannot handle floating garbage (floating garbage), which may cause concurrent modefailure to fail and cause another full GC. Because the CMS collector and user thread run concurrently, so in the collection process, there are constantly new garbage generation, the garbage appears after the tagging process, the CMS can not be processed in this collection, and had to wait for the next GC to clean it up, the garbage is called floating garbage.

The CMS garbage collector cannot wait for the old generation to be completely filled up, as other garbage collectors do, and to reserve a portion of the space for concurrent collection use, which can be-xx by parameter: Cmsinitiatingoccupancyfraction to set the percentage of old age generation space to trigger a CMS for garbage collection, default is 68%.

If the reserved memory does not meet the needs of the program during the CMS run, a concurrentmode failure failure occurs, at which point the virtual machine initiates a preliminary scheme and uses the serial old collector to re-recycle the older generation.

The C.cms collector is based on the tag-purge algorithm and therefore inevitably generates a large number of discontinuous memory fragments that will trigger the full GC if it cannot find a chunk of contiguous memory that is large enough to hold the object. The CMS provides a switch parameter-xx:+usecmscompactatfullcollection, which specifies that memory is collated after the full GC, memory grooming can cause garbage collection to stall longer, and the CMS provides another parameter-xx: Cmsfullgcsbeforecompaction, which is used to set the number of uncompressed full GC after execution, followed by a memory collation.

(7). G1 Collector:

Garbage first garbage collector is the most cutting-edge result of the current garbage collector theory, compared with the CMS collector, the G1 collector two most prominent improvements are:

A. Based on the tag-collation algorithm, no memory fragmentation is generated.

B. The pause time can be controlled very precisely, and low-pause garbage collection is achieved without sacrificing throughput.

The G1 collector avoids full-area garbage collection, which divides the heap memory into separate, fixed-size areas and tracks the garbage collection progress of those areas while maintaining a priority list in the background, prioritizing the most garbage-collected areas each time, based on the allowable collection time.

Zoning and priority zone recovery mechanisms ensure that the G1 collector can achieve the highest garbage collection efficiency for a limited time.

Java Virtual Machine Common garbage collector related parameters are as follows:

Parameters

Describe

Useserialgc

The virtual machine runs the default value in client mode, and when this switch parameter is turned on,
Use the serial+serial old collector combination for garbage collection.

Useparnewgc

When this switch parameter is turned on, use the parnew+serial old collector to combine the
Garbage collection.

Useconcmarksweepgc

After you turn on this switch parameter, use the parnew+cms+serial old collector Group
Collection of garbage. Serial old as a CMS collector appears concurrent
Alternate garbage collector for Mode failure.

Useparallelgc

The virtual machine runs the default value in Server mode, and when this switch parameter is turned on,
Use the parallel scavenge+serial old collector combination for garbage collection.

Useparalleloldgc

When this switch parameter is turned on,
Use the Parallel scavenge+parallel old collector combination for garbage collection.

Survivorration

In the Cenozoic memory, Eden Region and survivor region capacity ratio, default is 8, that is
Eden:survivor=8:1.

Pretenuresizethreshold

Directly promoted to the age of the object size, after setting this parameter, the size of the
The object allocates memory directly in the old generation.

Maxtenuringthreshold

Direct promotion to the age of older generations, each object after a minor GC
Survive, the age increases by 1, and when the age exceeds that value, it enters the elderly generation.

Useadaptivesizepolicy

Java Virtual machine Dynamic Adaptive strategy, dynamically adjust the age of older generations of objects and the size of each region.

Handlepromotionfailure

Whether to allow the guarantee to allocate memory failure, that is, the whole old generation of space is not enough, and the entire Cenozoic in the Eden and Survivor objects are the extreme conditions of survival.

Parallelgcthreads

The number of threads that are memory-reclaimed when parallel GC is set.

Gctimeration

Parallel Scavenge collector run time as a percentage of the total time.

Maxgcpausemillis

Parallel Scavenge collector maximum GC pause time.

Cmsinitiatingoccupancyfraction

Sets the percentage of the CMS collector to trigger garbage collection after the amount of space used in the old generation, which defaults to 68%.

Usecmscompactatfullcollection

Sets whether the CMS collector will perform a memory collation once the garbage collection is complete.

Cmsfullgcsbeforecompaction

Sets the number of garbage collections that the CMS collector does after a memory collation.

The-xx:+printgcdetails parameter of the Java Virtual machine can print the log information of the garbage collector.

-VERBOSE:GC can view Java Virtual machine garbage collection results.

(EXT) "In-depth understanding of Java Virtual Machine" Learning note 4--java virtual machine garbage collector

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.