Garbage collection algorithm and Garbage Collector
1. Live time determines the collection algorithm of the New and Old generations.
The new generation uses copy collect and the old generation uses markseep or markcompact.
In the new generation, objects are much less active, and the copy policy is adopted. There are few copy objects and the target space is small. The cost of copy is small.
Because the old generation has a long lifetime, the mark sweep/mark compatc policy is adopted. If the copy policy is used, because there are many objects to copy, the target space is required, and the copy cost is high.
2.1 copy collect:
Theoretically, the new generation space of the copy algorithm is divided by. Half of the space is used to store new objects, and the other half is used to prepare and receive objects. In this way, the space utilization is only 50%, that is, ensure that the other half of the space is cleared at all times, so as to receive objects from copy in the target space. Practice has shown that 98% of New Generation objects are born and died overnight, so division is not required.
2.1.1 Implementation of the copy Algorithm in hotspot
Modern Commercial JVM does not need to divide the new generation by, but divides the new generation space into a large Eden and two smaller zones or regions (from space and to space ), in the hotspot, the default ratio is, so that each new generation of available memory space accounts for 80% + 10% of the total space, and the remaining 10% is used as the to space, that is, the target receiving space. Note that the roles of from space and to space are exchanged at the end of each minor GC. To space features: awlays free before the live object is copied from space.
In the available space, the purpose of separating Eden is to only store the created live object with Eden. When the Eden space cannot accommodate the new object, the minor GC may be triggered. From space is used to store live objects that are not eligible to enter the old generation.
During replication, the active objects in the Eden space and a shard or space are not qualified (also known as from space, less than-XX: maxtenuringthreshold (31 by default) times minor GC) copy the active object of old generation to another vertex or space (also called to space.
For objects in the from space that have undergone-XX: maxtenuringthreshold lifetime, they are copied to the old generation, and large objects are also copied directly to the old generation. For example, if the space is full, the object is directly copied to the old generation (this is worth noting that in actual products, we should try to avoid objects directly going to the old generation). You can use-XX: adjust vorratio to adjust the size occupied by buckets or spaces, and then clear Eden space and from space
The cleared from space will be used as to space in the next minor GC. After GC is complete, to space will be used as from space.
2.1.2 process description:
A new instance is first placed in Eden. When the space of Eden cannot be stored, if minor GC is triggered, the live object will be copied to S0, And Eden will be cleared.
After GC, the status is S0, Eden, and S1. At the end of GC, two S0 and S1 switches roles.
... Eden put the new object again until the next minor GC
In S0, the object state is old and dead:
There are two directions for "vomit old", one is to meet n rounds (from young Gen to Old Gen needs to mature) Recycling, can be placed in Old Gen, one direction is that N rounds are not enough and mature enough. You need to stay in the same vor region, but you need to move it to S1.
In S0, there is also a possibility that the object will not be referenced and become a dead object. These dead objects will be cleared after S0's live objects are moved to S1.
Objects in S1 have three states: Old and New
Indicates the live object that is included in Eden.
The old one indicates that the "immature" Object Moved by S0.
Dead indicates that the object is not referenced.
The status after GC is Eden and S0 cleared, and S1 is not empty. At the end of GC, two S0 and S1 switches roles.
... Eden put the new object again until the next minor GC
S0: Old and old
S1: Old
... Eden put the new object again until the next minor GC
........
2.1.3 I:
2:
3:
2.1.4 triggering of minor GC:
The object completes memory allocation in Eden Space
When the Eden space is full and the object is created, the minor GC is triggered because the requested space is not available, and the new (EDEN + S0 or Eden S1) generation is performed for garbage collection.
Minor GC,Objects that cannot be recycled in Eden space are put into an empty movie VOR (S0 or S1, Eden will certainly be cleared), Another object that cannot be recycled by GC in the same vor will also be placed in this same vor,Always ensure that one VOR is empty
In step 3, if the primary vor area is full, these objects are copied to the old area, or the primary vor area is not full, but some objects are old enough and put into the old space.
Full GC is performed when the old space is full.
3. the specific implementation of the garbage collection algorithm is the garbage collector provided by Sun. The new generation is serial, parnew, parallel scanage. The old generation includes serial old, parallel old, and CMS.
5. Collector Overview
Don't underestimate serial. In the jvm gc of the client, the CPU core of <= 2 is a reasonable choice.
The parnew collector is a multi-threaded version of the serial collector. Its other features are exactly the same as that of the serial collector. For example, STW. Parallel scanage looks like parnew, but it is a collector focusing on high throughput and long latency. (So it can be understood as a special collector of parllal. In many figures, it will be tied together with parnew, which will be confusing) it can be determined that all the collection algorithms used by the new generation are copy collect.
STW is like "when Mom is cleaning the room, you cannot throw scraps on the floor ." STW makes sense for garbage collection. "Therefore, the new generation of garbage collectors are all designed according to STW.
But for the old generation, the emergence of CMS broke the seemingly reasonable rule of garbage collection, STW. CMS allows "Mom is cleaning the room while you can throw paper scraps. ", The purpose of CMS is because online applications require GC to maintain a low throughput so that the application can respond quickly, that is, the application can be executed for a longer time.
Because CMS was born in jdk1.5 and is specially designed for offline tasks with high GC throughput, the parallel scanage recycler with slow application response was born in jdk1.4. Therefore, CMS can only be used with the new generation of serial and parnew, but cannot be used with parallel scanage.
Parallel old appeared in jdk1.6. It is used together with the new generation of parallel scanage. In jdk1.5, if parallel scanage is used in the new generation, the old generation has only two types of collectors, serial old and CMS, and CMS cannot be used with parallel scanage because of exceptions, in this way, parallel scanage can only work with serial old. Because the old generation garbage collection algorithm is serial, it affects the overall effect of parallel scanage in the new generation.
6. The company usually configures parnew + CMS online. The configuration of some offline servers running scheduled tasks is parallel scanage + parallel old.
CMS Overview
The purpose of the CMS collector design is that the application server allocates a short period of time to GC for a lower latency or faster user response.
Note that CMS does not completely suspend the application. In the CMS step, STW is required for two steps: initial mark and remark (). Other steps can be executed concurrently with the application. Initial mark is executed by a GC thread, and its pause time is relatively short. The pause time of the remark process is longer than that of the initial mark, and it is usually executed by multiple threads.
Next, compare the parallel mark compact with CMS:
Full GC
After talking about the history of the garbage collector, the next step is object allocation. The concept of full GC is introduced in the object allocation process:
Several scenarios: assuming that the concurrent collection of oldgen uses the CMS collector, the threshold for concurrent collection startup is set to the default 68%, and the CMS's own dynamic computing collection time function is disabled, CMS is started only when the value of threshold is reached (that is,-XX: + usecmsinitiatingoccupancyonly is added to the JVM startup parameter)
1. When CMS is used for concurrent collection, the size of the newly created object is greater than the size of Eden + region VOR (so you need to upgrade to oldgen directly) and 32% of the space reserved by oldgen, the CMS collection fails, so a full GC is triggered.
2. CMS does not collect data concurrently, but the size of the newly created object is greater than the size of the Eden + region vor region and greater than the total size of the remaining oldgen space, which triggers full GC.
3. During minor GC, younggen found that the total size of the objects currently alive in younggen and old enough is greater than the total size of the remaining oldgen space, thus triggering full GC.
4. -XMS and xmx are used to set the maximum memory usage of JVM (excluding perm generation). If the value is different, once the value exceeds-XMS, the system will perform full GC, the setting of perm generation is the same as that of maxpermsize.
Scenario 1 also shows that it is confusing to think that CMS is full GC. It is a big mistake. The mechanism that triggers CMS in scenario 1 is to reach the threshold of 68%. Full GC is often the last JVM used for self-help. The full GC feature is that the entire heap is scanned from the beginning to the end. If the heap size specified by-XMS and xmx is large, it can be imagined how slow the full GC will be, in addition to being slow, full GC also features STW.
Throughput and latencyAfter understanding it, GC has taken a big step.
The first arrow line in indicates high throughput, while the red arrow indicates high GC throughput. GC takes a long time. Generally, serial is used in this mode. The second arrow line indicates low throughput, and the red arrow indicates short GC time. The long and green arrows indicate that the application has been executed for a long time. CMS is in this mode.
Other GC knowledge points:
1. Why avoid full GC?
Because full GC scans the heap allocated by the entire JVM. Scanning will take a long time. Full GC may also cause STW, resulting in application unavailability.
2. cms c indicates concurrency. What is the difference between cms c and parallel?
First, the two objects are different. During the execution of the Application Thread, there is always a garbage collection thread.
Compared with a single collection thread of serial in the STW Stage, parallel runs multiple garbage collection threads in multiple cores in the STW Stage. (This explains why a single-core server can only use the serial reclaim policy .) 3. Compared to 3.1serial and parallel, both of them share the same STW, but the time of parallel's STW becomes shorter and the recovery efficiency is higher. 3.2 STW is the opposite of CMS. CMS greatly reduces the STW time.
3.3 full indicates that GC is performed only when it is full, while increment GC indicates that GC is performed when it reaches a certain capacity. 4. Why STW? Because GC conflicts with application content allocation. 5. Java objects are not only referenced or not referenced. There are four types of Java references. Google colleciton's mapmaker uses the weak reference. (To be supplemented)
6. It is difficult for JVM to understand multiple terms, such as minor GC, Major GC, and fullgc.
There is the minor collection which is just the Eden or the Eden and other vor spaces. A full collection which is everything, and a concurrent collection which is only the old generation.There is no major collection as such. The term full collection is more often used and clearer as to its meaning.
Therefore, full GC is the major GC, but full GC is clearer than major GC. Major GC is a term relative to minor GC.
6.1 minor GC to full GC (http://stackoverflow.com/questions/13683866/garbage-collection-issue-in-young-generation/13764935#13764935)
"If it is not possible to do/complete a minor collection, then a major/full collection is saved med. this is typically done using a mark-sweep-compact algorithm rather than copying algorithm... which is one reason why full collection is expensive. but ultimately (if you keep filling the heap) a full collection will not be able to reclaim enough space to continue and an Oome will be thrown. (or if you are using-XX:+UseGCOverheadLimit
, The Oome will be thrown when the percentage time spent in GC exceeds a designated threshold.)"
"Young generation has 3 segments Eden space, between vor1 and between vor2. these are just logical divisions of young generation. so objects get copied from Eden space to fetch vor1 and then to fetch vor2.so minor collection in general means that collection happens in young generation. and if young generation is full then object gets copied to old generation. again, collection is minor or major depends on multiple factors one of them is space availability in young generation. so if there is enough space in young generation for object allocation then it will be minor collection. but if there is not enough free space in YG then the same collection can turn into major."
Reference: http://blog.ragozin.info/2011/09/hotspot-jvm-garbage-collection-options.htmlhttp://codegotbugs.blogspot.com/2012/06/java-garbage-collection.html