Java object creation process


提示:以下是本篇文章正文内容,下面案例可供参考

1. The process of creating Java objects?

insert image description here

  1. Class loading check
    When the virtual machine encounters a new instruction, it will first go to the constant pool according to the parameters of the instruction to see if it can locate the symbol reference of this class, and check whether the symbol reference represented by this class has been loaded and parsed and initialized. If not, the corresponding class loading process must first be performed.

  2. Allocate memory
    When the class loading check is passed, the virtual machine will allocate memory for the new object. The memory size required by the object can be determined after the class is loaded. The task of allocating space for the object is equivalent to moving a piece of memory The Java heap is divided into two allocation methods: "pointer collision" and "free list". Which allocation method to choose is determined by whether the Java heap is regular, and whether the Java heap is regular is determined by whether the garbage collector has compression It is determined by the function of finishing.
    Two types of memory allocation:

    1. Pointer collision
      Applicable occasions: When the heap memory is regular (that is, there is no memory fragmentation)
      Principle: Put the used memory on one side, and put the unused memory on the other side. There is a boundary pointer in the middle, and only the pointer needs to point to the free memory Just move the space of the object memory size in the direction.
      The GC collectors that use this allocation method are: Serial, ParNew
    2. Free list
      Applicable occasions: When the heap memory is irregular
      Principle: The virtual machine maintains a list, which records which memory blocks are available. When allocating, find a large enough memory block to assign to the object instance, and finally update it List records
      The GCs using this allocation method are: CMS

    Memory allocation concurrency issues (supplementary content, need to master)

There is a very important issue when creating objects, which is thread safety, because in the actual development process, creating objects is very frequent. As a virtual machine, it is necessary to ensure that threads are safe. Generally speaking, virtual The machine adopts two methods to ensure thread safety:
CAS + retry on failure: CAS is an implementation of optimistic locking. The so-called optimistic locking is to complete an operation without locking every time but assuming that there is no conflict. If it fails due to conflict, try again until it succeeds. The virtual machine uses CAS with failure retry to ensure the atomicity of the update operation.
TLAB: Pre-allocate a piece of memory in the Eden area for each thread. When the JVM allocates memory to the object in the thread, it first allocates it in TLAB. When the object is larger than the remaining memory in TLAB or the memory in TLAB is exhausted, then use The above CAS performs memory allocation

  1. Initialize zero value
    After the memory allocation is completed, the virtual machine needs to initialize all the allocated memory space to zero value (excluding the object header). This step ensures that the instance fields of the object can be used directly in the Java code without assigning an initial value. , the program can access the zero values ​​corresponding to these data fields.

  2. Set the object header
    After the initialization of the zero value is completed, the virtual machine needs to perform necessary settings for the object, such as which class instance the object is, how to find the original data information of the class, the hash code of the object, and the GC partition age of the object. This information is stored in the object header. In addition, the virtual machine has different settings depending on the running status of the virtual machine, whether to enable bias lock, etc.

  3. Executing the init method
    After the above steps are completed, from the perspective of the virtual machine, a new object has been generated, but from the perspective of the Java program, the object creation has just started, and the init method has not been executed yet. All fields are still 0, so in general, the init method is executed immediately after the execution of the new instruction, and the object is initialized according to the programmer's wishes, so that a truly usable object can be completely generated

Guess you like

Origin blog.csdn.net/CXgeng/article/details/124204274