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

Detailed introduction of garbage collection algorithm of JVM

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "JVM garbage collection algorithm detailed introduction", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "JVM garbage collection algorithm detailed introduction"!

First, how to judge that the object is dead.

1, reference counting algorithm

If an object does not have any reference to it, it can be considered "dead". The drawback of this method is that the existence of the reference ring cannot be detected.

Algorithm characteristics

1. Separate field storage counters are required, which increases the overhead of storage space

two。 The counter needs to be updated for each assignment, which increases the time overhead

3. Garbage objects are easy to identify and can be used as garbage collection as long as the counter is 0.

4. Timely recycling of garbage without delay

5. Can not solve the problem of circular reference

2, root search algorithm

Java uses the root search algorithm to collect garbage. The basic principle of this algorithm is to define a series of objects called GC Roots as the starting point, search from the starting point down, and the path taken by the search is called the reference chain.

When an object is not linked to the GC Roots by any reference chain, the object is not available, and the Java virtual machine can recycle these objects.

The Java virtual machine defines the following objects as GC Roots:

1), objects referenced in the Java virtual machine stack: for example, the local variable User user= new User () is defined in the method.

2), objects referenced by static attributes: for example, private static User user = new User ()

3), objects referenced by constants: for example, private static final User user = new User ()

4), objects referenced in the local method stack

Second, commonly used garbage collection algorithms

1, mark-clear algorithm.

The algorithm consists of two stages: marking and clarity:

First of all, all the garbage objects that need to be recycled at the mark will be recycled uniformly after the marking is completed.

The main shortcomings of the algorithm are as follows:

A), one is the question of efficiency. Both labeling and clarity are inefficient.

B), one is the problem of space. Clearly marked will result in a large number of discontiguous memory fragments. Too much space debris will make it impossible to find continuous memory when a program needs to fragment a large amount of memory, so it has to start another garbage collection in advance. The execution process of the mark-clear algorithm is shown below:

2, replication algorithm

This algorithm divides the memory into two areas of equal size, each of which uses one of them. in the process of recycling, all the surviving objects are copied to the other area to clear the original area. In the younger generation, this algorithm is used in the Eden region and two survivor regions. This algorithm only replicates the living objects, the cost is low, and there is no memory fragmentation problem, the disadvantage is that it needs 2 times the memory space.

3. Tag finishing algorithm

The replication collection algorithm will carry out more replication operations when the object survival rate is high, and the efficiency will become very low. More crucially, if you don't want to waste half of your memory space, you need additional memory allocation guarantees to deal with the extreme situation in which all objects in used memory are 100% alive, so this algorithm is not used in the old days.

According to the characteristics of the old years, someone proposed another mark-finishing algorithm, the marking process is still the same as the mark-clearing algorithm, and then directly clean up the memory outside the end boundary. The schematic diagram of the "mark-finishing" algorithm is as follows:

4, generation recovery algorithm

At present, the garbage collectors of commercial virtual machines all use generational collection algorithm, which has no new idea, but costs several pieces of memory according to the different survival cycle of objects. Generally, the java heap is divided into the new generation and the old age, so that the most appropriate collection algorithm can be adopted according to the characteristics of each age. In the new generation, each garbage collection found that a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small number of copies of living objects to complete the collection. In the old days, because of the high survival rate of the object, there was no extra space to allocate guarantee to it, so it was necessary to use tag removal or tag collation algorithm for garbage collection.

Third, the strategy of memory allocation and recovery

At present, the generation of JVM is mainly divided into three periods:

New generation: all newly created objects are first allocated memory in the new generation. The Cenozoic era can be divided into three regions, one Eden region, one From Survivor region and one To Sruvivor region. Most of the objects are assigned to the Eden area. When the Eden area is full, the surviving objects will be copied to the From Survivor area, and when the From Survivor area is full, the surviving objects in this area will be copied to the To Survivor area. Finally, when the To Survivor area is also full, objects that have been copied from the From Survivor area and are still alive will be copied to the old age.

Old age: objects who survive N times (usually 15 times) GC in the younger generation will be placed in the old age. Therefore, it can be considered that the old age is to store some objects with a longer life cycle.

Persistent generation: used to store static files, such as Java classes.

Types of GC:

New generation GC (Minor GC):

It refers to the garbage collection actions that occur in the new generation. Because most Java objects have the characteristics of dying out, MinorGC is very frequent and the collection speed is generally fast.

The old GC (MajorGC/FullGC):

Refers to the old GC, the emergence of MajorGC, often accompanied by at least one MinorGC (but not absolutely, there is a policy selection process for MajorGC directly in the collection policy of the Parallel Scavenger collector).

MajorGC is generally more than 10 times slower than MinorGC.

Allocation policy for the object:

In most cases, objects are allocated in the Cenozoic Eden zone. When there is not enough space in the Eden zone to allocate, the virtual machine will initiate a GC.

The big object goes straight into the old age. The so-called large objects are Java objects that require a lot of contiguous memory space. The classic large objects are very long strings and arrays.

Those who survive for a long time will enter the old age. In order to distinguish whether the object should be placed in the old age or the new generation, the virtual machine defines an object age counter for each object. If the object is born in Eden and survives after the first MinorGC, and can be accommodated by Survivor, it will be moved to Survivor space and the age will be set to 1. Each time the object survived the MinorGC in the Survivor area, the age increased by 1 year. When he is over a certain age (the default is 15), he will be promoted to the old age. The threshold for an object to be promoted to the old age can be set by the parameter-XX:MaxTenuringThreshold.

Dynamic object age binding:

In order to better adapt to the memory conditions of different programs, the virtual machine does not always require that the age of the object must reach the MaxTenuringThreshold in order to promote the old age. If the size of all objects of the same age in the Survivor space is more than half of the Survivor space, the objects whose age is greater than or equal to that age can directly enter the old age without waiting for the required age in the MaxTenuringThreshold.

Space allocation guarantee:

Before MinorGC occurs, the virtual machine checks whether the largest contiguous space available in the old era is larger than the total space of all objects in the new generation, and if this condition is true, then MinorGC can ensure that it is secure. If not, the virtual machine checks to see if the HandlePromotionFailure setting allows the guarantee to fail. If allowed, it will continue to check whether the maximum contiguous space available in the old age is larger than the average size of the previous promotion to the old age object, and if so, try a MinorGC, although this time the MinorGC is risky; if it is less than, or the HandlePromotionFailure setting does not allow risk, then do a FullGC instead.

The risk actually refers to: the space of the old age may not be able to accommodate all the living objects of the youth, once it can not, it will need to conduct a Full GC.

Fourth, garbage collector

Thank you for your reading, the above is the content of "detailed introduction of JVM garbage collection algorithm". After the study of this article, I believe you have a deeper understanding of the detailed introduction of JVM garbage collection algorithm, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report