JVM advanced features-2. Distribution, layout, access process, and JVM layout of jvm objects in the heap
After introducing the data area of the jvm runtime, I will explain other details about the data in the memory to see how they are created, laid out, and accessed.
I. Object Creation
1. Object allocation
There are two ways to create and allocate objects:Pointer collision and idle list
Pointer collision:
Assuming that the heap memory is absolutely regular, when allocating memory space for the new object, you only need to move the pointer to the idle space.
The size of a part.
Pointer collisions are generally used when a collector with a compact process is used.
Idle list:
If the memory is not regular, you need to maintain a list to record which memories are idle. When allocating space, find a block from the list.
Divide enough space into object instances and update list records
Use the mark-sweep algorithm-based collectorIdle list
2. object initialization and settings
Initialization: After the memory allocation is completed, the object is initialized. The VM initializes the initialized memory space to 0,
This is why the instance field of the object can be used in java code without initialization.
Set: After that, the virtual machine makes some necessary settings for the object, mainly to save the class metadata information to the object header, the object hash value, generational age, and so on.
Init: After the above two steps, the object is created, but all fields are still 0 values. The last step is to execute the init method and initialize the object according to the programmer's meaning.
After completing the preceding three steps, an object is created.
Ii. Object Memory Layout
The layout of objects stored in the memory is divided into three parts: object header, instance data, and alignment filling.
Object Header:
The object header is mentioned in the object setting stage, that is, some class metadata, hash value, and generational age will be stored in the object header during the setting stage.
Objects are mainly divided into two parts: runtime data and type pointer
Runtime data: Stores the hash code, generational information, lock status identifier, and thread lock stored in the above object setup phase.
Type pointer: Mainly stores the class metadata pointer of the object, that is, the instance of the class of the object. In addition, if the object type is array,
The object header also stores a data that represents the Data Length.
Alignment Filling: Not necessarily exists. In hot spot, the starting address of the object must be a multiple of 8 bytes,
When the object instance is not aligned, fill the object with alignment.
3. Access and positioning of Objects
Java programs operate object instances through reference data on the stack, so it is just a reference. The specific Positioning Method of jvm has different implementations.
There are two mainstream implementation methods: handle and direct pointer
Handle pool:
Principle: The java heap uses a region as the handle pool, and the reference stores the handle address,
Contains pointer and object type pointer of object instance data
Benefit: if the object is moved, you only need to change the address stored in the handle pool.
Direct pointer
Principle: If direct pointer is used, the object address is stored in reference.
Advantage: it saves time for positioning and is more efficient.
Hotspot uses the direct pointer method.