Access positioning of Java objects

Objects are created to use objects, and our Java programs need to manipulate specific objects on the heap through the reference data on the stack. Since the reference type only specifies a reference to an object in the Java virtual machine specification, and does not define how the reference should locate and access the specific location of the object in the heap, the object access method also depends on the virtual machine implementation. Depends. The current mainstream access methods are the use of handles and direct pointers.
1. Handle access method:
If you use handle access, then a piece of memory will be divided in the Java heap as a handle pool, the handle address of the object is stored in the reference, and the handle contains the object instance data and type data (class information, method type information) specific address information, as shown in the following figure.

access by handle 
2. Pointer access method: 
If direct pointer access is used, then the layout of the Java heap object must consider how to place the relevant information of the access type data, and the object address stored in the reference is directly, as shown in the following figure.
pointer access 

These two object access methods have their own advantages: 
the biggest advantage of using handles to access is that the reference stores a stable handle address, and only the handle is changed when the object is moved (moving objects is a very common behavior during garbage collection). Instance data pointer in the reference, and the reference itself does not need to be modified.
The biggest advantage of using the direct pointer access method is that it is faster. It saves the time overhead of a pointer positioning. Since object access is very frequent in Java, this kind of overhead is also a very considerable execution cost. .
For Oracle's HotSpot virtual machine, it uses the second way to access objects, but from the perspective of the entire software development, it is also very common for various languages ​​and frameworks to use handles to access.

 Taken from "In-depth Understanding of Java Virtual Machine Second Edition" 2.3.3 Object Access Positioning

Guess you like

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