Java lock and object header, java object

Source: Internet
Author: User

Java lock and object header, java object

I. Object Header

In a hot spot virtual machine, the layout of objects stored in memory can be divided into three areas: object Header, Instance Data, and Padding ).

The Object Header of the HotSpot Virtual Machine includes two parts. The first part is used to store the data of the Object's own runtime, such as the HashCode) GC generational age, lock status mark, lock held by the thread, biased thread ID, biased timestamp, etc, the Data Length of this part is 32 Bits and 64 Bits in 32-bit and 64-bit virtual machines (for the scenario where compression pointer is not considered, it is officially called "Mark Word ".

The number of runtime data that an object needs to store exceeds the record limit of the 32-bit and 64-Bit Bitmap structures, however, the object header information is an external storage cost unrelated to the data defined by the object. Considering the space efficiency of virtual machines, mark Word is designed as a non-fixed data structure to store as much information as possible in a very small space. It will reuse its storage space according to the object state. For example, if an object in a 32-bit hot spot virtual machine is not locked, 25Bits in the 32 Bits space of Mark Word is used to store the HashCode ), 4 bits is used to store object generations, 2 bits is used to store the lock flag, 1 bit is fixed to 0, in other states (lightweight locking, heavyweight locking, GC marking, bias) the following table lists the storage content of objects.

Another part of the object header is the type pointer, which is the pointer to the metadata of the object's class. The VM uses this pointer to determine the instance of the class of the object. Not all Virtual Machine implementations must retain type pointers on object data. In other words, searching for object metadata does not have to go through the object itself. In addition, if the object is a Java array, a piece of data must be included in the object header to record the length of the array, the virtual machine can determine the size of a Java object through the metadata information of a common Java object, but the size of an array cannot be determined from the metadata of an array.

 

Which of the following is important?Lock flag,The lock flag corresponds to the biased lock to the unique lock status.

Therefore, the lock status is stored in the object header, so we can understand it again.

These two questions are easy to understand!

Ii. Lock status

There are four types of locks in total: unlocked, biased, lightweight, and heavyweight locks. As the locks compete, the locks can be upgraded from biased locks to lightweight locks, and then upgraded to heavyweight locks (but the locks are upgraded in one way, that is, they can only be upgraded from low to high, locks are not degraded ). In JDK 1.6, the biased lock and lightweight lock are enabled by default. We can also disable the biased lock through-XX:-UseBiasedLocking.

 

1. Locking process of lightweight locks

(1) When the Code enters the synchronization block, if the lock status of the synchronization object is unlocked (the lock flag is in the "01" status, whether the lock is biased towards "0 "), the Virtual Machine will first create a space named Lock Record in the stack frame of the current thread to store copies of the current Mark Word of the Lock Object, officially referred to as Displaced Mark Word. At this time, the state of the thread stack and object header is 2.1.

(2) copy the Mark Word in the object header to the lock record.

(3) After the copy is successful, the virtual machine uses the CAS operation to update the Mark Word of the object to the pointer to Lock Record, and points the owner pointer in the Lock record to the object mark word. If the update is successful, perform step (4); otherwise, perform step (5 ).

(4) If the update operation succeeds, the thread will have the lock of the object, and the lock flag of the object Mark Word is set to "00 ", this indicates that the object is in the lightweight Lock State. In this case, the State of the thread stack and object header is 2.2.

(5) If the update operation fails, the VM first checks whether the Mark Word of the object points to the stack frame of the current thread. If yes, it indicates that the current thread already has the Lock of this object, then you can directly go to the synchronization block to continue execution. Otherwise, it indicates that multiple threads compete for the lock. The lightweight lock will be expanded to a heavyweight lock, and the status value of the lock Mark will change to "10". The Mark Word stores the heavyweight lock (mutex) the thread waiting for the lock will also enter the blocking status. The current thread tries to use spin to obtain the lock. Spin is used to obtain the lock in a loop to prevent thread congestion.

 

Figure 2.1 Status of the stack and object before the lightweight CAS Lock Operation

 

Figure 2.2 Status of the stack and object after the lightweight CAS Lock Operation

Iii. Biased lock

The introduction of biased locks aims to minimize unnecessary lightweight lock execution paths without multi-thread competition, because the acquisition and release of lightweight locks depend on CAS atomic commands for multiple times, the biased lock only needs to rely on the CAS atomic command once when the ThreadID is replaced (because the biased lock must be revoked once the multi-thread competition occurs, therefore, the performance loss of the lock-biased Undo operation must be less than the performance consumption of the saved CAS atomic commands ). As mentioned above, the lightweight lock aims to improve the performance when threads execute synchronization blocks alternately, while the biased lock aims to improve the performance step by step when only one thread executes synchronization blocks.

1. Biased lock acquisition process:

(1) Whether to set the logo of the biased lock to 1 and the lock flag to 01 in the access Mark Word -- confirmed to be biased.

(2) If the status is biased, test whether the thread ID points to the current thread. If yes, go to step (5); otherwise, go to step (3 ).

(3) If the thread ID does not point to the current thread, the CAS operation will compete for the lock. If the competition succeeds, set the thread ID in Mark Word to the current thread ID, and then execute (5). If the competition fails, execute (4 ).

(4) If the CAS fails to obtain the biased lock, it indicates competition. When a thread arrives at the global security point (safepoint), the thread that obtains the biased lock is suspended, the biased lock is upgraded to a lightweight lock, and the thread that is blocked at the security point continues to execute the synchronization code.

(5) execute the synchronization code.

2. Release of biased locks:

The revocation of biased locks is mentioned in Step 4 above. The biased lock will only release the lock when other threads attempt to compete in favor of the lock. The thread will not actively release the biased lock. To cancel a biased lock, wait for the global security point (no bytecode is being executed at this time point). It will first suspend the thread with a biased lock and determine whether the Lock Object is locked, after the offset lock is removed, it is restored to the unlocked status (the flag is "01") or lightweight lock (the flag is "00.

3. Conversion between heavyweight locks, lightweight locks, and biased locks

This figure mainly summarizes the above content. If you have a good understanding of the above content, it should be easy to understand.

 

References:

Http://blog.csdn.net/u010723709/article/details/50341631

Http://www.cnblogs.com/paddix/p/5405678.html

Http://www.cnblogs.com/lingepeiyong/archive/2012/10/30/2745973.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.