Yige

Yige

Build

JVM Series - Exploring HotSpot Virtual Machine Objects

JVM Series - Exploring HotSpot Virtual Machine Objects#

Content adapted from: Exploring HotSpot Virtual Machine Objects

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 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 part consists of 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 part is exactly a multiple of 8 bytes (1 or 2 times), so when the instance data part of the object is not aligned, alignment 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 the class loading is complete. Next, a block of memory of the corresponding size 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 organized (indicating the use of the "copying algorithm" or "mark-sweep method"), a pointer is placed between free memory and used memory as a boundary indicator. In this case, memory allocation only requires moving the pointer 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 organized, 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. During allocation, 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 preferentially allocated in the Eden area
  • Large objects go directly to the old generation, avoiding efficiency loss due to copying caused by allocation guarantees when allocating memory for large objects (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, the member variables of the object are assigned initial values, 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 the memory situation of different programs, the virtual machine does not always require the object age to reach a certain value to enter the old generation. If the total size of all objects of the same age in the Survivor space is greater than half of the Survivor space, objects with age greater than or equal to that age can directly enter the old generation without reaching the required age.

Object Access Methods#

All object storage spaces are allocated in the heap, but the references to these objects are allocated in the stack. In other words, when creating an object, 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. Therefore, depending on the type of address where the reference is stored, objects have different access methods.

Handle Access Method#

A memory space called the "handle pool" is needed in the heap, which contains specific address information for both object instance data and type data.

The variable of reference type stores the address of the object's handle (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 reference type directly stores the address of the object, thus eliminating the need for a handle pool. The object can be accessed directly through the reference. However, the memory space where the object resides requires additional strategies 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, which is the direct pointer method to access objects, requiring only one addressing operation, making it twice as fast as the handle access method in terms of performance. However, as mentioned above, it requires additional strategies 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.