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 are the basic knowledge points of java GC algorithm

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the basic knowledge points of java GC algorithm", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "what are the basic knowledge points of java GC algorithm"!

Translation of related terms:

Mark, mark;

Sweep, clear;

Compact: Compact:

Copy, copy, copy,

Note: The Garbage Collection Algorithm Manual translates Mark and Sweep as: mark-sweep algorithm; the translator thinks mark-sweep is easier to understand.

This chapter briefly introduces the basic principle and related technologies of GC, and the next chapter explains the specific implementation of GC algorithm in detail. The implementation details vary, but in general, garbage collectors focus on two things:

Find all surviving objects

The rest is discarded, dead objects, objects that are no longer in use.

The first step is to census all surviving objects, and there is a process called Marking in garbage collection that does this.

Marking Reachable Objects

The first step in all GC algorithms in modern JVM is to find all surviving objects. This is best illustrated in the following diagram:

First, there are certain objects that are designated Garbage Collection Roots. These include:

Local variables and input parameters in the currently executing method

Active threads

Static fields for all classes in memory

JNI reference

Second, GC traverses the entire object graph in memory, scanning from the GC root element to direct references and other objects (through the object's attribute domain). All objects accessed by GC are marked as surviving objects.

Surviving objects are shown in blue in the image above. After the marking phase is complete, all surviving objects are marked. Other objects (gray data structures in the image above) are unreachable from the GC root element, meaning that programs can no longer use unreachable objects. Such objects are considered garbage and GC will clean them up in the next phase.

There are several points to note during the marking phase:

During the marking phase, you need to pause all application threads to traverse all object references. Because you can't keep track of a reference graph that's changing all the time without pausing. This scenario is called Stop The World pause, and the point at which it is safe to pause a thread is called a safe point, after which the JVM can concentrate on cleaning up. Safe points can be triggered by a variety of factors, and GC is currently the most common cause of safe point triggers.

The duration of this pause is not directly related to heap size or the total number of objects, but rather to the number of live objects. So increasing the heap size does not directly affect the time taken by the marking phase.

After the marking phase is complete, GC proceeds to delete unreachable objects.

Removing Unused Objects

GC algorithms vary slightly in how they remove unreachable objects, but can be grouped into three categories: sweeping, compacting, and copying. These algorithms are explained in detail in the next chapter.

Sweep(Clear)

The concept of the Mark and Sweep algorithm is very simple: it simply ignores all garbage. This means that after the marking phase is complete, all unreachable objects are considered free and therefore available for allocation of new objects.

This algorithm requires a free-list to keep track of all the free regions and the size of each region. Maintaining an idle list increases the overhead of object allocation. There is also another vulnerability-there may not be a region of memory large enough to hold the objects that need to be allocated, resulting in allocation failures (OutOfMemoryError in Java).

Compact

Mark-Sweep-Compact algorithm migrates all marked objects (surviving objects) to the beginning of memory space, eliminating the shortcomings of Mark-Sweep algorithm. The corresponding disadvantage is that GC pause times increase because you need to copy all objects to another place and then modify references to them. The advantage of this algorithm is also obvious. After defragmentation, it is easy to allocate new objects by pointer bumping. With this algorithm, the remaining capacity of memory space is always clear and does not cause memory fragmentation problems.

Copy

Mark and Copy and Mark and Compact are very similar: both move all surviving objects. The difference is that the mark-copy algorithm moves memory to another space: the survivor. The advantage of the mark-copy approach is that marking and copying can be done simultaneously. The disadvantage is that an extra memory bin is needed to store all the surviving objects.

At this point, I believe that everyone has a deeper understanding of "what are the basic knowledge points of java GC algorithm", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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