Tip: To use and locate Java objects, use the Java Virtual Machine Stack, which describes the thread-memory model for Java method execution: When each method is executed, the Java VIRTUAL machine synchronously creates a Stack Frame to store information about local variables, operand stacks, dynamic connections, method exits, and so on. The process of each method being called and executed corresponds to the process of a stack frame moving from the virtual machine stack to the virtual machine stack.

Code reading

Take the following code as an example to illustrate the process of object positioning:

class Bus extends Car {
    private String code;
    private String color;
    Bus(String code, String color) {
        this.code = code;
        this.color = color;
    }
    // omit other methods...
}
public class ReferenceTest {
    Bus myBus = new Bus("Java Chinese Community"."Blue");
}
Copy the code

In the official default HotSpot virtual machine, myBus is a variable of the reference type stored in the local variable table, new Bus(“Java Chinese community “, “blue “) is the object instance data stored in the Java heap, which stores all the field information of the entity class. For example, code=”Java Chinese community “and color=” blue” information, and the Java heap also stores the address of the object type data, it stores the object type information, as well as its parent class information.

conclusion

The Java Virtual Machine Specification only defines the reference type as a reference to an object, and does not define how the reference should locate and access the object in the heap. Therefore, the object access method is also determined by the virtual machine. The main access methods are handle and direct pointer:

  • If handle access is used, the Java heap may be divided into a block of memory as a handle pool. Reference stores the handle address of the object, and the handle contains the specific address information of the instance data and type data of the object.
  • If direct pointer access is used, the memory layout of objects in the Java heap must consider how to place the information related to the access type data. The direct stored in reference is the address of the object. If only the object itself is accessed, there is no need for the overhead of an additional indirect access.

Therefore, the biggest benefit of using handles for access is that reference stores a stable handle address and only changes the instance data pointer in the handle when the object is moved (which is a very common behavior in garbage collection). Reference itself does not need to be modified. Direct pointer access is faster, but the reference itself needs to be modified if the object is moved.

Because object access is so frequent in Java, this overhead can add up to a significant execution cost, so the official default HotSpot VIRTUAL machine uses “direct Pointers” to locate objects.

Reference & acknowledgements

Chou zhiming. Understanding the Java Virtual Machine in depth. 3rd edition

Follow the public account “Java Chinese community” reply “dry goods”, obtain 50 original dry goods Top list.