This is the first day of my August challenge
The most common operation in Java is to new an object. But how does the JVM run during the new object process? Using the HotSpot VIRTUAL machine as an example, this article briefly explores the entire process of the new object.
Take hotspot, the most common virtual machine, and Java heap, the most common memory region:
1. Object creation
When the Java virtual machine hits a bytecode new instruction –> whether to locate a symbolic reference to a class in the constant pool –> the virtual machine allocates memory for the new object (if the memory in the heap is absolutely clean, Pointers collide). Otherwise, the free list.
Pointer collision:
Free list:
2. Memory layout of objects
The layout of an object in heap memory can be divided into three parts: object Header, Instance Data, and object Padding.
The object header contains two pieces of information:
Mark Word:
Type pointer:
Instance object:
3. Object access positioning
Object access depends on the implementation of the virtual machine. When pointing to the heap in the stack, there are two main access methods: handle and direct pointer.
Handle access:
Pointer access:
Comparison: The biggest advantage of handle access is that reference stores the stable handle address. When the object is moved, only the instance data pointer in the handle needs to be changed, and reference itself does not need to be modified. Fast pointer access