Java programs operate on objects on the heap at runtime by reference to the local variable table in the JVM stack. However, reference is only a reference to an object in the JVM specification. There are no restrictions on how the reference locates the object. Therefore, different virtual machines may have different implementations

Access using a handle

You need to create an area of memory in the heap called the handle pool,

Each record in the handle contains a pointer to object instance data and a pointer to object type data

Reference stores the memory address of the handle to the object.

When accessing an object, we first need to find the handle of the object through reference, and then find the object according to the memory address of the object in the handle.

The advantages and disadvantages

Advantages: Reference stores a stable handle address, which only changes the instance data pointer in the handle when the object is moved (a very common behavior during garbage collection). The handle memory address of reference stores does not need to change

Disadvantages: Increased overhead of a pointer location

Using Pointers

Reference stores the memory address of the object instance in the heap directly, but the corresponding object instance data must store the access address of the object type data

Advantages: Saves the overhead of a pointer location. Disadvantages: Reference itself needs to be modified when the object is moved (such as memory rearrangement after GC)

In addition, direct Pointers are currently the way most JVM virtual machines, including HotSpot, are implemented