An approximate procedure for creating an object:
Virtual opportunity to a new directive, first checks whether the parameter of this directive can be positioned in a constant pool to a symbol reference of a class, and checks whether the class that the symbol reference represents has been loaded, parsed, and initialized. If not, you must first perform the appropriate loading procedure.
After the class load check passes, the new object is then allocated memory. The size of the memory required for an object is fully determined after the class is loaded. The task of allocating space to an object is equivalent to dividing a certain size of memory from the Java heap. The task of allocating space to an object is equivalent to dividing a certain size of memory from the Java heap. Allocation method: Pointer collision, idle list. The choice of which allocation method is determined by whether the Java heap is structured or not, and whether the Java heap is structured or not, is determined by whether the garbage collector in use has a compression collation function. Another concern is whether object creation is a very frequent behavior in a virtual machine, even if it is simply modifying the location pointed to by a pointer, which is not thread-safe in the concurrency scenario. There are two solutions: 1, the allocation of memory space to synchronize the action 2, the memory allocation of the action according to the thread divided in different space.
After the memory allocation is complete, the virtual machine needs to initialize the allocated memory space to 0 values (excluding object headers)
Next, the virtual confidential object to make the necessary settings, such as the object of which class instance, how to find the class metadata information, the object's hash code, the object's GC generational age and other information. This information is placed in the object header of the object.
Now, for a virtual machine, the new object has been created, but for Java programs, the object is just beginning because the Init method has not been executed yet.
Memory layout of the object
In a hotspot virtual machine, the layout of objects stored in memory can be divided into 3 areas: Object Header (header), instance (Instance data), Object Fill (Padding).
The object header consists of two parts: 1, which is used to store the runtime data of the object itself: such as hash code, GC generational age, lock status flag, thread holding lock, bias thread ID, bias timestamp. 2, the storage type pointer, that is, the object pointer to its class metadata, the virtual machine through this pointer to determine which object is the class instance. "3, if the object is a Java array, then there must be a piece of data in the object header to record the length of the array, because the virtual machine can determine the size of the Java object through the metadata information of the ordinary Java object, but the size of the array cannot be determined from the metadata of the array"
Creation of objects for Java virtual machines and memory layout