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 garbage collection algorithm in JVM?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about the garbage collection algorithm in JVM, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1, Mark-clear algorithm

Concept: first, mark out all the objects that need to be recycled, and uniformly recycle all marked objects after the marking is completed. Features: 1) efficiency problem: the efficiency of marking and removal is not high. 2) Space problem: a large number of discontiguous memory fragments will be generated after mark removal, and too many fragments may lead to the need to allocate larger objects in the future. because you can't find enough continuous memory, you have to trigger another garbage collection in advance.

2, replication algorithm

Concept: divide the available memory into two blocks of equal size, using only one of them at a time. When this piece of memory is used up, the surviving objects are copied to another block, and then the used memory blocks are cleaned up at once. Features: 1) efficiency: easy to implement, high efficiency; 2) Space problem: reduce the size of memory to half of the original application: recycling reminders of the new generation: 1) most of the objects in the new generation are "life and death". All do not need to install 1:1 ratio to divide the memory space. 2) the virtual machine divides the new generation of memory into a larger Eden space and two smaller Survivor spaces (Survivor to and Survivor from), using Eden space and one piece of Survivor space each time. Process: 1) each time an object is created, it will first be allocated in Eden, and if Eden is full, it will be allocated in Survior from. If neither Eden nor Survior from can accommodate the object, JVM will do a GC; 2) GC, copy the surviving objects in Eden and Survivor from to another Survivor to space at once, and finally clean up the Eden and Survivor form space. 3) when there is not enough Survivor to space, we need to rely on the old age to allocate the guarantee. For example, the default Eden and two Survivor size ratio of the HotSpot virtual machine is 8:1:1, that is, 90% of the memory available in each new generation is 90% of the entire new generation capacity, and only 10% of the memory is wasted. Memory allocation guarantee mechanism: before the new generation GC (Minor GC), the virtual machine first checks whether the maximum contiguous space available in the old era is greater than the total space of all objects of the new generation. 1) if the condition is met, then the new generation GC can ensure that it is safe. 2) if the condition is not met, the virtual machine checks whether the HandlePromotionFailure setting allows the guarantee to fail. If the guarantee is allowed to fail, then it will continue to check whether the maximum contiguous space available in the old age is larger than the average size of the objects promoted to the old age. If so, a new generation GC will be carried out, although this new generation GC is risky. If less than, then the new generation GC will not be carried out at this time. Instead, there will be an old GC (Full GC). If the guarantee is not allowed to fail, then there will be no new generation of GC, but an old GC (Full GC).

3, marking-finishing algorithm

Concept: first mark all the objects that need to be recycled, move all the living objects to one end after the tag is complete, and then directly clean up the other end of the non-living objects.

4, generation collection algorithm

Java heap is divided into the new generation and the old era, in the new generation using replication algorithm recycling, in the old era using mark-clean or mark-finishing algorithm to recycle the above content, do you have any further understanding of the garbage collection algorithm in JVM? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Servers

Wechat

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

12
Report