JVM object creation process (two)

**

Object creation

**

When the Java virtual machine encounters a bytecode new instruction, it first checks whether the parameter of this instruction can locate a symbol reference of a class in the constant pool , and checks whether the class represented by this symbol reference has been loaded, parsed and initialized . If not, you must first perform the corresponding class loading process.

After the class loading process passes, the virtual machine will allocate memory for the new object. The memory required by the object can be completely determined after the class is loaded. Allocating space for the object is equivalent to dividing a certain size of memory from the Java heap.

Assuming that the memory in the Java heap is absolutely regular, all the used memory is stored on one side, and the free memory is stored on the other side. A pointer is placed in the middle as an indicator of the demarcation point. Allocating memory is equivalent to the movement of the pointer. This allocation method is **"pointer collision"**.

If the memory in the Java heap is not regular, and the used memory is intertwined with the free memory, it is impossible to simply use pointer collisions. At this time, the virtual machine needs to maintain a list to record which memory is available and when the allocation is Find a large enough space to allocate to the object instance, and update the record on the table. This allocation method is called **"Free List"**.

The choice of allocation method is determined by the virtual machine, and whether the Java heap is regular is determined by whether the garbage collector used has the ability to compress and sort.

In the case of concurrency, it is not safe to allocate memory through pointers. For example, if memory is allocated for A, the pointer has not been modified in the future, and B has used the original pointer to allocate memory.
Solution:
1 Synchronize the action of allocating memory space. In fact, the virtual machine adopts CSA with failure retry to ensure the atomicity of the update operation.
2 The action of memory allocation is allocated in different spaces according to threads, for each Each thread allocates independent memory, called **"local thread allocation buffer"**, that is, in the buffer allocation, only when the local buffer is used up, the synchronization lock is required when a new buffer area is allocated.

After the memory allocation is completed, the virtual machine initializes the allocated memory to zero (excluding the object header), which ensures that the instance field of the object can be used without initial value. Next, the virtual machine needs to make necessary settings for the object, such as which instance of the object is, the hash code of the object, and so on. This information will be placed in the object header of the object.
The () method must be executed after the new instruction.

Symbol reference: It exists in the method area of ​​the virtual machine. Because the specific location of some classes and class fields and methods is not known at compile time, symbol references will be used, and then specific analysis will be performed during loading (note the symbol The reference must be able to specifically find the content that needs to be quoted).
CAS: CAS is the abbreviation of Compare And Swap (Compare And Swap), which implements Java's atomic operations like locks. Three basic operands are used in the CAS mechanism: the memory address V, the old expected value A (a copy of V), and the new value B to be modified after calculation.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109205636