Java object creation

java object creation process

  • The jvm encounters the new instruction and goes back to check whether the parameters of this instruction can locate a symbolic reference of a class in the constant pool, and check whether the class represented by this symbolic reference has been loaded, resolved and initialized. If not, the class loading process is performed.
  • Allocate space for an object, and divide a fixed size of memory from the Java heap. If the Java heap is neat, and the memory that has been used is placed on one side, and the memory that has not been used is placed on the other side, so that there is an intermediate pointer to separate the two areas, just move the pointer to the corresponding position, this kind of The method is called pointer collision. If used memory is interleaved with unused memory, a free list method is needed to decide where to allocate.
  • When multiple threads are concurrent, it will appear that memory is being allocated to object A, and the pointer has not been modified in a hurry, and object B uses this pointer to allocate memory. This is a problem. There are two solutions:
    • Synchronize actions on allocated memory space
    • The memory allocation action is divided into different spaces according to the thread.
  • After the memory is allocated, the jvm initializes all the allocated memory to zero. Next, set the object header, including the instance of which class the object is, how to find the original data information of the class, the hash code of the object, the GC generation age of the object and other information.
  • Execute init to initialize.

Object's memory layout

  • Object header: Part of it is to store the runtime data of the object itself, such as hash code, GC generation age, etc. The other part is the type pointer, that is, the pointer that the object points to the original data of the class.
  • instance data
  • Align padding

Five cases must be initialized

  • When encountering the four bytecodes of new, getstatic, putstatic, and invokestatic, if the class has not been initialized, its initialization needs to be triggered first.
  • When using the java.lang.reflect package method to make a reflection call to a class
  • Initialize a class, if its parent class has not been initialized, trigger the initialization of the parent class first
  • When the virtual machine starts, the user needs to specify an execution main class, and the virtual machine initializes this class first
  • When using JDK1.7 dynamic language support, the final parsing result of a java.lang.invoke.MethodHandle instance is the method handle of REF_getstatic, REF_pustatic, REF_invokeStatic, and the class corresponding to this method handle has not been initialized, it needs to be triggered first its initialization.

During the loading phase, the jvm needs to complete the following

  • Get the binary byte stream defining this class by its fully qualified name.
  • Convert the static storage structure represented by the binary byte stream that defines the class to the runtime data structure of the method area
  • A java.lang.Class object representing the class is generated in the java heap as the access entry for the data in the method area.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560689&siteId=291194637