Access location of objects in Java

Days do not see, very miss. Today we continue to study the access location of objects in Java, mainly involving the Java stack and some definitions in the heap, combined with the picture we explore together.

  1. Java uses reference data in the stack to manipulate concrete objects in the heap

Java doesn’t specify how reference data should be referenced, so there are two common ways to access reference: handles and direct Pointers

  1. The handle way

Access objects through handles

When the object in the heap is accessed using a handle, a memory area is allocated in the Java heap. Reference stores the address of the handle, and the handle contains the address information of the instance data and the type data of the object respectively.

  1. Direct pointer mode

Objects are accessed through direct Pointers

When using direct Pointers to objects in the heap, the Java heap needs to consider how to place instance data of the object, and reference stores the object’s address directly. If you are only accessing objects, you do not need the overhead of an additional indirect access.

4. Compare the pros and cons

Both methods have their advantages and disadvantages. When the object is accessed using the handle method, the stable handle address is stored in reference. When the object is moved, only the pointer of the instance object in the handle needs to be changed (object address change is very common in garbage collection).

The biggest advantage of using direct Pointers to access objects is that they are faster. Accessing objects in Java is very frequent and can add up to a lot of overhead, which is what HotSpot uses.

The content of this article is not much, but the things involved are relatively bottom, share it with everyone, and move forward together!