Table of Contents
So how can we solve cross-generation references?
Memory set
Card table
Home Java javaTutorial Understand Java garbage collection mechanism

Understand Java garbage collection mechanism

Apr 24, 2023 pm 02:10 PM
java

Before talking about memory sets and card tables, let me first introduce to you the issue of cross-generation reference.

Understand Java garbage collection mechanism

If you want to perform a collection (Minor GC) that is limited to the new generation area, but the instance object 1 of the new generation is referenced in the old generation, in order to find To remove all surviving objects in this area (new generation), in addition to the fixed GC Roots, all objects in the entire old generation have to be additionally traversed to ensure the correctness of the reachability analysis results, and vice versa. Although the solution of traversing all objects in the entire old generation is theoretically feasible, it will undoubtedly bring a large performance burden to memory recycling.

In fact, the problem of cross-generation references is not only between the new generation and the old generation, but also all garbage collectors involving partial area collection (Partial GC) behavior, such as G1, ZGC and Shenandoah collection. All devices will face the same problem.

So how can we solve cross-generation references?

First of all, cross-generational citations account for only a very small number compared to same-generation citations. The reason is that objects referenced across generations should tend to survive or die at the same time (for example: if a new generation object has a cross-generation reference, because the old generation object is difficult to die, this reference will allow the new generation object to be collected when it is collected. Survive, and then be promoted to the old generation as it ages, at which time cross-generation references are also eliminated).

Based on what is said above, we no longer need to scan the entire old generation for a small number of cross-generation references, nor do we need to waste space to record whether each object exists and which cross-generation references exist. We only need to scan the entire old generation for a small number of cross-generation references. Create a global data structure (this structure is called a "Remembered Set"). This structure divides the old generation into several small blocks and identifies which piece of memory in the old generation will have cross-generation references. Afterwards, when Minor GC occurs, only objects in small blocks of memory containing cross-generational references will be added to GCRoots for scanning. Although this method needs to maintain the correctness of the recorded data when the object changes its reference relationship (such as assigning itself or a certain attribute), which will increase some runtime overhead, it is still more cost-effective than scanning the entire old generation during collection. of.

Let’s introduce this global data structure memory set.

Memory set

The memory set is an abstract data structure used to record a set of pointers from the non-collection area to the collection area. If we do not consider efficiency and cost, the simplest implementation can use all object arrays containing cross-generation references in the non-collection area to implement this data structure, as shown in the following code:

//以对象指针来实现记忆集的伪代码
Class RememberedSet {
	Object[] set[OBJECT_INTERGENERATIONAL_REFERENCE_SIZE]; 
}
Copy after login

All such records Implementation solutions containing cross-generation reference objects are quite expensive in terms of space usage and maintenance costs. In a garbage collection scenario, the collector only needs to use the memory set to determine whether a certain non-collection area has a pointer pointing to the collection area. It does not need to know all the details of these cross-generation pointers. When designers implement the memory set, they can choose a rougher record granularity to save the storage and maintenance costs of the memory set. The following lists some record precisions for selection (of course, you can also choose outside this range):

  • Word length precision: Each record is accurate to one machine word length (that is, the processor The number of addressing bits, such as the common 32-bit or 64-bit, this precision determines the length of the pointer used by the machine to access the physical memory address), this word contains the cross-generation pointer.

  • Object precision: Each record is accurate to an object, and there are fields in the object that contain cross-generation pointers.

  • Card precision: Each record is accurate to a memory area, and there are objects in this area that contain cross-generation pointers.

Above, the third type of "card precision" refers to using a method called "Card Table" to implement the memory set, which is currently the most Commonly used implementations of memory sets.

What is the relationship between card list and memory set?

When I introduced the memory set earlier, I mentioned that the memory set is actually an "abstract" data structure. Abstraction means that it only defines the behavioral intention of the memory set and does not define the specific implementation of its behavior. The card table is a specific implementation of the memory set, which defines the recording accuracy of the memory set, the mapping relationship with the heap memory, etc. Regarding the relationship between the memory set and the card table, it can be understood by analogy with the relationship between Map and HashMap in Java (that is, the relationship between interfaces and implementation classes).

Let’s talk about the specific implementation of the memory set in detail. Card table

Card table

The card table is implemented using a byte array CARD_TABLE[], each element corresponds to its The identified memory area is a memory block of a specific size. Each memory block is called a card page. The card page used by hotspot is 2^9 in size, which is 512 bytes. As shown in the figure below

Understand Java garbage collection mechanism

In this way we can divide a certain area according to card pages. If we now want to garbage collect the new generation area, then we can The era area is divided into card pages one by one, as shown in the figure below.

Understand Java garbage collection mechanism

As shown in the figure, because there is a cross-generation reference pointing to the new generation in cardpage1, the first position of the corresponding card table is 1, indicating that there is a cross-generation application in this page area object.

  • Card table angle: Because there are cross-generation drinking objects in page1, the first position corresponding to the card table is recorded as 1, indicating that the page1 element is dirty.

  • Memory recycling perspective: Because the first position of the card table is 1, it indicates that there are cross-generation application 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 a cross-generation pointer in the field of one (or more) objects in the card page, the corresponding card table will be The value of the array element is marked as 1, which means the element is dirty (Dirty), if not, it is marked as 0. When garbage collection occurs, as long as the dirty elements in the card table are filtered out, you can easily find out which card page memory blocks contain cross-generation pointers, add them to GC Roots and scan them together. This eliminates the need to scan the entire old generation and greatly reduces the scanning range of GC Roots.

The above is the detailed content of Understand Java garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles