Java Object creation Process:
the creation of the object begins :
Virtual opportunity to the New keyword, first go to the constant pool to find out if there is no symbolic reference to this class, and check whether the referenced class has been loaded, parsed, and initialized, if not, the class will first execute the loading process, after passing the check, the virtual machine allocates memory for the new object.
-
Allocate memory :
There are two ways in which the object is allocated:
One allocation method is "pointer collision", when the memory is normalized, the memory used on one side, unused memory on one side, the middle is the indicator pointer, The memory allocation at this time is to move the indicator pointer to an unused area to a distance equal to the size of the object created.
Another allocation method is the "Idle list", when the memory is not structured, the virtual machine must be in a discontinuous memory space to find a suitable size of the memory area of the object, and use a list to maintain each region created, and update the records on the list.
Select the allocation method that is determined by whether the heap memory is structured or not, and whether the GC that is used has a compression collation function.
when faced with concurrency, it is possible that a virtual machine allocates memory to object A before the pointer is changed. At this time, there are also B objects that use pointers to allocate memory to solve this problem in two ways:
Initialize Object memory space :
After the memory allocation is complete, the virtual machine initializes the memory space for the object to a value of 0 (in addition to the object header), and if Tlab is used, the work can be done in advance to Tlab allocation. Initializing the 0 value This step is why the object was created and can be used.
Object Settings :
The virtual machine sets the object, such as the object is an instance of that class, the object's hash value, the GC band age, and so on, which all exist in the object's head. This is followed by the Execute <init> method, to the end of this class creation.
memory layout for Java objects :
Objects in memory are divided into three blocks of area, object header, instance data, and aligned padding.
Java Object Header part of the two parts: part is used to save the object itself run-time data, such as: hash code, GC band age, lock status flag, thread holding lock, bias thread ID, biased time stamp , etc.
The other part is a pointer to the type, which points to the class metadata, which the virtual machine uses to determine which instance of the class it belongs to, to find the object's metadata information, and not necessarily to go through the object itself.
The instance data section is a valid piece of information that is actually stored by the object, as well as the field content of the type defined in the code, whether the parent class or the subclass needs to be logged.
Alignment padding does not necessarily exist, it is just a placeholder, the HotSpot VM's automatic memory management system requires that the object's starting address must be an integer multiple of 8 bytes, and if not, it needs to be complete by aligning the padding.
Object Access Location:
One is access through the handle, reference is stored in the handle address, this method first need to partition a chunk of memory in the Java heap as a handle pool, the advantage is that when the object pointer changes such as: The object is moved, this time the reference itself does not need to change.
The other is direct pointer access, which reference directly to the class object address in the Java heap. Object access is very frequent in Java virtual machines, so the first approach creates a cost of overhead.
Java Virtual machine Learning (ii) Java object creation and access positioning