Jvm1.6 GC details

Source: Internet
Author: User
Tags xms
Preface

Jvm gc is the memory collection algorithm of JVM. Adjusting jvm gc (garbage collection) can greatly reduce the program running interruption caused by GC, in this way, the Java program's work efficiency can be appropriately improved. However, adjusting GC is an extremely complex process. Therefore, we need to understand the JVM memory composition, collection algorithm, and object allocation mechanism.

 

JVM heap memory Composition

The Java heap consists of the perm and heap areas. The heap area consists of the old area and the new area (also called the Young area). The new area consists of the Eden area, the from area, and the to area).


The Eden area is used to store newly generated objects. The object life in Eden will not exceed one minor GC.

The same vor space contains two objects that survive each garbage collection, namely, S0 and s1.

The old generation old area, also known as the old generation, mainly stores surviving objects with long lifecycles in applications.

 

The initial memory allocated by JVM is specified by-XMS, and the maximum memory allocated by JVM is specified by-xmx. By default, when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-xmx. When the free heap memory is greater than 70%, the JVM will reduce the minimum limit of heap until-XMS. Therefore, the server generally sets-XMS and-xmx to be equal to each other to avoid adjusting the heap size after each GC.

-XX: newratio = You can set the size ratio of young to old. The default value of-server is. If it is too small, the large object will be directly allocated to the old area, increase the number of major collections executions and affect performance.
-XX: Required vorratio = the parameter can be set to set the ratio of Eden to dedicated vor. The default value is. Large vio will be wasted. If it is small, it will cause some large objects to be in the minor GC process, directly escape from the Eden area to the old area, which causes frequent GC in the old area. This parameter should be set to the default value, which generally has little impact on performance.

After the startup, you can view it through jmap-heap [pid. Because the overall size of the heap is fixed, the larger the young generation, the smaller the tenured generation, and the higher the number of major collections executions. Therefore, the best choice is determined by the lifecycle distribution of objects.

 

Collector in the new area

 

1. Serial GC (Serial copying)

The default GC mode in client mode can also be specified using-XX: + useserialgc.

2. Parallel GC recovery (parallel scavenge)
The default GC mode in server mode can also be forcibly specified by-XX: + useparallelgc.

When PS is used, by default, the JVM dynamically adjusts the EDEN: S0: S1 ratio during runtime. If you do not want to adjust it automatically, you can use the-XX:-useadaptivesizepolicy parameter, the memory allocation and collection algorithms are the same as those in serial mode. The only difference is that the multi-thread mode is used when the memory is recycled.

3. Parallel GC (parnew)

Cms gc is used by default, or-XX: + useparnewgc can be used. Memory Allocation, recovery, and PS are the same. The difference is that they are packed and processed with CMS.

 

Several collectors in the old area

1. Serial GC (Serial MSC)

The default GC mode in client mode can be forcibly specified by-XX: + useserialgc. It takes a lot of time to collect all data and perform compact.

2. Parallel GC (parallel MSC) (Note: high throughput, but slow response during GC)

-XX: + useparallelgc = can be forcibly specified for the default GC mode in server mode. You can add equal signs after the option to specify the number of parallel threads.

3. Concurrent GC (CMS): the GC method used in the online environment, that is, the realese environment. (Note: The response is much faster than the parallel GC, but the throughput is reduced)

CMS is used to reduce the pause time during GC execution. The garbage collection thread and Application Thread run simultaneously. You can use-XX: + useconcmarksweepgc = to specify the number of concurrent threads. Each CMS collection only pauses for a short period of time. The second time is slightly longer at the beginning (initial marking) and the middle (final marking. For more information about the CMS process, see related documents. In jstat, both initial mark and remark are counted as FGC.

One of the major problems with CMS is the fragmentation and floating garbage (floating gabage ). Because CMS does not compact the memory by default, you can use-XX: + usecmscompactatfullcollection.

 

In general, the size of the old area is large, and the garbage collection algorithm is time-consuming. As a result, the Application Thread stops working for a long time and compact is required. Therefore, there should be no more major GC. The time for major GC is usually dozens of times that of minor GC. The focus of JVM memory optimization is to reduce the number of major GC times, because for major GC, the program will be suspended for a long time. If the number of major GC times is large, this means that the JVM memory parameters of the application need to be adjusted.

 

JVM Memory Allocation Policy

 

1. objects are preferentially allocated in Eden.


If the Eden area does not have enough allocation objects, a minor GC will be made to recycle the memory and try to allocate objects. If the allocation is still not enough, it will be allocated to the old area.


2. large objects directly enter the Old Age


A large object is a Java object that requires a large amount of continuous memory space. The most typical large object is the long string and array. The VM provides the-XX: pretenuresizethreshold parameter, objects larger than this value are directly allocated in the old age. The purpose of this operation is to avoid a large number of memory copies between the Eden zone and the two vor zones (the new generation uses the replication algorithm to collect memory ). The pretenuresizethreshold parameter is only valid for the serial and parnew collectors,

3. Long-lived objects will enter the Old Age

After several minor GC operations, the system still exists: after the minor GC is triggered, the surviving object is stored in the same vor region. After multiple minor GC operations, the object is promoted to the old area.
Since the virtual machine uses the idea of generational collection to manage memory, it must be able to identify which objects should be stored in the new generation and which objects should be stored in the old generation. To achieve this, the virtual machine defines an object age counter for each object. If the object is still alive after being born in Eden and after the first minor GC and can be accommodated by the same vor, it will be moved to the same vor space and set the age of the object to 1. The age of an object increases by one year for every minor GC time in the VOR region. When the age of the object increases to a certain degree (15 years by default, will be promoted to the old age. You can set the age threshold of an object in the old age through the-XX: maxtenuringthreshold parameter.

4. Dynamic Object age determination


In order to better adapt to the memory conditions of different programs, virtual machines do not always require that the object age must reach maxtenuringthreshold to be promoted to the old age, if the total size of all objects of the same age in the primary vor space is greater than half of the primary vor space, objects of the same age or age can directly enter the old age, you do not need to wait until the age specified in maxtenuringthreshold.


5. After minor GC, if there is not enough vor space in the region, it will be placed in the old zone.

6. space allocation guarantee

In the case of minor GC, the virtual opportunity checks whether the average size of each previous promotion to the old age is greater than the remaining space of the old age. If it is greater than, a full GC is performed directly. If the value is less than, check whether the handlepromotionfailure setting allows guarantee failure. If the value is allowed, only minor GC is performed. If the value is not allowed, full GC is performed again. In most cases, the handlepromotionfailure switch is enabled to avoid too frequent full GC.

 

Jvm gc combination

 

 

How to monitor GC

1. Overview monitoring GC.

Jmap-heap [pid] view memory distribution

Jstat-gcutil [pid] 1000 output the GC of the Java Process every 1 s

2. Monitor GC in detail.

In the JVM startup parameter, add-verbose: GC-XX: + printgctimestamps-XX: + printgcdetails-xloggc:./GC. log.

Input example:

 [GC [ParNew: 11450951K->1014116K(11673600K), 0.8698830 secs] 27569972K->17943420K(37614976K), 0.8699520 secs] [Times: user=11.28 sys=0.82, real=0.86 secs]

It indicates that a minor GC occurs, parnew is the GC algorithm of the new generation, 1110000951k indicates the total memory size of the surviving objects in the Eden area, and 1014116k indicates the total memory size of the surviving objects after collection, 11673600k is the total memory of the entire Eden area. 0.8699520 secs indicates the time spent by minor GC.

27569972k indicates the total number of surviving objects in the heap region, 17943420k indicates the total number of surviving objects in the heap region after recycling, and 37614976k indicates the total memory size in the heap region.

[Full GC [tenured: 27569972 K-> 16569972 K (27569972 K), 180.2368177 secs] 36614976 K-> 27569972 K (37614976 K), [perm: 28671 K-> 28635 K (28672 K)], 0.2371537 secs]

It indicates that a full GC has occurred, and the entire JVM has paused for more than 180 seconds. The output description is the same as above. Only tenured: 27569972 K-> 16569972 K (27569972 K) indicates the old zone, while the Eden zone is above.

 

For more information, see Alibaba Cloud's PPT sunjdk1.6gc.pptx.

Jvm1.6 GC details

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.