How to Implement object access in JVM (detailed description)
The previous article introduced the JVM runtime data zone.
Next, let's discuss how objects in JVM are allocated and accessed? This process will involve three important memory areas, namely the Virtual Machine stack, heap, and method zone, mentioned in the previous article.
For example, the following code is as follows:
ClassA object = new ClassA ();
If the code appears in a method, the "ClassA object" part will be reflected in the local variable table of the Java Virtual Machine stack, it stores reference data in the VM stack. The "new ClassA ()" part will be reflected in the Java heap memory to form a structured memory that stores the data values of all instances of the ClassA type. The length of this memory is not fixed, it varies according to the specific type and the Object Memory layout implemented by virtual machines. In addition, the Java heap must contain the address information that can be used to find data of this object type (such as object type, parent class, implemented interface, and method, these types of data are stored in the method area.
In the Java Virtual Machine specification, there are no rules on how to access specific objects through reference type reference. Currently, there are two mainstream implementation methods:
1. access through a handle. A memory block will be allocated in the Java heap as the handle pool. The reference stores the handle address of the object,
The handle contains the specific address information of the object instance and type data, as shown in:
2. for direct pointer access, you must consider how to place information about type data in the access method area in the layout of Java heap objects,
The address of the object instance is directly stored in reference, as shown in
Summary:
In both cases, the object access method has its own advantages. The biggest advantage of using the handle access method is that the reference stores a stable handle address.
The biggest advantage of using direct pointers is faster.
- Previous detailed description of memory regions of Java Virtual Machine
-
Top
-
1
-
Step on