Java GC Related knowledge

Source: Internet
Author: User
Tags compact

Classification of Java Heap
    • Divided into two categories: Younggen and Oldgen. Among them, Younggen is divided into three parts: Eden,from survivor and to survivor, the proportional default is: 8:1:1
    • PermGen does not belong to the Java heap category

      It is important to note that, starting with Java8, PermGen has been canceled and replaced by Metaspace, with the difference: PermGen contains class Metadata,class static variable and interned string, But Metaspace has only class metadata, and two other (class static variable and interned string) are moved from Java8 to the Java heap.
Classification of GC

For the implementation of the Hotspot VM, the GC in it is actually classified in exactly two ways:

    • Partial GC: The pattern of the entire GC heap is not collected and is currently divided into the following three main categories:

Young GC: Collect only Young Gen's GC
Old GC: Collects only the GC of the old Gen. The concurrent collection of CMS is this pattern
Mixed GC: Collects GC for entire young Gen and part of Old Gen. Only G1 has this pattern.

    • Full GC: Collects the entire heap, including the pattern of all parts of young Gen, Old Gen, Perm Gen (if present). A young GC is performed by default before full GC, and can be configured with parameter-XX:+SCAVENGEBEFOREFULLGC

    • Various GC algorithms and how to use them

Young Old
collectorcollector JVM Option
Serial (defnew) Serial Mark-sweep-compact -xx:+useserialgc
Parallel Scavenge (Psyounggen) Serial Mark-sweep-compact (Psoldgen) -xx:+useparallelgc
Parallel Scavenge (Psyounggen) Parallel Mark-sweep-compact (Paroldgen) -xx:+useparalleloldgc
Serial (defnew) Concurrent Mark Sweep -xx:+useconcmarksweepgc
-xx:-useparnewgc
Parallel (parnew) Concurrent Mark Sweep -XX:+USECONCMARKSWEEPGC
-xx:+useparnewgc
The G1 algorithm is suitable for younger generations and older ages, using-XX:+USEG1GC to enable
The GC trigger condition 1. Young GC Trigger conditions
    • The Eden area is full, and a young GC is carried out to put the surviving objects from Eden and from in to.
2. Full GC Trigger conditions
    • When calling System.GC, the system recommends performing a full GC, but does not necessarily perform
    • Oldgen the largest "continuous space" is insufficient, when the new generation of objects into the old age, if there are large objects or large arrays, then there will be a lack of space in the old age;
    • PermGen Lack of space
    • CMS GC appears promotion failed and concurrent mode failure
      Promotion failed is in the minor GC, Survivor space, the object can only be placed in the old generation, and the old generation "maximum continuous space" is not enough to cause; Concurrent mode failure is executing the CMS In the process of GC, there are objects to be put into the old generation, while the old generation space is insufficient.
    • The average size of the minor GC promoted to the old generation is greater than the remaining space of the old generation
    • For sun JDK applications that use RMI for RPC or management, the full GC is executed one hour by default. You can set the time interval for full GC execution by using-java-dsun.rmi.dgc.client.gcinterval=3600000 at startup or by-xx:+ DISABLEEXPLICITGC to disallow RMI calls to System.GC.
Introduction to CMS GC and G1 GC1. CMS GC Collection process (CMS GC is a GC algorithm for the old age)
  • CMS Initial Mark (stop-the-world): Collection phase, beginning to collect all GC roots and direct references to the object "This phase will be counted into the full GC";
  • Cms-concurrent-mark: This phase traverses the entire old age and marks all surviving objects, starting with the GC roots found in the Initialize tag phase. Concurrency tagging is characterized by running concurrently with the application thread. Not all surviving objects in the old age will be tagged, because the application will change the references of some objects as well as the markup.
  • Cms-concurrent-preclean: This phase is also a concurrency phase, and the application thread runs in parallel without interrupting them. When the previous phase runs in parallel, references to some objects have changed, and when these references change, the JVM marks the area of the heap as the Dirty Card (which contains the tagged but changed object, which is considered "dirty"), which is the Card marking.
  • Cms-concurrent-abortable-preclean: Another concurrency phase does not stop the application thread. This phase is trying to take on enough work in the final remark stage of STW. The duration of this phase depends on a number of factors, as this stage is repeated to do the same thing until one of the aboart conditions (such as the number of repetitions, the amount of work, the duration, etc.) is stopped.
  • CMS final Remark (Stop-the-world): This phase is the second in the CMS and is the last STW stage. The task of this phase is to complete all surviving objects that mark the entire old age. Because the pre-processing is concurrent, it may not keep up with the speed at which the application changes, and at this point, STW is very much needed to complete this rigorous test phase. "This phase will be counted into full GC";
usually the CMS runs as far as possible when the final remark stage is clean enough in the young generation, the aim is to eliminate the subsequent successive stages of several STW.
    • Cms-concurrent-sweep: does not require STW to be performed concurrently with the application thread. The goal of this phase is to remove the unused objects, reclaim the space they occupy and use for the future.
    • Cms-concurrent-reset: This phase executes concurrently, re-sets the data structure within the CMS algorithm, and prepares the next CMS life cycle for use.
2. G1 GC (The new generation and the old era Universal collection algorithm, does not need other collection algorithm coordination)
    • "Follow-up will be supplemented"
experience and rules for tuning some JVM parameters
    1. Young Generation Size Selection
  • Response Time-First application: as large as possible until the minimum response time limit of the system is approached (depending on the actual situation). In such cases, the frequency of collection of young generations is also minimal. At the same time, reduce the reach of older generations of the object.
  • Throughput-First application: as large as possible, can reach Gbit. Because there is no response time required, garbage collection can be done in parallel, generally suitable for more than 8CPU applications.
  • Avoid setting too small. When the new generation is set to hours, it causes: 1.YGC times more frequently 2. The YGC object may be brought directly into the old generation, and if the old generation is full at this time, the FGC will be triggered.
    1. Elder Generation Size Selection
  • Response Time-First application: Older generations use concurrent collectors, so their size needs to be set carefully, and some parameters, such as concurrency session rate and session duration, are generally considered. If the heap setting is small, it can result in memory fragmentation, high recovery frequency, and application pauses using the traditional markup cleanup method; If the heap is large, It takes a long time to collect. The most optimized scenario typically requires reference to the following data acquisition: Concurrent garbage collection information, persistent generation of concurrent collection times, traditional GC information, and the time scale spent on recycling in young and old generations.
  • Throughput-First applications: General throughput priority applications have a very large young generation and a smaller old generation. The reason is that it is possible to recycle most of the short-term objects, reduce the medium-term objects, and the old generation to store long-term survivors.
  • Fragmentation problems caused by smaller heaps because the old generation of the concurrent collector uses tags to clear the algorithm, the heap is not compressed. When the collector recycles, he merges the adjacent space so that it can be allocated to larger objects. However, when the heap space is small, a period of time after the run, there will be "fragmentation", If the concurrent collector does not find enough space, the concurrent collector will stop and then be reclaimed using the traditional markup and cleanup method. If "Fragmentation" occurs, the following configuration may be required:-xx:+usecmscompactatfullcollection: When using the concurrency collector , turn on the compression of older generations. -xx:cmsfullgcsbeforecompaction=0: When the above configuration is on, how many times the full GC is set, the old generation is compressed
  • The advantage of using CMS is to use as few new generation as possible, the experience value is 128m-256m, then the Laosheng generation uses the CMS to collect in parallel, which can guarantee the system low latency throughput efficiency. In fact, the CMS collection pause time is very short, 2G of memory, about 20-80MS application pause time
about some parameters
  • -xx:maxtenuringthreshold=n sets the maximum tenuring threshold for use in adaptive GC sizing. The current largest value is 15. The default value is a parallel collector and is 4 for CMS. (Note that this parameter defaults to 15, but for a CMS, the default is 4, which is excerpted from the official document)
  • -xx:+usecmscompactatfullcollection:cms is garbage collected using the tag-clear method, so the memory is not organized casually, this option allows you to specify the defragmentation of memory, which is turned on by default
  • -XX:+SCAVENGEBEFOREFULLGC: Specifies that a young GC is performed before FULLGC
  • The proportions used by the old age when the-xx:cmsinitiatingoccupancyfraction:cms is triggered
  • -XX:MAXGCPAUSEMILLIS=50: Maximum time of GC, MS, parallel Scanvenge algorithm and G1
  • -xx:pretenuresizethreshold: exceeds the set size, then the object will be allocated directly to the old age. The unit is byte, which defaults to 0 and does not turn on the feature. (This option is not available for the PS collection algorithm)
  • -xx:+handlerpromotionfailure: Before Minore GC, the JVM predicts whether the largest available contiguous space in the old age is greater than the total space of all objects in the new generation, and if this switch is turned on, the maximum available contiguous space in the JVM accounting age is greater than "Dynasties" young generation promoted to the old age the average size of all objects, if less than, will be minore GC, otherwise, full GC, if this switch is not open, then will go directly to the whole GC, (currently according to the JDK source code, this option is invalid, The JVM will make the above judgments directly)
several common misconceptions about GC:
  • In addition to CMS and G1, collection algorithms such as Psyoung Gen,defnew,psoldgen,paroldgen need to be STW.
  • STW (Stop-the-world) does not mean full gc,full GC refers to GC that occurs in the younger generation and the old age.
  • CMS is a GC algorithm that occurs in the old age, but two of these phases, initial marking and final remark occur in the young generation and the old age, so their STW belong to the full GC statistics.
  • When the CMS is running, the old age space is not enough, the default is to use the serial GC for a full GC.
References
  • https://www.zhihu.com/question/41922036/answer/93079526 Rednaxelafx's answer
  • Http://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html
  • Http://ifeve.com/useful-jvm-flags-part-7-cms-collector

Java GC-related knowledge

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.