Yige

Yige

Build

JVM Series - Exploring HotSpot Virtual Machine Objects

JVM Series - Exploring Objects in HotSpot Virtual Machine#

Content adapted from: Exploring Objects in HotSpot Virtual Machine

Memory Layout of Objects#

In the HotSpot virtual machine, the memory layout of objects is divided into the following 3 areas:

  • Object Header
  • Instance Data
  • Alignment Padding
    image.png

Object Header#

The object header records some data needed during the object's runtime:
image

  • Hash Code
  • GC Generation Age
  • Lock State Flag
  • Thread Holding the Lock
  • Biased Thread ID
  • Biased Timestamp

The object header may contain a type pointer, which can determine which class the object belongs to. If the object is an array, the object header will also include the array length.

Instance Data#

The instance data section contains the values of member variables, including both parent class member variables and current class member variables.

Alignment Padding#

Used to ensure that the total length of the object is a multiple of 8 bytes.

The automatic memory management system of the HotSpot VM requires that the size of an object must be a multiple of 8 bytes. The object header is exactly a multiple of 8 bytes (1 or 2 times), so when the instance data part of the object is not aligned, padding is needed to complete it.

Alignment padding is not necessarily present and has no special meaning; it merely serves as a placeholder.

Object Creation Process#

Class Loading Check#

When the virtual machine parses a .class file and encounters a new instruction, it first checks whether there is a symbolic reference for this class in the constant pool and whether the class represented by this symbolic reference has been loaded, resolved, and initialized. If not, the corresponding class loading process must be executed first.

Allocating Memory for New Objects#

The size of memory required for the object can be fully determined after class loading is completed, and then a corresponding block of memory is allocated from the heap for the new object. There are two ways to allocate memory in the heap:

Pointer Collision#

If the memory in the Java heap is absolutely regular (indicating the use of the "copying algorithm" or "mark-compact method"), and there is a pointer as a boundary indicator between free memory and used memory, then when allocating memory, the pointer can simply be moved towards the free memory by a distance equal to the size of the object. This allocation method is called "pointer collision."

Free List#

If the memory in the Java heap is not regular, with used memory and free memory interleaved (indicating the use of the mark-sweep method with fragmentation), pointer collision cannot be simply performed. The VM must maintain a list that records which memory blocks are free and available. When allocating, a sufficiently large memory space is found from the free list to allocate to the object instance. This method is called the "free list."

Allocation Strategy#

  • Objects are prioritized to be allocated in the Eden area
  • Large objects go directly to the old generation to avoid efficiency loss due to copying caused by the allocation guarantee mechanism (large objects: objects that require a large contiguous memory space, such as strings and arrays)
  • Long-lived objects will enter the old generation

Initialization#

After memory allocation, initial values are assigned to the member variables of the object, object header information is set, and the object's constructor method is called for initialization. At this point, the entire object creation process is complete.

Object Age Judgment#

If an object is born in Eden and survives the first Minor GC, and can be accommodated by Survivor, it will be moved to the Survivor space, and its age will be set to 1. Each time the object survives a Minor GC in Survivor, its age increases by 1. When its age reaches a certain level (default is 15), it will be promoted to the old generation. The age threshold for an object to be promoted to the old generation can be set by the parameter -XX.

Dynamic Object Age Determination:
To better adapt to different program memory situations, the virtual machine does not always require that an object's age must reach a certain value to enter the old generation. If the total size of all objects of the same age in the Survivor space exceeds half of the Survivor space, objects with an age greater than or equal to that age can directly enter the old generation without needing to meet the required age.

Object Access Methods#

All objects' storage space is allocated in the heap, but the reference to this object is allocated in the stack. In other words, when an object is created, memory is allocated in both places: the memory allocated in the heap actually creates the object, while the memory allocated in the stack is merely a pointer (reference) to this heap object. Depending on the type of address where the reference is stored, there are different access methods for objects.

Handle Access Method#

There needs to be a memory space in the heap called the "handle pool," which contains specific address information for both the object instance data and type data.

The variable of the reference type stores the handle address of the object (reference). When accessing the object, it first needs to find the handle of the object through the reference type variable, and then find the object based on the address in the handle.

image.png

Direct Pointer Access Method#

The variable of the reference type directly stores the address of the object, thus eliminating the need for a handle pool, allowing direct access to the object through the reference. However, the memory space where the object resides requires an additional strategy to store the address of the class information to which the object belongs.
image.png

It should be noted that HotSpot uses the second method, i.e., direct pointer access to access objects, which requires only one addressing operation, making it twice as fast as the handle access method. However, as mentioned above, it requires an additional strategy to store the address of the class information of the object in the method area.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.