Previous article
(1) The data region of the JVM runtime
In this article, we’ll look at the object creation process. Note that this article should be helpful for you to write high performance code.
Object is really a word that lifts the spirits of single people, especially in object-oriented programming, where you can do as much new as you like and do as much new as you like.
Okay, seriously, object creation is one of the things that we do a lot in programming, so what does object creation go through? In this article we will only describe what happens when we encounter a new instruction.
Let’s take this code as an example
Public class TestNew {public static void main(String[] args) {new MyEntity(1,"a"); public static void main(String[] args) {new MyEntity(1,"a"); }}Copy the code
MyEntity.java
@setter@getter // Just two attributes with empty and no arguments construct public class MyEntity {private Integer ID; private String name; public MyEntity(Integer id) { this.id = id; } public MyEntity(Integer id, String name) { this.id = id; this.name = name; }}Copy the code
Javap -v testnew.class javap -v testnew.class javap -v testnew.class javap -v testnew.class javap -v testnew.class
Here is the bytecode for the main methodI pasted it down for the sake of explanation:
// The only thing you need to focus on is my 5-12. Just follow the rest. // The only thing you need to focus on is my 5-12. Public static void main(java.lang.String[]); public static void main(java.lang.String[]); // 1. Descriptor describes methods whose arguments are arrays of string, and finally the capital V represents a return value descriptor of void: ([Ljava/lang/String;)V // 2. Method descriptor, the following two values indicate a public static method flags: ACC_PUBLIC, ACC_STATIC // 3. Code indicates the Code part of the method Code: Stack =4, locals=1, args_size=1 Execute the default initialization logic (there is no member attribute assignment) 0: new # 2 / / class com/example/demo/asmtest MyEntity / / 6. Pressing an object reference the operand stack stack top 3: Iconst_1 // 8. Execute integer. valueof on 1, which is an int 5 because id is an Integer: invokestatic #3 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; // add the string "a" from the constant pool to the top of the stack. LDC #4 // String a // 10. Call MyEntity constructor. invokespecial #5 // Method com/example/demo/asmtest/MyEntity."<init>":(Ljava/lang/Integer;Ljava/lang/String; )V // 11. The object created in the previous step is not received with a variable, so it is directly popped with the pop command 13: pop // 12. LineNumberTable: line 8: 0 line 9: LocalVariableTable LocalVariableTable LocalVariableTable LocalVariableTable LocalVariableTable Start Length Slot Name Signature 0 15 0 args [Ljava/lang/String; Name Flags argsCopy the code
Let’s talk about it bit by bit. A lot of things have been done here in sentence 5.
-
First check to see if the argument to the new directive can locate a symbolic reference to a class in the constant pool, and check whether the symbolic reference represents a class that has been loaded, validated, prepared, parsed, and initialized. If not, it indicates that the class has not been loaded by the vm and cannot be created. You need to perform the loading process of the class (in this case, the class is MyEntity) first.
-
After class loading checks, the JVM allocates memory for newly generated objects in the Java heap in a “pointer collision” or “free list” manner, the size of which is fully determined after class loading (since we are using a CMS garbage collector, we do so in a free list manner).
-
After memory allocation is complete, the JVM needs to initialize the allocated memory space with zero values (excluding object headers). This step ensures that the instance fields of the object can be used in Java code without initial values, and the program can access the zero values corresponding to the data types of these fields. (Instance variables can be used by programs without being initialized, as opposed to local variables that must be initialized.)
-
The object is then set up as necessary: which instance of the class the object is, how to find the metadata information of the class, the hash code of the object, the GC generation age of the object, and so on, which is stored in the object header information of the object.
Now that the new directive is done, it’s time to populate the object instance. That is, it’s time to assign values to member attributes. But the values of member attributes also require preparation. Didn’t I already give the value in New MyEntity (1, “a”)? That’s because the 1 you gave is actually an int, but the id member property is actually an Integer, so you have to box it, so you have 8 here, and then the “A” is in the string constant pool, and you need to load the reference from the constant pool onto the top of the stack, which is 9, and that’s all set up here, The constructor of the MyEntity class was called in 10 to initialize the member variable. 10 What is done here can be summarized as follows:
5. Start to execute the method to initialize the object, initialize the object according to the wishes of the program ape, until a finished object is finished loading
I mentioned 12345 as the whole process of object creation, you can see the details of bytecode, the next article on object creation process memory allocation.
If you don’t understand any questions, please ask me in the comment section. I am not impatient, but busy. I will give you a serious explanation when I see the questions you ask.