GC Algorithm garbage collector

Source: Internet
Author: User

GC Algorithm Garbage Collection Device

Reference: http://www.cnblogs.com/ityouknow/p/5614961.html

Overview

Garbage collection Garbage Collection is often referred to as the "GC", which was born in the 1960 MIT Lisp language, and has matured for more than half a century.

In the JVM, program counters, virtual machine stacks, and local method stacks are all born with threads, and stack frames are stacked and out-of-stack as the method enters and exits, enabling automatic memory cleanup, so our memory garbage collection is mainly concentrated in the Java heap and the method area , during program run , the allocation and use of this part of memory is dynamic.

Object Survival Judgment

There are two ways to determine if an object is alive:

reference count : Each object has a reference count property, a new reference count plus 1, a reference release count minus 1, and a count of 0 can be recycled. This method is simple and does not solve the problem of circular referencing between objects .

Accessibility Analysis : Starting from the GC roots, the search is traversed by a path called a reachability chain. When an object is connected to a GC roots without any reference chain, it proves that the object is not available. Unreachable objects.

In the Java language,GC Roots includes:

The object referenced in the virtual machine stack.

The object referenced by the class static property entity in the method area.

The object referenced by the constant in the method area.

The object that is referenced by JNI in the local method stack.

garbage Collection Algorithm tag-purge algorithm

The " mark-Clear " (mark-sweep) algorithm, like its name, is divided into "mark" and "purge" two phases: first mark all objects that need to be recycled, and then uniformly reclaim all tagged objects after the tag is complete. The reason is that it is the most basic collection algorithm, because the subsequent collection algorithms are based on this idea and improve their shortcomings.

Its main shortcomings are two: one is the efficiency problem, the labeling and removal process is not efficient, and the other is a space problem, the mark after the purge will produce a large number of discontinuous memory fragmentation, too much space debris may cause, When the program needs to allocate large objects in the future, it cannot find enough contiguous memory and has to trigger another garbage collection action in advance.

Replication Algorithms

The Copying collection algorithm, which divides available memory by capacity into two blocks of equal size, using only one piece 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.

This makes each one of the pieces of memory recycling, memory allocation will not consider the complexity of memory fragmentation, as long as the mobile heap top pointer, in order to allocate memory, simple implementation, efficient operation. Only the cost of this algorithm is to reduce the memory to half the original, the continuous replication of long-lived objects leads to inefficient.

Mark- Compression algorithm

The replication collection algorithm performs more replication operations when the object has a higher survival rate and becomes less efficient. More crucially, if you do not want to waste 50% of space, you need to have additional space to allocate security, in order to deal with all the objects in the memory used in 100% survival extreme situation, so in the old age generally can not directly select this algorithm.

According to the characteristics of the old age, someone proposed another "mark-and-sweep" (mark-compact) algorithm, the marking process is still the same as the "tag-purge" algorithm, but the next step is not directly to the recyclable objects to clean up, but to let all the surviving objects moved to one end, Then directly clean out the memory outside the end boundary.

Generational Collection Algorithms

the basic assumptions of GC generational: Most objects have a very short life cycle and short survival times.

The "generational collection" (generational Collection) algorithm, which divides the Java heap into the Cenozoic and the old, allows the most appropriate collection algorithm to be used according to the characteristics of each age. In the Cenozoic, every garbage collection is found to have a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small number of surviving objects of the replication cost 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 the "mark-clean" or "mark-sweep" algorithm.

garbage collector

If the collection algorithm is the method of memory recycling, the garbage collector is the specific implementation of memory recovery

Serial collector

The serial collector is the oldest, most stable, and efficient collector, which can cause a long pause, using only one thread to recycle. The new generation, the elder generation uses the serial recovery, the new generation replication algorithm , the old age mark-compression ; The garbage collection process stops the world (service paused)

Parameter control:-XX:+USESERIALGC serial Collector

parnew Collector

The Parnew collector is actually a multithreaded version of the serial collector. Cenozoic parallel, old age serial; new generation replication algorithm, old generation marker-compression

Parameter control:-XX:+USEPARNEWGC parnew Collector

-xx:parallelgcthreads Limit the number of threads

Parallel Collector

The Parallel scavenge collector is similar to the Parnew collector, and the Parallel collector pays more attention to the throughput of the system. The Adaptive throttling strategy can be turned on by parameters, and the virtual opportunity collects performance monitoring information based on the current system's operation, dynamically adjusting these parameters to provide the most appropriate pause time or maximum throughput, or by using parameters to control the GC's time by no more than milliseconds or scale New generation replication algorithm, old generation marker-compression

Parameter control:-XX:+USEPARALLELGC using parallel collector + old age serial

Paralle L Old   Collecting Device

Parallel old is an older version of the Parallel scavenge collector, using multithreading and the "mark-and-organize" algorithm. This collector is only available in JDK 1.6

Parameter control: -XX:+USEPARALLELOLDGC using parallel collector + old age parallel

CMS Collecting Device

The CMS (Concurrent Mark Sweep) collector is a collector that targets the shortest recovery pause time. At present, a large part of the Java applications are concentrated in the Internet or B/s system services, such applications pay particular attention to the response of the service, the hope that the system will be the shortest time to pause, in order to bring a better user experience.

From the name (including "Mark Sweep") you can see that the CMS collector is implemented based on the "tag-purge" algorithm, its operation is more complex than the previous collectors, the entire process is divided into 4 steps, including:

Initial tag (CMS initial mark)

Concurrency token (CMS concurrent mark)

Re-tagging (CMS remark)

Concurrent Purge (CMS concurrent sweep)

Where the initial tag, re-tagging these two steps still need "Stop the World". The initial tag simply marks the object that the GC Roots can directly relate to, is fast, the concurrent tagging phase is the process of GC Roots tracing, and the re-tagging phase is to fix the concurrency tag period. The mark record of the part of the object that caused the tag to change as the user program continues to work, the period of time will generally be slightly longer than the initial marking phase, but much shorter than the time of the concurrent tag.
Because the collector thread can work with the user thread during the longest concurrent markup and concurrent cleanup process throughout the process, the memory reclamation process for the CMS collector is performed concurrently with the user thread, in general. old age collector (new generation using parnew)

Pros: Concurrent collection , low pauses

Cons: generating large amounts of space debris , concurrent phases can reduce throughput

Parameter control:-XX:+USECONCMARKSWEEPGC using CMS collector

-xx:+ usecmscompactatfullcollection full GC, a defragmentation is done, and the finishing process is exclusive, causing a pause time to grow

-xx:+cmsfullgcsbeforecompaction set up a few full GC and then defragment it once

-xx:parallelcmsthreads Set the number of threads for the CMS (typically approximately equal to the number of available CPUs)

G1 Collecting Device

G1 is one of the most cutting-edge results of current technology development, and the mission assigned to it by the Hotspot development team is to replace the CMS collectors released in JDK1.5 in the future. Compared to the CMS collector, the G1 collector has the following features:

1. Spatial Integration , the G1 collector uses a tagging algorithm that does not generate memory space fragmentation. Assigning large objects does not trigger the next GC ahead of time because no contiguous space is found.

2. Predictable pauses , which is another big advantage of G1, reduce the pause time is a common concern of G1 and CMS, but G1 in addition to the pursuit of low pauses, but also to establish a predictable pause time model, allowing the user to explicitly specify in a length of n milliseconds in a time fragment, The time spent on garbage collection must not exceed n milliseconds, which is almost a feature of the real-time Java (RTSJ) garbage collector.

The garbage collectors mentioned above are collected in the whole new generation or the old age, and G1 is no longer the case. The memory layout of the Java heap differs greatly from other collectors when using the G1 collector, which divides the entire Java heap into separate, equal-sized regions (region), although it retains the concept of the Cenozoic and the old, but the new generation and the old age are no longer physical barriers, They are all part of a set of (can not be contiguous) region.

G1 's Cenozoic collection is similar to that of Parnew, where the new generation takes up a certain percentage of time to start collecting. Similar to CMS, the G1 collector collects an old age object with a short pause.

Collection Steps :

1, marking phase , first initial mark (Initial-mark), this stage is paused (Stop the World Event), and will trigger a normal mintor GC. corresponding GC log:gc pause (Inital-mark)

2,Root region scanning, the program will be in the process of recycling survivor area (survive to the old age), this process must be completed before young GC.

3,Concurrent marking, in the entire heap for concurrent tagging (and application concurrent execution), this process may be interrupted by young GC. In the concurrency tagging phase, if all objects in a zone object are found to be garbage, the area is immediately recycled (x in the figure). At the same time, the object activity of each region (the proportion of the surviving objects in the region) is calculated during the concurrent tagging process.

4,Remark, re-mark, there will be a short pause (STW). The re-tagging phase is used to collect the concurrency tag phase to generate new garbage (concurrent phases and applications run together), and the G1 uses a faster initial snapshot algorithm than the CMS: Snapshot-at-the-beginning (SATB).

5,Copy/cleanup, multi-threaded removal of inactivated objects, there will be STW. G1 copies the surviving objects of the reclaimed area to the new zone, clears the remember sets, and empties the Reclaim area and returns it to the list of idle areas.

6. After the copy/purge process. The active object of the reclaimed area has been concentrated back into the deep blue and dark green areas.

used in Collecting Device Combination

1, Serial
2, Parnew + CMS
3, Parallelyoung + parallelold
4, G1GC

Reference

http://my.oschina.net/hosee/blog/644618

Deep understanding of Java Virtual machines: JVM advanced features and best practices PDF

: http://download.csdn.net/detail/ityouknow/9557109

GC Algorithm 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.