jvm mystery objects

Previous jvm blog we learned jvm memory model, this blog entry we explore how objects are created , the memory layout object , the object is how to access to .

Creation of objects

The first step: class loading inspection

When a new opportunity to the virtual instruction, first to check whether the command parameters in the constant pool ( JDK1.7 and later versions of the JVM runtime constant pool has been shifted out of the zone method, the Java heap (Heap ) opens up a storage area for runtime constant pool if) to locate the symbol reference to this class, and checks the class represented by the reference symbol has been loaded before, parsing and initialized. If not, then you must perform the appropriate class loading process.

Step two: allocate memory

In the class loader checks after the adoption of the following virtual machine for newborn objects allocate memory . Object required memory size can be determined after the class is loaded, the space allocated for the task object is equivalent to determine the size of a piece of memory from the Java heap carved out.

Distribution has  "pointer collision"  and  "free list"  are two, choose the kind of distribution of the Java heap decide whether structured, and whether regular Java heap and used by the garbage collector with or without compression decided to organize functions .

Pointer collision : the used memory integrated to the side of the memory not used to store the other side, the middle of a cutoff value of pointer memory only needs to have not used the direction of movement of the pointer to the object memory size. Memory apply to regular conditions.

Free list : a virtual opportunity to maintain a list that is recorded in that memory is available, the redistribution of time to find a large enough memory allocated to the object instance, last updated list of records for memory irregular situation.

important point:

When creating an object there is a very important issue, that is thread-safe, because in the actual development process, creating objects is very frequent thing, as a virtual machine, the thread must ensure it is safe, generally speaking, virtual machine uses two ways to ensure thread safety:

  • CAS + failure retry:  CAS is an implementation of optimistic locking. The so-called optimistic locking is that each unlocked assumes that there is no conflict away to complete an action, if it fails because of conflicts retry until it succeeds. CAS coupled by way of a virtual machine failure retry guarantee atomic update operation.
  • TLAB:  for each thread in Eden pre-allocated memory area together, while the JVM thread to allocate an object is first allocated Tlab, when the subject is greater than or TLAB TLAB remaining memory in memory is exhausted, then using CAS aforementioned memory allocation

The third step: initialization value of zero

After the memory allocation is completed, the virtual machine needs to allocate memory space is initialized to a value of zero (not including the object header), this step ensures that an object instance field in Java code may not be assigned an initial value is used directly, the program can access to the data type of zero values ​​corresponding to these fields.

The fourth step: setting object header

After zero value initialization is complete, the virtual object is confidential for the necessary settings , for example, this object is an instance of that class, how to find information such as metadata, hash code of the object, GC target sub-generation information age. This information is stored in the object header. 

In addition, depending on the current operating status of the virtual machine, such as whether to enable biased lock, head objects have different set up.

Step five: Perform init () method

After completion of the above work, from the perspective of the virtual machine's point of view, a new object has been created, but from the perspective of the Java programming point of view, object creation has just begun, <init> the method has not been performed, all the fields are still zero. So, in general, after the execution of new instructions would then execute  <init>the method, the object to be initialized in accordance with the wishes of the programmer, such an object is actually considered completely available emerge.

 

Memory layout objects

In the Hotspot virtual machine, object layout in memory can be divided into three regions: the object header , instance data , and alignment padding .

Hotspot virtual machine object header includes two pieces of information , a first portion for storing the object's own runtime data itself (the hash code, the GC generational age, the lock status flags, etc.),

The other part is the type of pointer , i.e. a pointer to its class object is metadata, the virtual machine is determined by the pointer of this object is an instance of that class.

Examples of valid data portion is stored in the real target information , various types of field contents is defined in the program.

Alignment padding part is not bound to exist, there is no special meaning, occupying only play the role.  Because the automatic memory management system of claim Hotspot virtual machine start address of an object must be an integer multiple of 8 bytes, in other words 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-fold or 2-fold), and therefore, when the object instance data portion is not aligned, it is necessary to completion by aligning padding.

 

How is access to the objects

It is to create an object using the object, our Java program to manipulate objects on the heap by reference specific data on the stack. Object access method may be implemented by a virtual machine, the current mainstream access methods are ① use the handle and ② direct pointer in two ways:

1. Handle

If the handle, then the Java heap will be divided into a memory cell as a handle, Reference handler address is stored in the object, and the handle contains the specific address information of each data object instances and types of transactions;

2. 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 the address is the direct object reference stored.

Compared:

Access efficiency analysis: direct pointer superior side handle, because the direct pointer pointer is positioned only once, saving time cost, which is also used by HotSpot implementation.

Garbage Collection Analysis : When the garbage handle the recovery of moving objects, the address reference stored in the address is stable, no modification is only necessary to modify the address of the object handle; direct pointer to modify the address reference stored in the garbage collector.

Guess you like

Origin www.cnblogs.com/woxbwo/p/11570141.html