Java Virtual Machine garbage collector

Source: Internet
Author: User

Garbage collection algorithm for garbage collectorTag-Purge (mark-sweep) algorithm:

All objects that need to be reclaimed are marked first, and all tagged objects are collected uniformly after the tag is complete. It has two disadvantages: one is the efficiency problem, the mark and the clear process is not efficient, the other is the space problem, the marked clear will produce a large number of discontinuous memory fragments, space debris too much can cause, When the program needs to allocate large objects in the future, it cannot find enough contiguous memory space and has to start another garbage collection action in advance.

The execution process is as follows:

Replication algorithm:

It divides the available memory by capacity into two blocks of equal size, one at a time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once. In this way, each time it is a piece of memory recovery, no allocation when the memory fragmentation is not considered complex situations, as long as the mobile heap top pointer, in order to allocate memory, simple, efficient operation.

Today's commercial virtual machines are using this collection algorithm to recover the new generation, IBM's special research shows that the new generation of object 98% is going to die, so do not need to divide the memory space according to the ratio of 1:1, but the memory is divided into a larger Eden space and two smaller survivor space, Each time you use Eden and one of the survivor. When recycled, make a one-time copy of Eden and survivor to another survivor space, and finally clear out the space of Eden and the survivor you just used.

The execution process is as follows:

Tag-Collation algorithm:

The tagging process is still the same as the tag-clear algorithm, but the next step is not to clean up the recyclable objects directly, but rather to have all the surviving objects move toward one end and then directly clean out the unexpected memory at the end of the boundary.

The execution process is as follows:

Generational collection algorithm:

The current garbage collection of commercial virtual machines uses the generational collection (generational Collection) algorithm, which does not have any new ideas, but divides the memory into several blocks based on the different inventory cycles of the objects. In general, the Java heap is divided into the new generation and the old age, so that the characteristics of each era to adopt the most appropriate mobile phone algorithm. In the new generation, each garbage collection found that a large number of objects died, only a small number of survival, then the choice of replication algorithm, only a small number of surviving objects to pay the cost of replication can be completed collection. In the old age, because of the high survival rate of the object and the lack of additional space to guarantee it, it must be recycled using a tag-clean or tag-collation algorithm.

garbage collector

I drew a mind map to describe the garbage collector:

garbage collector Common parameters
Parameters Description
Useserialgc Use Serial + Serial old collector combination for memory reclamation
Useparnewgc Use Parnew + Serial old collector combination for memory reclamation
Useconcmarksweepgc Use Parnew + CMS + Serial old collector combination for memory reclamation. Serial old collector will appear as a CMS collector after concurrent Mode failure failed fallback collector use
Useparallelgc Memory reclamation using parallel scavenge + Serial old collector combination
Useparalleloldgc Memory recycling using the collector combination of parallel scavenge + Parallelold
-xx:survivorratio The ratio of the capacity of the Eden region to the Survivor region in the Cenozoic, which defaults to 8, represents eden:survivor=8:1
Pretenuresizethreshold Direct promotion to the old age object size, objects larger than this parameter will be allocated directly in the old age
Maxtenuringthreshold The age at which the old age was promoted. After each object has persisted once minor GC, the age increases 1, when exceeds this parameter to enter the old age
Useadaptivesizepolicy Dynamically adjusts the size of each area in the Java heap and the age of entry into the old age
Handlepromotionfailure Whether to allow the guarantee to fail, that is, the remaining space in the old age to cope with the new generation of the entire Eden and all the objects of the survivor area survive extreme situations
Parallelgcthreads Number of threads that are memory-collected when parallel GC is set
Gctimeratio The ratio of GC time to total time. Effective only when using the parallel scavenge collector
Maxgcpausemillis Sets the maximum pause time for the GC. Effective only when using the parallel scavenge collector
Cmsinitiatingoccupancyfraction Set the CMS collector to trigger garbage collection after the old age space is used. Effective only when using the CMS collector
Usecmscompactatfullcollection Sets whether the CMS collector will perform a memory defragmentation once the garbage collection is complete. Effective only when using the CMS collector
Cmsfullgcsbeforecompaction Set up the CMS collector to start a memory defragmentation after several garbage collection times

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.