This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.
Object memory layout
In the HotSpot VIRTUAL machine, the memory layout of objects is divided into three areas:
- Object Header
- Instance Data
- Align Padding
Object head
The object header records some of the data that the object needs to use during execution:
- Hash code
- GC generational age
- Lock status flag
- The lock held by the thread
- Biased thread ID
- Bias timestamp
The object header may contain a type pointer that determines which class the object belongs to. If the object is an array, the object header also includes the array length.
The instance data
The instance data part is the value of the member variables, including the parent and parent member variables.
Alignment filling
Used to ensure that the total length of the object is an integer multiple of 8 bytes.
HotSpot VM’s automatic memory management system requires that the object’s size be an integer multiple of 8 bytes. The object header is exactly a multiple (1 or 2) of 8 bytes, so when the object instance data part is not aligned, it needs to be filled by alignment.
Alignment padding does not necessarily exist and has no special meaning. It simply serves as a placeholder.
Object creation process
Class loading check
When a virtual machine encounters a new instruction while parsing a.class file, it first checks to see if there is a symbolic reference to the class in the constant pool, and to see if the class represented by the symbolic reference has been loaded, parsed, and initialized. If not, the corresponding class loading process must be performed first.
Allocate memory for newborn objects
The size of the memory required by the object is fully determined after the class is loaded, and a corresponding chunk of memory is allocated from the heap for the new object. There are two ways to allocate memory in the heap:
- Pointer to the collision If the Java heap memory is absolutely neat (use of replication algorithm or tag finishing method), the free memory and has been using memory with a pointer as a cut-off point indicator, so only when allocating memory need to free memory pointer move a paragraph of distance with the object of the same size, this way of distribution is called a pointer.
- Free list If the Memory in the Java heap is disorganized and used memory is interlaced with free memory (indicating mark-purge, fragmentation), then pointer collisions cannot simply be performed, and the VM must maintain a list of which memory blocks are free and available. Find a chunk of memory from the free list that is large enough to allocate to the object instance. This approach is called a free list.
Initialize the
After allocating the memory, assign initial values to the member variables in the object, set the object header information, and call the constructor method of the object for initialization.
At this point, the entire object creation process is complete.
Object access mode
Storage for all objects is allocated in the heap, but references to this object are allocated in the stack. That is, when an object is created, memory is allocated in both places, the memory allocated in the heap actually creates the object, and the memory allocated in the stack is just a pointer (reference) to the heap object. Objects can be accessed differently depending on the type of address the reference is stored in.
Handle access mode
The heap needs to have a memory area called the “handle pool”, which contains the specific address information of the object instance data and the type data.
A variable of a reference type holds the handle address of the object. When accessing an object, you first need to find the handle to the object by referring to the variable of the type, and then find the object based on the address of the object in the handle.
Direct pointer access
Variables of the reference type store the address of the object directly, thereby eliminating the need for a handle pool and providing direct access to the object through reference. However, the memory space where the object resides requires an additional policy to store the address of the class information to which the object belongs.
It should be noted that HotSpot uses the second approach, the direct pointer approach to accessing objects, which requires only one addressing operation and is twice as fast as the handle approach. But as mentioned above, it requires additional policies to store the address of the object’s class information in the method area.