In-depth understanding of java virtual machine---object access positioning (10)

Cite other people's articles: https://www.cnblogs.com/YYfish/p/6722258.html

 So how do you access objects?

  Java programs operate specific objects on the heap through reference data on the stack.

 


First let's review the virtual machine stack:

 

The virtual machine stack is the memory model of java method execution: each method will create a stack frame (Stack Frame) to store the local variable table , operand stack, dynamic link, method exit and other information when it is executed.

The local variable table stores various basic data types known at compile time (long, boolean, int, byte, short, long, double, float), returnAddress type (pointing to the address of a bytecode instruction) and

Object reference (reference type, it does not represent the object itself, it may be a reference pointer to the starting address of the object, or it may point to a handle represented by an object or other locations related to this object)

 


That's right, the object instance in the heap memory is accessed or operated through the object reference stored in the local variable table in the stack frame!

A simple understanding is that there is a pointer to an object reference in the stack frame, which points to the object instance in the heap memory through various methods .

Among these methods, there are two main ones:

1. Handle

A piece of memory is divided into a handle pool in the java heap, the handle address of the object is stored in the reference, and the handle contains the specific address information of the object instance data and the type data. As shown in Figure 2-2:

 

 

 

2. Direct access to objects

The reference directly points to the object type data, then the java heap object distribution must consider how to place the relevant information of the access type data,

The immediate object address stored by reference. Figure: 2-3

 

 

The access methods of these two objects have their own advantages. The biggest advantage of using handle access is that the stable handle address is stored in the reference. When the object is moved, only the instance data pointer in the handle needs to be modified, and the reference itself does not need to be modified. !

The advantage of directly accessing the object is that it reduces the time overhead of a pointer positioning. Since the access to the object is very frequent, this kind of overhead adds up to a very high execution cost.

Guess you like

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