After introducing the jvm runtime data area before, let’s explain other details of the data in the memory to see how they are created, laid out and accessed
1. Objects Creation
1. Allocation of objects
Type:
Pointer collision and free list
Pointer collision
: Assuming that the heap memory is absolutely regular, then , when allocating memory space for a new object, you only need to move the pointer to the free space direction of the new object by a section of the required size.
Usually when using a collector with compact (compacting) process, pointer collision is used
Free list
:
If the memory is not regular, then you need to maintain a list to record which memory is free. When allocating space, find a piece of from the list that is large enough and divide it into Object instance and update list record
When using a collector based on the mark-sweep algorithm, use
free list
2. Initialization and setting of objects
Initialization: After the memory allocation is completed, the object is initialized, and the virtual machine will initialize the memory space are all initialized to a value of 0,
## This is why the instance field of the object does not need to be initialized in the java code and can also be used
SET
: After that, the virtual machine performs operations on the object Some necessary settings are mainly to store the metadata information of the class, the hash value of the object, the generational age, etc. in the object header.init
: After the above two steps, the object creation is completed, 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 intentionAfter completing the above three steps, an object is created.
2. Memory layout of objectsThe layout of objects stored in memory is mainly divided into three parts. Object Header, instance data, alignment padding
Object header
:Class metadata information, hash value, generation age, etc.
The object is mainly divided into two parts: runtime data and type pointerRuntime data
: It mainly stores the hash code, generation information, lock status identification, locks held by threads, etc. stored in the object setting stage above.Type pointer
: A pointer that mainly stores the class metadata of the object, that is, which class the object is an instance of. In addition, if the object type is an array,Object header A data representing the data length will also be stored in it
Alignment padding
: It does not necessarily exist. In Hotspot, the starting address of the object must be a multiple of 8 bytes,When the object instance part is not aligned, use alignment padding to complete it.
3. Object access positioningThe Java program operates the object instance through the reference data on the stack, so it is just a reference , the specific positioning method jvm has different implementations
There are two mainstream implementation methods: using handles and direct pointersPrinciple: An area will be set aside in the Java heap as a handle pool, and reference stores the handle address, which contains pointers to object instance data and object type pointers
Advantages: If the object is moved, you only need to change the address stored in the handle pool
Direct pointerPrinciple: If you are using a direct pointer, then reference What is stored in is the address of the object
Advantages: It saves the time of positioning and is more efficient
Hotspot uses the direct pointer method
The above is the detailed content of JVM advanced features-2. JVM allocation, layout, and access process of objects in the heap. For more information, please follow other related articles on the PHP Chinese website!