2.1 Discovering HotSport VM Objects

Object creation, first let’s take a look at several ways to create objects in Java

  1. Use the new keyword
  2. Use the Class newInstance method
  3. Use the newInstance method in the Constructor class
  4. Using the Clone method
  5. Use deserialization

The main process of creating an object

When the virtual machine reaches a new instruction, it first checks to see if the corresponding class is already loaded in the constant pool. If not, the load of the corresponding class is performed. After the class load passes, memory is allocated.

2.1.1 Allocating Memory for Objects

Once the class is loaded, a block of memory in the Java heap is then allocated to objects. Depending on whether the Java heap is tidy, memory allocation can be done in two ways:

  • Pointer collision: If the memory of the Java heap is tidy, all used memory is placed on one side and free memory on the other. Allocating memory completes by moving the pointer pointer in the center toward free memory an equal distance to the size of the object
  • Free list: If the memory in Java is not tidy, the virtual machine maintains a list of available memory, so that when allocating memory, it can query the list to find enough memory to allocate to objects, and update the list after allocating memory.

The choice of allocation method is determined by the cleanliness of the Java heap, which in turn is determined by the use of a garbage collector with collation capabilities.

2.1.2 Object Access positioning

Java programs need to access objects on the heap via references on the JVM stack. Object access depends on the IMPLEMENTATION of the Jvm virtual machine. At present, there are two main access methods: handle and direct pointer.

Pointer: Points to an object and represents the starting address of an object in memory. Handle: A pointer to a pointer that maintains a pointer to an object. Instead of pointing directly to an object, the handle points to a pointer to the object (the handle does not change, pointing to a fixed memory address), which in turn points to the object's real memory address.Copy the code

Handle access:

A block of memory in the Java heap is used as a handle pool, which stores the address of the handle of the object, and the handle contains the specific address information of the object instance data and the object type data respectively advantageThe reference stores a stable handle address and only changes the instance data pointer in the handle when the object is moved (a very common behavior in garbage collection). The reference itself does not need to be modified.

Direct pointer:

If direct pointer access is used, the direct stored in the reference is the address of the object, then the layout inside the Java heap object must consider how to place information about accessing type data.

Advantages: Faster speed, saving the time cost of a pointer location. Because objects are accessed very frequently in Java, this kind of overhead can add up to significant execution costs. This is the approach used in HotSpot.