1. In Java, the allocation of objects is generally using the new keyword.
When the virtual opportunity comes to the new directive, it first checks to see if the parameter contained in the directive can find a symbol reference in the constant pool, and checks whether the class represented by the symbol reference is loaded, parsed, and initialized.
If not, the class's loading process is performed first.
2. After the class load is complete, the object is allocated memory space. How much memory space an object needs is determined after the class is loaded.
There are two ways to allocate memory, depending on whether the memory space is structured to determine.
If the memory space is regular, as long as the idle pointer to the free memory direction, this method is called "pointer collision";
If the memory space is not structured, a "free list" is required to record which memory is available, allocate enough memory from available memory, and modify the "Idle list";
A pointer collision is used when using algorithms such as serial and parnew, and idle lists are used when using algorithms such as Mark-sweep.
3. How to ensure that the action of allocating memory is thread-safe in multi-threaded environment.
One method is to synchronize the action of allocating memory;
Another method is to use "Local thread allocation cache (Tlab)", each thread will be pre-allocated to a piece of memory, the request memory will be on the respective Tlab, only if Tlab is not enough, it is necessary to synchronize the allocation of new Tlab;
4. After allocating memory, the virtual opportunity initializes the space except the object header to 0. That is why global variables in Java code can be used without initialization.
In addition, the virtual machine also makes the necessary settings for the object's header, such as the class to which the object belongs, the hash code of the object, and the age of the object's GC generational.
5. The above process is complete or, the virtual machine thinks that an object has been created, but from the point of view of the program, the object has not been initialized, so the individual variables need to be initialized according to the code.
Once this step is complete, a usable object is created.
JAVA Object Assignment Process