As we all know, Java is an object-oriented language, so what does an object contain in memory? First of all,Most objects are stored on the heap (except escape).
Then the object storage in the heap is mainly divided into three parts
Object header, object instance data, alignment supplement (the array will have one more Array length)
Object header:
mark word:Stores the hashCode and lock information of the object ( Lock upgrade) or information such as generation age or GC flag
Type pointer:Stores a pointer to the class to which the object belongs (class filein metadata), the JVM passes This determines which class this object belongs to
Object instance data:
The object information generated by new stores the attribute data information of the class, including the parent Class attribute information;
Alignment supplement
Array objects will be filled with multiple alignments
JVM requires object occupation The space must be a multiple of 8 to facilitate memory allocation (allocated in bytes as the minimum unit), so this part is used to fill up insufficient space to make up the number.
The mainstream access methods mainly includehandleanddirect Pointer
Handle:
A piece of memory is divided into the Java heap as a handle pool, and the object stored in the reference in the stack The handle address, and the handle contains the specific address information of the object instance data and type data
direct pointer:
java heap The memory layout of the object must consider how to prevent access to relevant information of the type data. What is stored in the reference is directly the object address.
Direct pointer access to the object does not require an additional indirect access overhead, and the handle is convenient. When the object address changes (garbage collection will move the object address), you only need to change the pointer reference itself in the handle. No need to change.
When the virtual machine encounters the new instruction, it first checks whether the specified class isloaded and verified , Prepare(Allocate memory space for all static variables in the class, and set an initial value for them),Parse and initialize.
After the class check, the virtual machine allocates memory for new objects
How to ensure the safety of heap memory allocated in concurrent situations
The virtual machine uses CAS to configure failure recovery Try to ensure atomicity
Leave memory allocation to threads, allocate space when creating threads, and leave the task of allocating memory to threads. Open through TLAB (Thread local Allocation Buffer)
After allocating the memory, set the object header, such as which class instance, hashcode, class metadata information pointer (method area)
Execute the init method (kernel method), initialize member variables, execute the instantiation code block, call the construction method of the class, and assign the first address of the object in the heap to the reference variable.
CAS adds failure retries to ensure the atomicity of updates.
Divide the memory allocation into different spaces according to threads, that is, each thread pre-allocates a small block of memory in the Java heap, which is called the local thread allocation buffer TLAB. Which thread wants to allocate memory? Allocate in the corresponding TLAB, and synchronize after the TLAB is used up.
Static members and static initialization blocks of the parent class are executed in the order they appear in the code
Subclass static members and static initialization blocks are executed in the order they appear in the code.
Parent class instance members and instance initialization blocks are executed in the order they appear in the code. Execute
parent class construction method
subclass instance members and instance initialization blocks in the order they appear in the code. Execute the
subclass construction method in sequence
The above is the detailed content of What is Java object storage memory layout method. For more information, please follow other related articles on the PHP Chinese website!