JVM garbage collection policy

Source: Internet
Author: User

Why generation division?

The generational garbage collection policy is based on the fact that different objects have different lifecycles. Therefore, objects in different lifecycles can be collected in different ways to improve recycling efficiency.

During Java program running, a large number of objects are generated, some of which are related to business information, such as session objects, threads, and socket connections in HTTP requests, this type of object is directly linked to the business, so the lifecycle is relatively long. However, there are still some objects, mainly temporary variables generated during the program running process. These objects will have a short life cycle, such as string objects, because of their unchanged class features, the system generates a large number of these objects, and some objects can be recycled only once.

Imagine that, without differentiation of the object survival time, every garbage collection process recycles the entire heap space, which takes a long time. At the same time, because every reclaim operation requires traversing all the surviving objects, but in fact, this traversal is ineffective for objects with a long life cycle, because it may have been traversed many times, but they still exist. Therefore, the division of generation garbage collection adopts the idea of division and governance to divide the objects of different life cycles on different generations, different generations use the most suitable garbage collection method for recycling.

  Generation Division



Virtual machines are divided into three generations: young generation, old generation, and permanent generation ). The persistent generation mainly stores the class information of Java classes, which has little to do with Java objects to be collected by garbage collection. The division of the young and old generations has a great impact on garbage collection.

Young generation:

All newly generated objects are first put in the young generation. The goal of the young generation is to quickly collect objects with short lifecycles as much as possible. The young generation is divided into three zones. One Eden zone and two vor zones (generally ). Most objects are generated in the Eden area. When the Eden zone is full, the surviving objects will be copied to the primary vor zone (one of the two). When the primary vor zone is full, the surviving objects in this region will be copied to another region vor. When the region VOR is full, the surviving objects will be copied from the first region vor, it will be copied as "tenured )". Note that the two regions of the same vor are symmetric and irrelevant. Therefore, the same zone may be copied from Eden at the same time.
Objects, and objects copied from the previous primary vor, but only objects copied from the first primary vor in the old area. In addition, there is always a blank vor area. At the same time, according to the program requirements, the VOR area can be configured as multiple (more than two), which can increase the time for the object to exist in the young generation and reduce the possibility of being put into the old generation.

Elder Generation:

Objects that are still alive after N garbage collection in the young generation will be put into the old generation. Therefore, it can be considered that objects with long lifecycles are stored in the old generation.

Permanent generation:

Used to store static files. Currently, Java classes and methods are supported. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as hibernate, in this case, you need to set up a large persistent storage space to store the classes added during the running process. The persistent generation size is set through-XX: maxpermsize =.

  Under what circumstances will garbage collection be triggered?

Because the object is divided into generations, the garbage collection area and time are different. There are two types of GC: scavenge GC and full GC.

Scavenge GC

Generally, when a new object is generated and the Eden application fails, scavenge GC is triggered to perform GC on the Eden region to clear non-surviving objects, and move the surviving objects to the same vor area. Then, sort out the two zones in the same vor. In this way, GC is performed on the Eden area of the young generation and will not affect the old generation. Because most objects start from the Eden area and the Eden area is not allocated much, GC in the Eden area is performed frequently. Therefore, it is generally necessary to use fast and efficient algorithms so that Eden can be idle as soon as possible.

Full GC

Organize the entire heap, including young, tenured, and perm. Full GC is slower than scavenge GC because it is necessary to recycle the entire GC, so the number of full GC should be minimized. In the process of JVM optimization, a major part of the work is to adjust fullgc. Full GC may occur due to the following reasons:

· The tenured is full

· Perm is full

· System. GC () is displayed and called

· Dynamic Change of heap allocation policies for various domains after the last GC

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.