Java object memory layout

1, object header (Header)

  • The object itself runtime data (mark word): hash code, GC generational age, lock state identification, whether biased locking thread holding the lock, missed the thread ID, timestamp bias;
  • Type pointers: a pointer to the object metadata.

mark word:

Independent data storage costs additional object header information of the object itself is defined, taking into account the space efficiency of the virtual machine, the information object header is designed as a non-fixed data structures to store as much information in a very small space, it according to the state of the object reuse their own storage space.

 The following diagram describes the meaning in the virtual machine 32, different state of the object mark word bit interval:

Lock status 25bit 4bit 1bit 2bit
23bit 2bit Whether bias Lock flag
No lock state hashCode object Generational Age 0 01
Lightweight lock Pointing the stack pointer record lock 00
Heavyweight lock Mutex pointer (heavyweight lock) of 10
GC mark air 11
Biased locking Thread ID Epoch Generational Age 1 01

 Find two ways of object metadata:

  • Handle manner: pile maintains a pool handle, the handle comprising a pointer type data objects and data pointer (reference stack pool handle to the handle, the handle to the instance of the heap, the handle pointing to the data type area method);
  • Direct pointers: stack pointer directly includes type data (heap stack reference point to the object, the object type of the data points in the area method).

Find the difference between metadata in two ways:

  • Handle manner: stable reference stack, a need for additional open area (cell stack handles), the object moves, to change its address handler in Example data;
  • Direct pointers: a pointer is positioned overhead savings, faster.

2, instance data (Instance Data)

Various types of field contents (parent class + subclass).

3, alignment padding (the Padding)

Placeholder (no other effects), the virtual machine management system requires the object memory start position must be a multiple of 8, the data portion is not aligned instance, is filled.

 

supplement:

The illustrations in the text, can be derived from many interview questions, such as:

1, GC generational age of Why default is 15?

2, the state of the object lock There are several types?

3, why would 1bit to mark whether bias?

......

Guess you like

Origin www.cnblogs.com/miaooooo/p/12179534.html