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

What is the generational collection algorithm of JVM runtime memory?

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

Share

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

This article is about what the generation-by-generation algorithm of JVM runtime memory collection is. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

2.4.5. Generation collection algorithm

Generation-by-generation collection is the method adopted by most JVM at present. Its core idea is to divide memory into different domains according to the different life cycle of objects. Generally speaking, GC heap is divided into Tenured/Old Generation and Young Generation. The characteristic of the old generation is that only a small number of objects need to be recycled each time, and the characteristic of the new generation is that there is a large amount of garbage to be recycled every time, so we can choose different algorithms according to different regions.

2.4.5.1. New Generation and replication algorithm

At present, most of the GC of JVM adopt the Copying algorithm for the new generation, because in the new generation, most of the objects are collected for each garbage collection, that is, there are fewer operations to copy, but the new generation is not usually divided according to 1:1. Generally, the Cenozoic era is divided into a larger Eden space and two smaller Survivor spaces (From Space, To Space). The Eden space and one of the Survivor spaces are used each time. When recycling, the surviving objects in the two spaces are copied to another Survivor space.

2.4.5.2. Old Age and Mark replication algorithm

In the old days, because only a small number of objects were recycled at a time, the Mark-Compact algorithm was adopted.

1. The Permanet Generation in the method area mentioned by the JAVA virtual machine, which is used to store class classes, constants, method descriptions, etc. The recycling of the immortal generation mainly includes abandoned constants and useless classes.

two。 The memory allocation of objects is mainly in the new generation of Eden Space and Survivor Space's From Space (the Survivor currently holds the object), and in a few cases it will be allocated directly to the older generation.

3. A GC occurs when the new generation of Eden Space and From Space runs out of space. After GC, the living objects in the Eden Space and From Space areas are moved to To Space, and then the Eden Space and From Space are cleaned.

4. If To Space cannot store an object sufficiently, store the object to the old generation.

5. After GC, Eden Space and To Space are used, and so on.

6. When the object avoids GC once in the Survivor area, its age will be + 1. By default, objects that reach the age of 15 are moved to the older generation.

Reference types in 2.5.JAVA IV

2.5.1. Strong citation

The most common thing in Java is strong reference, which assigns an object to a reference variable, which is a strong reference.

Use it. When an object is referenced by a strong reference variable, it is in a reachable state and cannot be reclaimed by the garbage collection mechanism, that is,

So that the object will never be used in JVM or recycled in the future. Therefore, strong reference is the main cause of Java memory leak.

one.

2.5.2. Soft reference

Soft references need to be implemented with the SoftReference class, and for objects with only soft references, when the system has enough memory, it

It will not be recycled, and it will be recycled when the system runs out of memory space. Soft references are commonly used in memory-sensitive programs.

2.5.3. Weak reference

Weak references need to be implemented with WeakReference classes, which have a shorter lifetime than soft references, and for objects with only weak references

As long as the garbage collection mechanism is running, the memory occupied by the object will be reclaimed regardless of whether the JVM has enough memory space or not.

2.5.4. Virtual reference

Virtual references need to be implemented by the PhantomReference class, which cannot be used alone and must be used in conjunction with reference queues. The main function of virtual references is to track the status of objects being garbage collected.

2.6.GC generation collection algorithm

VS partition collection algorithm

2.6.1. Generation collection algorithm

At present, the mainstream VM garbage collection adopts the "generation collection" (Generational Collection) algorithm, which divides the memory into several blocks according to the different survival periods of objects, such as the new generation, the old generation and the permanent generation in JVM, so that the most appropriate GC algorithm can be adopted according to the characteristics of each age.

2.6.1.1. In the new generation-replication algorithm

With each garbage collection, a large number of objects are found dead and only a few survive. Therefore, by choosing the replication algorithm, the collection can be completed by paying only a small amount of the replication cost of living objects.

2.6.1.2. In the old days-tagging algorithm

Because the object has a high survival rate and there is no extra space to allocate it, it is necessary to use the "mark-clean" or "mark-organize" algorithm to reclaim the object without copying memory and directly freeing up free memory.

2.6.2. Partition collection algorithm

In the partition algorithm, the whole heap space is divided into continuous different cells, each cell is used independently and recycled independently. The advantage of this is that you can control how many cells are reclaimed at a time, and reasonably reclaim several cells at a time (rather than the entire heap) according to the target pause time, thereby reducing the pause caused by a GC.

2.7.GC garbage collector

Java heap memory is divided into two parts: the new generation and the old generation, the new generation mainly uses the copy and mark-clear garbage collection algorithm, and the old generation mainly uses the mark-clean garbage collection algorithm, so the java virtual machine provides a variety of different garbage collectors for the new generation and the old generation respectively. The garbage collector of the Sun HotSpot virtual machine in JDK1.6 is as follows

Thank you for reading! This is the end of this article on "what is the generation-by-generation collection algorithm of JVM runtime memory?". I hope the above content can be of some help to you, so that you can 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

Internet Technology

Wechat

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

12
Report