Object access is ubiquitous in the Java language and is the most common program behavior, but even the simplest accesses involve the association between the three most important memory areas of the Java stack, the Java heap, and the method area , as in the following code:
ObjectnewObject();
We know that in the Java stack is a reference to the object, in the Java heap is the specific new object entity, depending on the specific type and the virtual machine implementation of the object memory layout (objects), the length of this block of memory is not fixed.
Also, in the Java heap, you must include address information that can find data for this object type, such as Object types, parent classes, implemented interfaces, methods, and so on, which are stored in the method area.
Since the Java stack is a reference to the object, then how we use the object there are two main ways to access it: using a handle and a direct pointer .
(1) using a handle:
If you use handle access, a block of memory will be partitioned into the Java heap as a handle pool, where the handle address of the object is stored, and the handle contains the specific address information of the object instance data and the type data, reference.
(2) Direct pointer
If you use direct pointer access, the layout of the Java heap object must consider how to place information about the access type data, the object address is stored directly in the reference,
There are advantages to the way these two objects are accessed, and the greatest benefit of using handle access is that reference stores a stable handle address that changes only the instance data pointer in the handle when the object is moved (which is a very common behavior when the object is garbage collected), and reference itself does not need to be modified.
The biggest benefit of using direct pointer access is faster speed , which saves time spent on pointer positioning, because object access is very frequent in Java, so this overhead is a significant execution cost.
Java Virtual Machine Value object access and how to use the object reference (2)