The principle of garbage collection mechanism of Java Virtual machine

Source: Internet
Author: User

1. Common algorithms:

A. Reference counting method: Configure an shaping counter for each object, when there is a reference, counter +1, when the reference fails, counter-1. Counter 0 for garbage collection

Problem: A object reference b,b object reference A. Circular reference, cannot be purged, causes memory leak

The Java garbage collector does not use the algorithm

B. Tag-purge algorithm

The tagging phase and the purge phase.

There is a problem: the freed space is not contiguous. Memory allocation discontinuous space, low productivity

C. Replication algorithms

Divide the memory space into two pieces, use only one piece at a time, garbage collection, copy the surviving objects in the memory that is being used into unused memory, and then clear all objects that use memory.

Analysis: You can guarantee that the recovered memory space is not fragmented. When there are a large number of garbage objects in memory, the replication algorithm is very efficient, but there are a large number of surviving objects, inefficient. Suitable for young generation

D. Tag-compression algorithm

Optimization of the mark-sweep algorithm, when cleared, compresses the surviving object to one end of the memory, clears all space outside the boundary, and avoids fragmentation

E. Incremental algorithm

During garbage collection, other threads hang, and the idea of an incremental algorithm is to reclaim a small area of memory each time, alternating with the application,

Analysis: Application One-time downtime, but switching waste time, the number of pauses increased. System Throughput down

F. Sub-generational

Divided into the young generation memory space, the old memory, the younger generation uses the copy algorithm, the old age uses the tag-compression algorithm

Garbage collector classification in the JVM

1. Cenozoic Serial Collector: single-threaded, exclusive. Use a replication algorithm. The Cenozoic is divided into Eden space, from space, to space 3 parts. The From space and to space can be considered as two blocks of the same size, equal status, and can be used for character interchange. The From and to spaces are also known as Survivor Spaces, which are survivor spaces that are used to store objects that have not been reclaimed.

2. The old age serial collector: single-threaded, exclusive. Using the tag compression algorithm

3. Parallel collector: Work in the new generation of garbage collection multi-threading, exclusive. You can specify the number of threads

4. Cenozoic Parallel Collector: Differences from the new generation of parallel collectors: focus on system throughput to set maximum pause time and throughput size

5. Old age Parallel Collection collector: multithreading. Use the tag-compression algorithm.

6.CMS Collector: Tag-purge algorithm. It is not exclusive. You can run programs during garbage collection. Focus on system downtime. You can set the timing of memory grooming

7.G1 Collector: As a garbage collector for a server, it is expected to outperform the CMS collector for throughput and pause control. Use the tag-compression algorithm. Can control the pause time

Summarize:

      

GC-Related Parameters summary

1. Parameters related to the serial collector

-XX:+USESERIALGC: Use the serial collector in the new generation and the old age.

-xx:+suivivorratio: Sets the scale of the Eden area size and the size of the survivor area.

-xx:+pretenuresizethreshold: Sets the threshold for large objects to enter the old age directly. When the size of the object exceeds this value, it is allocated directly in the old age.

-xx:maxtenuringthreshold: Sets the maximum age for an object to enter the old years. Each time Minor GC, the object age is added 1. Any object greater than this age is bound to enter the old age.

2. Parameters related to parallel GC

-XX:+USEPARNEWGC: Using a parallel collector in the Cenozoic.

-XX:+USEPARALLELOLDGC: The old age uses the parallel collection collector.

-xx:parallelgcthreads: Sets the number of threads to use for garbage collection. Typically, the number of CPUs can be equal. However, it is reasonable to set a relatively small number in the case of more CPU.

-xx:maxgcpausemills: Sets the maximum garbage collection pause time. Its value is an integer greater than 0. When the collector is working, it adjusts the Java heap size or some other parameters, as much as possible to control the pause time within Maxgcpausemills.

-xx:gctimeratio: Sets the throughput size, whose value is an integer between 0-100. Assuming that the value of Gctimeratio is N, the system will spend no more than 1/(1+n) for garbage collection.

-xx:+useadaptivesizepolicy: Turn on adaptive GC policy. In this mode, the size of the Cenozoic, the proportions of Eden and survivor, and the age of the older age are automatically adjusted to achieve a balance between heap size, throughput, and pause time.

3. Parameters related to the CMS collector

-XX:+USECONCMARKSWEEPGC: The new generation uses the parallel collector, the older generation uses the cms+ serial collector.

-xx:+parallelcmsthreads: Sets the number of threads for the CMS.

-xx:+cmsinitiatingoccupancyfraction: Set the CMS collector to trigger after the old age space is used, default is 68%.

-xx:+usefullgcsbeforecompaction: Sets the number of times the CMS garbage collection is performed, and then the memory is compressed once.

-xx:+cmsclassunloadingenabled: Allows the collection of class metadata.

-xx:+cmsparallelremarkendable: Enables parallel re-tagging.

-xx:cmsinitatingpermoccupancyfraction: When the occupancy of the zone reaches this percentage, start the CMS recycle (provided the-xx:+cmsclassunloadingenabled is activated).

-xx:usecmsinitatingoccupancyonly: Indicates that the CMS is recycled only when the threshold is reached.

-xx:+cmsincrementalmode: Use incremental mode, which is more suitable for single CPU.

4. Parameters related to the G1 collector

-XX:+USEG1GC: Use the G1 collector.

-xx:+unlockexperimentalvmoptions: Allows the use of experimental parameters.

-xx:+maxgcpausemills: Sets the maximum garbage collection pause time.

-xx:+gcpauseintervalmills: Sets the time between pauses.

5. Other parameters

-XX:+DISABLEEXPLICITGC: Disables the display of GC.

Optimization is based on the design of your own program. No fixed optimization scheme

The principle of garbage collection mechanism of Java Virtual machine

Related Article

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.