JVM note three: HotSpot virtual machine management objects

This paper used to record the knowledge of Java heap allocated objects, access and layout.

Creating objects

Create a general object of the process are summarized below

  • Check if the class is already loaded, perform class loading process is not loaded
  • Allocated for the object memory, if the memory is structured using a pointer collision to allocate, irregular use the free list to find the memory meets the size requirements of
    concurrent memory allocation problem: the operation of memory allocated to synchronize, after optimization, virtual machine using CAS with failure retry mechanisms to guarantee atomicity, if TLAB, you can just run out of time needs to be allocated a new thread TLAB only be synchronized.

    TLAB: to create a thread when a small piece of pre-allocated on the heap memory, called thread-local buffers, you can -XX: whether to configure +/- UseTLAB

  • The allocated memory is set to zero value, if TLAB, it is possible to advance this step in the creation of TLAB
  • The object header information set, while generating the optional monitor object
  • The init method, class initialization information will of programmers in accordance with

Memory layout objects

In HotSpot VM, the object is stored in memory layout may be divided into three areas: object header (Header), instance data (Instance Data) and alignment padding (Padding).

Object header

HotSpot VM object header includes two pieces of information: types and runtime data pointer.

  • Runtime data: for storing runtime data object itself, such as a hash code (HashCode), GC generational age lock state flag thread holds the lock, the thread ID bias, bias timestamp, in accordance with the different lock state different structures.
  • Type pointers: a pointer to the object that is its class metadata, the virtual machine is determined by the pointer which is an instance of the object class. (Not all virtual machine implementations must retain the data type of the pointer on the object, in other words, find the object metadata does not have to go through the object itself, can access the location of the reference object)
    if the object is a Java array, that in the object header must also have an array length of the data recording, because the virtual machine can determine the size of the Java object by ordinary Java objects metadata information, but can not determine the size of the array from the metadata array.

Examples of data

Examples of valid data portion is stored in the real target information, various types of program code in the field contents is defined. Whether it is inherited from the parent class down, or defined in a subclass, you need to be recorded. HotSpot VM default assignment policy longs / doubles, ints, shorts / chars, bytes / booleans, oop, it can be seen from the assignment policy, the same width field is always assigned together.

Alignment padding

HotSpot VM request object start address must be an integer multiple of 8 bytes, that is, the size of the object must be an integer multiple of 8 bytes. The head portion and the object is exactly a multiple of 8 bytes (1 or 2 times), and therefore, when the portion of the object instance data is not aligned, it is necessary to completion by aligning padding.

Object Access positioning

Java program requires to manipulate objects on the heap by specific reference data on the stack. Access methods object depends on virtual machine implementations, there are handles and use of two mainstream direct pointer access.
Handle, can be understood as a pointer to a pointer, the pointer changes to maintain the pointing object, and the object's handle itself does not change; pointer to the object, the object representing the memory address.

  • Handle: Java heap memory partitioned handler address as a pool handle, stores the object references, and the handle contains the specific address information of each data type data object instances.
    Advantage: Reference is stable in storage handler address will only change the instance data pointer in the handle when the object is moved (moving object when garbage collection is a very common behavior), while the reference itself does not need to be modified.
  • Direct pointer: If you use direct access pointer, then the layout of objects in the Java heap must consider the type of data access related information of how to place, and directly referenced object address is stored.
    Advantages: faster, saving time cost of a pointer positioning. Due to access objects in Java very frequently, so after such expenses add up is very impressive execution costs. (Such as HotSpot)

Guess you like

Origin www.cnblogs.com/NotNil/p/11542679.html
Recommended