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 understand the garbage collection of Java

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

Share

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

Most people do not understand the knowledge points of this article "how to understand garbage collection in Java", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to understand garbage collection in Java" article.

Before talking about memory sets and card tables, I'd like to introduce you to the problem of intergenerational references.

If you want to conduct a collection limited to the Cenozoic area (Minor GC) now, but the Cenozoic instance object 1 is referenced in the old era, in order to find out all the living objects in this area (Cenozoic), you have to traverse all the objects in the old age in addition to the fixed GC Roots to ensure the correctness of the accessibility analysis results, and vice versa. Although the scheme of traversing all objects throughout the old age is feasible in theory, it will undoubtedly put a great performance burden on memory recovery.

In fact, it is not only between the new generation and the old era that there is the problem of intergenerational references. All garbage collectors that involve partial area collection (Partial GC) behavior, such as G1, ZGC and Shenandoah collectors, will face the same problem.

So how can intergenerational references be solved?

First of all, intergenerational references account for only a small number of references compared with those of the same generation. The reason is that objects with intergenerational references should tend to live or die at the same time (for example, if there is an intergenerational reference in a Cenozoic object, because it is difficult for the old object to die, the citation will also make the Cenozoic object survive at the time of collection, and then be promoted to the old age as you get older, when the intergenerational reference is eliminated).

According to what has been mentioned above, we should no longer scan the whole old age for a small number of intergenerational references, nor waste space to record whether each object exists and which intergenerational references exist, just establish a global data structure in the new generation (this structure is called "memory set", Remembered Set), which divides the old age into small blocks to identify which memory of the old era will have intergenerational references. After that, when Minor GC occurs, only small chunks of memory containing intergenerational references are added to the GCRoots for scanning. Although this approach requires maintaining the correctness of the recorded data when the object changes the reference relationship (such as assigning itself or an attribute to a value), it adds some runtime overhead, but it is still cost-effective compared to scanning the entire old age when collecting.

Let's take a look at this global data structure memory set.

Memory set

A memory set is an abstract data structure used to record a collection of pointers from a non-collection area to a collection area. If we do not consider efficiency and cost, the simplest implementation can implement this data structure with an array of all objects in the non-collection area that contain cross-generational references, as shown in the following code:

/ / use object pointer to implement pseudo code Class RememberedSet {Object [] set [object _ INTERGENERATIONAL_REFERENCE_SIZE];}

This kind of record all contains the implementation scheme of cross-generational reference objects, which is very expensive both in space consumption and maintenance. In the garbage collection scenario, the collector only needs to determine whether a non-collection area has a pointer to the collection area through the memory set, and does not need to know all the details of these inter-generational pointers. When implementing the memory set, the designer can choose a more rugged record granularity to save the storage and maintenance costs of the memory set. Here are some recording accuracy to choose from (or, of course, outside this range):

Word length precision: each record is accurate to one machine word length (that is, the number of addressing bits of the processor, such as the common 32-bit or 64-bit, which determines the pointer length of the machine to access the physical memory address), which contains intergenerational pointers.

Object precision: each record is accurate to an object in which fields contain intergenerational pointers.

Card precision: each record is accurate to a memory area where objects contain intergenerational pointers.

Above, the third kind of "card precision" refers to the implementation of memory sets in a way called "Card Table", which is also the most commonly used form of memory set implementation.

What does a card watch have to do with a memory set?

In the previous introduction to the memory set, it was mentioned that the memory set is actually an "abstract" data structure, which means that it only defines the behavioral intention of the memory set, but does not define the concrete implementation of its behavior. Card table is a concrete implementation of memory set, which defines the recording accuracy of memory set and the mapping relationship between memory set and heap memory. The relationship between memory set and card table can be understood by analogy according to the relationship between Map and HashMap in Java (that is, the relationship between interface and implementation class).

Let's talk about the specific implementation card table of the memory set in detail.

Card watch

The card table is implemented using a byte array CARD_TABLE []. Each element corresponds to a specific size memory block in the area of memory it identifies. Each memory block is called a card page. The card page used by hotspot is 2 ^ 9 or 512 bytes. As shown in the following figure

In this way, we can divide an area according to the card page, and if we want to garbage collect the Cenozoic area now, we can think of the old area as a card page, as shown in the following figure.

As shown in the figure, because there is an intergenerational reference to the new generation in cardpage1, the first position of the corresponding card table is 1, indicating that there are objects for intergenerational applications in this page area.

Card table angle: because there are intergenerational drinking objects in page1, the first position of the card table is marked as 1, indicating that the element page1 has become dirty.

Memory recovery angle: because the first position of the card table is 1, it indicates that there are intergenerational objects in the page area, and this area needs to be scanned during garbage collection.

The memory of a card page usually contains more than one object. As long as there is an intergenerational pointer in the field of one (or more) object in the card page, the value of the array element corresponding to the card table is identified as 1, which is called Dirty, or 0 if not. When garbage collection occurs, as long as you filter out the dirty elements in the card table, you can easily find out which card page memory blocks contain intergenerational pointers and add them to the GC Roots to scan together. This eliminates the need to scan the entire old age and greatly reduces the scanning range of GC Roots.

The above is about the content of this article on "how to understand the garbage collection of Java". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow 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

Development

Wechat

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

12
Report