Part 3: Layout of objects in memory

1. The composition of the object layout
1) Object header
Including hash, GC generation age, lock status flags, locks held by threads, etc.
The other part is the type pointer, that is, the pointer that the object points to its class metadata. The virtual machine uses this pointer to determine which class the class is.
2) Instance data
Stores the real valid data of the object.
3) Align padding
The size of the object must be a multiple of 8, and the object header is exactly a multiple of 8, so when the instance data is not aligned, there is alignment padding to complete.
2. Access positioning of objects
1. Access method
Object access method: handle, direct pointer
2. Access objects by handle
The java heap will divide a piece of memory as a handle pool
The reference stores the handle address of the object, and the handle contains the specific address information of the instance data and data type of the object.

3. Direct pointer access
Stored in reference is the address of the object.

3. Advantages and disadvantages of the two access methods
Handle access mode Since the address is stored in the referenece, when the java object is changed, moved, garbage collected, etc., only the reference address in the handle needs to be changed.
The advantage of the direct pointer method is that it is fast. The pointer stores the address of the object, which saves the time for the first pointer positioning and search. Because the access to the object is very frequent in Java, the time saved is added as the program runs. quite impressive.
The object access method of HotSpot is mainly the second, direct pointer method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324776839&siteId=291194637
Recommended