Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the garbage collection strategy of JVM generation in JVM

2025-02-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to analyze the JVM generation garbage collection strategy in JVM. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Because different objects have different lifecycles, there is a generational strategy in JVM's garbage collection strategy.

The basic concept of JVM generational garbage collection strategy

Why do we need generations?

The JVM generational garbage collection strategy is based on the fact that different objects have different lifecycles. Therefore, objects with different life cycles can be collected in different ways in order to improve the efficiency of recycling.

In the process of running Java programs, 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, which are directly linked to the business, so the life cycle is longer. But there are also some objects, mainly temporary variables generated during the running of the program, whose life cycle will be relatively short, such as: String object, because of its immutable class characteristics, the system will produce a large number of these objects, and some objects can even be recycled only once.

Imagine that without distinguishing the survival time of objects, each garbage collection collects the entire heap space, which takes a relatively long time, but in fact, this traversal is ineffective for objects with a long life cycle, because each collection needs to traverse all the living objects, because there may be many traverses, but they still exist. Therefore, generation-by-generation garbage collection adopts the idea of divide and conquer, divides the generations, puts the objects of different life cycle on different generations, and uses the most suitable garbage collection method for different generations.

How to divide the generations

As shown in the figure:

Virtual machines are divided into three generations: the younger generation (YoungGeneration), the older generation (OldGeneration) and the persistent generation (PermanentGeneration). The persistent generation mainly stores the class information of the Java class, which has little to do with the Java objects to be collected by garbage collection. The division of the younger generation and the older generation has a great impact on garbage collection.

Younger generation:

All newly generated objects are first of all placed in the younger generation. The goal of the younger generation is to collect objects with a short life cycle as quickly as possible. The younger generation is divided into three districts. One Eden zone and two Survivor zones (generally speaking). Most objects are generated in the Eden zone. When the Eden area is full, the surviving objects will be copied to the Survivor area (one of the two). When the Survivor area is full, the surviving objects in this area will be copied to another Survivor area. When the Survivor is full, the objects copied from * * Survivor areas and still alive at this time will be copied "Tenured". It should be noted that the two regions of Survivor are symmetrical, so there may be objects copied from Eden and objects copied from the previous Survivor in the same zone, while objects copied to the old zone only come from * Survivor. Also, one of the Survivor zones is always empty. At the same time, according to the needs of the program, the Survivor area can be configured as multiple (more than two), which can increase the duration of the object in the younger generation and reduce the possibility of being placed in the older generation.

Senior generation:

Objects that are still alive after N garbage collections in the younger generation will be put into the older generation. Therefore, it can be considered that the objects stored in the elderly are objects with a longer life cycle.

Persistent generation:

Used to store static files, now Java classes, methods, and so on. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some class, such as Hibernate, at this time, you need to set a large persistent generation space to store these new classes. The persistent generation size is set through-XX:MaxPermSize=.

Under what circumstances will JVM generation garbage collection be triggered?

Because the object is processed by generations, the garbage collection area and time are also different. There are two types of GC: ScavengeGC and FullGC.

ScavengeGC

In general, when a new object is generated and when Eden fails to apply for space, it triggers ScavengeGC, GC the Eden area, clears non-living objects, and moves objects that are still alive to the Survivor area. Then sort out the two districts of Survivor. This way of GC is carried out on the Eden area of the younger generation and will not affect the older generation. Because most of the objects start in the Eden area, and the Eden area is not allocated very large, the GC of the Eden area occurs frequently. Therefore, we generally need to use fast and efficient algorithms here, so that Eden can be idle as soon as possible.

FullGC

Organize the entire heap, including Young, Tenured, and Perm. FullGC is slower than ScavengeGC because it needs to recycle the entire pair, so the number of FullGC should be reduced as much as possible. In the process of tuning JVM, a large part of the work is to adjust FullGC. FullGC can occur for the following reasons:

The Tenured of ◆ is full of

The ◆ persistent generation (Perm) is full of

◆ System.gc () is displayed to call

Dynamic changes of Domain allocation policies of Heap since the last GC of ◆

This is the end of how to analyze the JVM generation garbage collection strategy in JVM. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report