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 use Java garbage collection algorithm

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

Share

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

This article focuses on "how to use Java garbage collection algorithm", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Java garbage collection algorithm.

Reference counting: in Java, references are associated with objects. If you want to manipulate an object, you must use a reference. Therefore, it is obvious that a simple way is to determine whether an object can be recycled by reference counting. To put it simply, if an object does not have any references associated with it, that is, their reference count is not zero, then the object is unlikely to be used again, then the object is a recyclable object.

Reachability analysis: in order to solve the circular reference problem of citation counting, Java uses the method of reachability analysis. Search through a series of "GC roots" objects as a starting point. If there is no reachable path between "GC roots" and an object, the object is said to be unreachable. It should be noted that an unreachable object is not equivalent to a recyclable object, and it takes at least two marking processes for an unreachable object to become a recyclable object. If it is still a recyclable object after being marked twice, it will face recycling.

Garbage collection algorithm

Tag cleanup algorithm (Mark-Sweep) is the most basic garbage collection algorithm, which is divided into two phases, labeling and clearing. The marking phase marks all the objects that need to be recycled, and the cleanup phase reclaims the space occupied by the marked objects. The biggest problem of this algorithm is the serious fragmentation of memory, and the problem that large objects can not find available space may occur later.

Replication algorithm (copying) is an algorithm proposed to solve the memory fragmentation defect of Mark-Sweep algorithm. The memory is divided into two blocks of the same size according to the memory capacity. Use only one piece at a time, and when this piece of memory is full, copy the surviving objects to another piece to clear the used memory. Although this algorithm is simple to implement, high memory efficiency, and not easy to produce fragments, the biggest problem is that the available memory is compressed to half of the original. And if the number of surviving objects increases, the efficiency of Copying algorithm will be greatly reduced.

The generation-by-generation collection algorithm is the method adopted by most JVM at present. Its core idea is to divide the memory into different domains according to the different life cycle of the object. Generally speaking, the 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.

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.

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.

(2) the memory allocation of objects is mainly in the new generation of Eden Space and Survivor Space's From Space (the part where Survivor currently stores objects), and in a few cases it will be allocated directly to the old generation.

(3) when the new generation of Eden Space and From Space space is insufficient, a GC will occur. After GC, the living objects in Eden Space and From Space will be moved to To Space, and then the Eden Space and From Space will be 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 Survivor area, his age will be + 1. By default, objects that reach the age of 15 are moved to the older generation.

At this point, I believe you have a deeper understanding of "how to use the Java garbage collection algorithm". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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