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 principle of replication algorithm in java garbage collection algorithm?

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

Share

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

Today, I would like to share with you the relevant knowledge about the principle of the replication algorithm in the java garbage collection algorithm. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Algorithm principle

The replication algorithm first divides the memory space of or into two blocks, using only one of them at a time, and reduplicates the living objects in the memory being used when garbage is searched.

Make it into the unused memory block, then clear all the objects in the memory block you are using, swap 2 memory roles, and finally complete garbage collection.

GC in the younger generation of image

HotSpot JVM divides the younger generation into three parts: one Eden area and two Survivor regions (called from and to, respectively). The default ratio is 8:1, why the default is this ratio, we will talk about next. In general, newly created objects are assigned to the Eden area (some special treatment for large objects), and these objects are moved to the Survivor area if they are still alive after the first Minor GC. Each time the object endures Minor GC in the Survivor area, its age increases by 1 year, and when its age increases to a certain extent, it is moved to the older generation.

Because objects in the younger generation are basically life-and-death (more than 80%), the garbage collection algorithm of the younger generation uses a replication algorithm. The basic idea of the replication algorithm is to divide the memory into two pieces, using only one of them at a time. When this piece of memory is used up, the living object is copied to the other. The replication algorithm does not produce memory fragmentation.

At the beginning of GC, objects only exist in the Eden area and the Survivor area named "From", and the Survivor area "To" is empty. Then all the surviving objects in the GC,Eden area are copied to the "To", while in the "From" area, the surviving objects decide where to go based on their age. Objects that reach a certain age threshold (age threshold, which can be set by-XX:MaxTenuringThreshold) are moved to the older generation, and objects that do not reach the threshold are copied to the "To" area. After this GC, the Eden area and From area have been emptied. At this time, "From" and "To" will exchange their roles, that is, the new "To" is the "From" before the last GC, and the new "From" is the "To" before the last GC. In any case, the Survivor area named To is guaranteed to be empty. Minor GC repeats this process until the "To" area is filled and the "To" area is filled, and all objects are moved to the older generation.

There are problems

Because most of the objects in JVM are transient and their life cycle is very short, replication algorithms are widely used in the younger generation. The idea of partitioning and replication not only greatly improves the efficiency of garbage collection, but also makes the original complex memory allocation algorithm more concise than ever (since each memory collection is the recovery of the entire half-area space, memory allocation does not have to consider complex situations such as memory fragments, as long as you move the pointer at the top of the stack and allocate memory sequentially).

However, in garbage collection technology, the cost of improving the efficiency of replication algorithms is to reduce the available memory by half.

These are all the contents of this article entitled "what is the principle of replication algorithm in java garbage collection algorithm?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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