Explain the process of java object creation

Preface: How did java objects change from code to a memory space? Just looking at the code level, we just used the new keyword and called the constructor to generate an object, and then we can use this object, then how does the virtual machine implement this process in this, here we are Learn this process together.

Scenario hypothesis

  1. Display of java code and bytecode information
    Assuming the following code, Human is a class, and we want to create an object of this class for use. Let’s discuss this code below:

    pubic class TestCreateObject{
          
          
    	public void test(){
          
          
    		Human human = new Human();
    	}
    }
    

    The class bytecode information corresponding to the test method above is shown in the figure below (the test party publishes information).
    Insert picture description here
    We can clearly see the virtual machine instructions corresponding to this line of code. Among these bytecode instructions, the instructions generated by the new keyword are new. Instruction, invokespecial instruction. The other commands dup, aload, and astore are irrelevant.

  2. The process of loading the virtual machine class
    Recall the process of loading the next class by the virtual machine, as shown in the figure below. This process is frequently used when the object is created, so it is listed first for viewing at any time.
    Insert picture description here

Object creation process

  1. When the virtual machine encounters the new instruction, it will check whether there is a symbol reference of the Human class in the runtime constant pool, and find the class file of this class according to the symbol quotes.
  2. Load the Class file corresponding to Human into the virtual machine. This loading is the loading in the flowchart above, and a class object is created at the same time. For use in reflection scenarios, the Class object (java.lang.Class) of this class is not used here.
  3. When the human class file is loaded into the virtual machine, the virtual machine verifies the information of the class file. This is the "verification" phase, such as verifying whether the magic number is 0xCAFEBABE, verifying the major and minor version numbers, verifying metadata, and verifying Bytecode instructions, verify the existence of symbol references, etc.
  4. After passing the above verification, the virtual machine assigns initial values ​​to the class variables. This is the "preparation" stage. This stage is for assigning initial values ​​to class-level information. If there are no class variables in Human, this stage is not involved.
  5. Then the virtual machine starts to translate the symbol references in this class into direct references. This is the "resolving" phase. It is worth noting that the resolution phase does not necessarily occur here, but may also occur in the "initialization" in the figure above. "After that, we can just treat it as happening here to make it easier to understand. After the symbolic reference is converted to a direct reference, it can be used directly when in use.
  6. Up to now, we have not fully loaded the class. At this stage, we need to assign values ​​to class-level variables. This is the "initialization" phase. There are no class-level variables or code blocks in Human, so this block is also saved. Note that the "preparation" phase just mentioned is to assign the initial value, this phase is to assign the value set in the program, it is not the same thing.
  7. Up to now, the type information of Human has been completely loaded into the memory, and here is the action of the new instruction. The previous series are triggered by the new instruction, which is not what new really does. The virtual machine is opened in the heap. A piece of memory is used to store Human objects. There are two schemes for the allocation of objects. If they are directly allocated in the new generation, the allocation of objects generally uses the "pointer collision" method. If they are directly allocated in the old generation, it may be possible. Using "pointer collision" may also use "free list", which mainly depends on whether the allocated area has the ability to organize space. In addition, the virtual machine will also adopt a security strategy when assigning objects. Generally, CAS + failure retry is used to ensure thread safety. Of course, TLAB (local thread allocation buffer) is also used to ensure thread safety. This is a method for each thread. A way of pre-allocating a small piece of memory. Up to now, an object without any data has been generated in the virtual machine. An object is divided into three parts in the heap: the object header, the instance data area, and its supplement.
  8. After the virtual machine allocates the memory, it will initialize all the information except the object header to 0. Therefore, in our daily development, the instance variable can be used normally without initialization, but the local variable is not instantiated. Can not be used.
  9. Next, the virtual machine needs to set the necessary information for the Human object. The first is the object's header information, such as the setting of the GC generation age, the object's hash code, etc., and another comparison is stored in the object's header. Important information "type pointer", the pointer points to Human metadata.
  10. After all the above operations are completed, in fact, an object that does not contain any data is generated, because its instance data area is the default value of 0. We said at the beginning that the new keyword becomes after being compiled Two bytecode instructions, one is the new instruction and the other is the invokespecial instruction. The above processes are all completed by the new instruction. Now that the work of the new instruction is finished, it will definitely be the turn of the invokespecial instruction. This instruction is one of the five method call instructions in the virtual machine, which is used to call the constructor, private methods, and parent methods. It is natural to call Human's construction method here.
    Insert picture description here
    As can be seen from the red part above, the parameter of this instruction is Human's constructor method. After this part runs, it will assign values ​​to the instance data area in the object. Such a complete object is established in the heap, and then the virtual machine gives the address of the Human object to the variable human. This completes the whole process.

Guess you like

Origin blog.csdn.net/m0_46897923/article/details/113760181