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 process of initializing and creating classes in JVM

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What is the process of class initialization and creation in JVM? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

State change of class

The initialization of the class mainly goes through loading-> linking (verification, preparation, parsing)-> initializing these phases, and the corresponding state in JVM is shown in the following figure.

InstanceKlass.hpp

Allocated: assigned but not linked

Loaded: loaded and inserted into the JVM internal class hierarchy, but not yet linked

Linked: linked, but not initialized

Being_initialized: initializing

Fully_initialized: complete initialization

Initialization_error: an error occurred during initialization

Load

The .class file is a binary file, we can click on the .class file, we can see a variety of binary information, the character converted to the right is not very complete, there are a lot of identification bits, directly represented by numbers. What you can see on the right is basically the information in the constant pool string.

The source code for loading .class is in classFileParser.cpp, as shown in the following figure:

In the figure above, we can see that there is the definition of CAFEBABE and the definition of version number. Further down, we can see the parsing methods such as constant pool and attachment table in class files, so we will not repeat them here.

Link

As we can see in out/build or other output directories, class files are separate, and class files contain various static constant pools used by this class. There is also a runtime pool in jvm that can be accessed by each class. Because the most important thing of linking is to associate the static constant pool and the runtime constant pool in the class file, and convert the static symbol reference into a direct memory reference, and then we can call the corresponding method through the address to complete the operation.

There are three big steps for links: verification, preparation, and parsing.

Verify whether the binary information of the class or interface is correct, the access control of the method, whether the variable is initialized, etc. Generally speaking, as long as we write the code ide does not report an error, basically there is no problem, but some will construct their own .class files and let jvm run, so we have to verify all kinds of correctness

Preparation: in the preparation phase of the class, the class static variables will be allocated memory space and initial values will be assigned, but note that no assigned code or static code blocks have been executed at this time!

Parsing: as mentioned above, associate the static variable pool in the class file with the running pool within the jvm, and replace the symbolic reference with a direct reference

The location of the source code is shown in the following figure:

Clinit method

The clinit method is the key to initialization

This method, which we have not seen in the java source code, can only be automatically generated and named by the javac compiler and then automatically inserted into the Class file.

The clinit method collects class variables (static non-final), static code blocks, by the compiler

The clinit method does not have any virtual machine bytecode instructions to call, it can only be implicitly called by the virtual machine during the type initialization phase, and only once in the whole process.

If there is inheritance, the parent class will be initialized first

The source code is as follows:

As shown in the figure above, there are several steps, and the notes for each step are very clear. It is strongly recommended that friends pull down the source code and read it.

In fact, the parent class takes precedence over the subclass initialization, as you can see in steps 7 and 8, as shown in the following figure:

Creation of instance object

The creation of the instance object is relatively simple. When the virtual opportunity arrives at the new, the index of the target object in the constant pool is obtained from the top of the stack, and then the type of the target type is located. Next, the virtual machine sees whether it has been loaded and uses tlabs/ slow allocation (Eden) to find an empty space, and then initializes the instance data and the object header.

The process is the above process, in fact, it is not complicated, just like we bought something, saw the picture (klass) on JD.com, and then bought one back (with its own example). If the express delivery is very fast and you haven't had time to figure out where to put it (before loading this class), throw it to the warehouse (Eden area) first, if you have already figured out how to put it (this class has been loaded). Then it's easy to arrange it (using TLABS to assign it).

One of the things to pay attention to is that. As soon as I chose where to put it, I began to update it in my notebook, and I put the XXX at the XXX address. Even if you haven't gone over and put things down yet, you can use that address to answer when others ask.

The source entry is shown in the following picture. Interested partners can follow along this entrance and follow them deeply. After the knife, we will see these places again. Come on

This is the answer to the question about the initialization and new process of classes in JVM. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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