Java runtime data area-----object memory layout + object access positioning

In HostSpot, the layout of java in memory can be divided into three memory areas, object header, instance data, and alignment padding.

The object header is divided into two parts

       The first part is used to store the runtime data of the object itself, such as hash, GC generation age, lock status flag, biased lock ID, locks held by threads, biased timestamp, this part of data is called Mark Word, but the object The header information is an additional storage cost that has nothing to do with the information defined by the object itself. Considering the space efficiency of the virtual machine, Mark Word is designed as a non-fixed data structure to store as much information as possible in a very small space. Reuse your own storage space according to the state of the object. For example, in a 32-bit virtual machine, if the object is in an unlocked state, 25 bits in the 32 bits of Mark Word are used to store the hash code of the object, 4 bits are used to store the generation age of the object, and 2 bits are used to store Lock flag bit, 1 bit is fixed to 0.

       The second 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 that the object is an instance of that class. If the object is an array, there must be a block in the object header. It is used to record the data of the length of the array, because the virtual machine can determine the size of the java object through the metadata information of the ordinary java object, but if it is an array, the size of the length of the array cannot be determined.

Access positioning of objects: After creating an object, it is necessary to use it, usually through the reference in the virtual machine stack to find it. But how to find it is determined by the JVM. There are currently two mainstream access methods.

1 Using handle memory access, a piece of memory will be divided into a handle pool in the java heap. The handle pool contains pointers to object instance data and pointers to object type data. The java virtual machine stack stores the handle address, and the handle Contains object instance data and class type data.

2 Use direct pointer access, the direct pointer points directly to an area, and the area contains the data of the object type and the pointer to the data of the object type.

Both methods have their pros and cons

Handle access is relatively stable. When the object is moved, only the instance data pointer in the handle is changed, and the reference itself is not modified.

The biggest advantage of direct pointer access is that it is faster and saves the time overhead of a pointer positioning. Since the GC objects move very frequently, this reduction in overhead is still very considerable, so it is better to use this method.


Guess you like

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