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 load link and initialization of a class in JVM

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you to the JVM class loading links and initialization is how, the content is very detailed, interested friends can refer to, I hope to help you.

profile

With java class files, in order for class files to be transformed into structures that the JVM can actually run, they need to go through a process of loading, linking, and initialization.

How do these three processes work? In this article you will find the answer.

loaded

JVM can be divided into three parts, five spaces and three engines, to talk about it is not particularly complex, first look at the overall JVM architecture diagram below.

From the diagram above, we can see that there are three main parts in the JVM: the class loading system, the runtime data area, and the Execution Engine.

Loading is the process of finding a binary representation of a class or interface based on a specific name and creating classes and interfaces based on that binary representation.

01

run-time constant pool

We know that there is a method area in the JVM, and in JDK8, the implementation of the method area is called metaspace. This meta-space is stored in local memory.

The method area holds a pool of runtime constants for each class.

When a class or interface is created, the runtime constant pool is built from the constant pool defined in the class file.

There are two types in the runtime constant pool: symbolic references and static constants.

Static constants do not require subsequent parsing, while symbolic references require further parsing.

Static constants are divided into two parts: String constants and numeric constants.

String constants are references to String objects, built from the CONSTANT_String_info structure in class.

Numeric constants are constructed from CONSTANT_Integer_info, CONSTANT_Float_info, CONSTANT_Long_info, and CONSTANT_Double_info in the class file.

Symbol references are also constructed from constant_pool in class.

Symbol references to class and interface come from CONSTANT_Class_info.

References to fields in class and interface come from CONSTANT_Fieldref_info.

References to methods in class come from CONSTANT_Methodref_info.

References to methods in interface are from CONSTANT_InterfaceMethodref_info.

The reference to the method handle comes from CONSTANT_MethodHandle_info.

References to method types are from CONSTANT_MethodType_info.

Symbolic references to dynamically calculated constants come from CONSTANT_MethodType_info.

The reference to dynamically computed call site comes from CONSTANT_InvokeDynamic_info.

02

class loader

How are classes created? A class can be created by another class calling its initialization method, or by reflection.

In fact, classes can be divided into two types, one is an array class, and the other is a non-array class.

For non-array classes, because they have a binary representation, they are created by loading the binary representation with the class loader.

For array classes, because they have no external binary representation, array classes are created by the java virtual machine.

Java virtual machine class loader and there are two kinds, one is the virtual machine provides boot class loader, a user-defined class loader.

If it is a user-defined class loader, it should be an implementation of ClassLoader. User-defined class loaders are primarily designed to extend the capabilities of java virtual machines to support dynamic loading and class creation.

link

Chaining is the process of incorporating a class or interface into the runtime state of a java virtual machine so that it can be executed by the java virtual machine.

Link specific tasks include validating and preparing classes or interfaces. Resolving symbolic references in this class or interface is an optional part of the linking process.

If the java virtual machine chooses to resolve symbolic references in classes or interfaces only when they are used, this is called deferred resolution.

If the java virtual machine resolves symbolic references when validating classes, this is called pre-resolution.

01

verification

Validation is primarily about ensuring that the binary representation of classes and interfaces is structurally correct.

If the binary representation of a class or interface does not satisfy the corresponding constraint, a VerifyError exception is thrown.

02

ready

Preparation consists primarily of creating static fields for classes or interfaces and initializing them with default values.

03

parsing

Resolution is the process of dynamically determining the concrete value of a runtime constant based on its symbol reference in the pool.

Executing Java virtual machine instructions:

anewarray,checkcat, getfield, getstatic, instanceof, invokedynamic, invokeinterface, invokespecial, invokestatic, invokevirtual, ldc, ldc_w, multianewarray, new , putfield, and putstatic all point symbol references to the runtime constant pool, thus requiring symbol references to be resolved.

Parsing can be divided into class and interface parsing, field parsing, common method parsing, interface method parsing, method type and method handle parsing, call point qualifier parsing.

initialization

Initializing a class or interface means executing the initialization method of the class or interface.

A class or interface is initialized only in the following cases:

When executing Java VM directives (new,getstatic, putstatic, invokestatic) that require references to classes or interfaces.

When the java.lang.invoke.Methodhandle instance is first invoked.

When calling certain reflection methods in class libraries.

When initializing a subclass of a class.

Is selected as the initial class for java virtual machine startup.

After class files are loaded, linked, and initialized, they are available for use by the JVM at runtime.

About the JVM class loading links and initialization is how to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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

Internet Technology

Wechat

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

12
Report