Triggering the JVM for full GC and coping strategies

Source: Internet
Author: User

Heap memory is divided into Eden, Survivor, and tenured/old spaces, as shown in:

Recovering memory from the young generation space (including the Eden and Survivor regions) is called the Minor GC, which is called the major GC for the old age GC, and the full GC is for the entire heap, In the last several versions of the JDK, the default includes the reclaim of the Immortal belt as a method area (no immortality in JDK8), which is often accompanied by at least one minor GC when full GC is present, but not absolute. Major GC is typically 10 times times slower than the minor GC. Let's look at the situation that triggers the JVM to perform full GC and coping strategies.

1. Invocation of the System.GC () method

The call to this method is to recommend that the JVM perform full GC, although it is only recommended, not necessarily, but in many cases it triggers the full GC, which increases the frequency of the full GC, which increases the number of intermittent pauses. The strong Impact Department recommends that you do not use this method, let the virtual machine to manage its own memory, you can disable RMI call System.GC by-xx:+ DISABLEEXPLICITGC.


2, the old generation of space shortage


The old age space is only insufficient when the new generation objects are transferred and created as large objects and large arrays, and when there is still insufficient space after executing full GC, the following error is thrown:
Java.lang.OutOfMemoryError:Java Heap Space
In order to avoid the above two conditions caused by the full GC, tuning should try to make the object in the minor GC phase is recycled, so that the object in the Cenozoic to survive for a period of time and do not create too large objects and arrays.

3. Lack of space in the living area


The method area in the runtime data region in the JVM specification, which is also used in the Hotspot virtual machine as an immortal or immortalized zone, is stored in permanet generation for some class information, constants, static variables, and so on, when the system loads classes, When a class is reflected and a method is called, permanet generation may be full, and a fully GC will be executed if it is not configured to adopt a CMS GC. If the full GC is still not recycled, the JVM throws the following error message:
Java.lang.OutOfMemoryError:PermGen Space
To avoid the full GC phenomenon caused by Perm Gen, the method is to increase the perm gen space or to use a CMS GC.


4, CMS GC appears promotion failed and concurrent mode failure


For programs that use the CMS for an older GC, pay particular attention to whether there are two promotion failed and concurrent mode failure in the GC log, which can occur when both of these conditions are present

Will trigger the full GC.
Promotion failed is in the minor GC, Survivor space, objects can only be put into the old age, and at this time the old age also can not be caused; Concurrent mode failure is in

In the process of executing the CMS GC, there are objects to be put into the old age, while the old age space is insufficient (sometimes "insufficient space" is the CMS GC when the current floating garbage too much caused the temporary lack of space to trigger the full GC).
The measures are as follows: Increase survivor space, older generation spaces, or reduce the rate of triggering concurrent GC, but in the JDK 5.0+, 6.0+ version of the JDK may cause the bug29 of the CMS in remark complete

After a long time to trigger the sweeping action. For this condition, you can avoid this by setting the-xx:cmsmaxabortableprecleantime=5 (in MS).


5. The average size of the minor GC promoted to the old generation is greater than the remaining space in the old age.


This is a more complex trigger situation, the hotspot in order to avoid the new generation of objects promoted to the old generation of space caused by the old generation of the phenomenon, in the minor GC, made a judgment, if the

The average size of the minor GC promoted to the old generation is greater than the remaining space of the old generation, and the full GC is triggered directly.
For example, after the first time the program triggered the minor GC, there are 6MB objects promoted to the old generation, then the next time minor GC occurs, first check the old generation of the remaining space is greater than 6MB, if less than 6MB,

The full GC is executed.
When the new generation uses PS GC, the way is slightly different, PS GC is also checked after minor GC, for example, the first minor GC in the above example, the PS GC will check if the old generation of the remaining space

Greater than 6MB, such as less than, triggers the recovery of the old generation.
In addition to the 4 scenarios above, the full GC is executed one hour by default for sun JDK applications that use RMI for RPC or management. Can be passed at startup by-Java-

dsun.rmi.dgc.client.gcinterval=3600000 to set the interval between full GC execution or-xx:+ DISABLEEXPLICITGC to prohibit RMI from calling System.GC.

6. Large objects allocated in the heap

A large object is a Java object that requires a large amount of contiguous memory space, such as a very long array, which goes directly into the old age, while the old age has a lot of space left, but cannot find enough contiguous space to allocate to the current object, which triggers the JVM to perform a full GC.

To solve this problem, the CMS garbage collector provides a configurable parameter, the-xx:+usecmscompactatfullcollection switch parameter, for an extra free defragmentation process after "enjoy" the full GC service, The process of memory collation can not be concurrent, space debris problem is not, but the time of the Tim is not long, the JVM designers also provided another parameter-xx:cmsfullgcsbeforecompaction, this parameter is used to set the number of times to execute the full GC, Then come with a compressed one.

Triggering the JVM for full GC and coping strategies

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.