Java Virtual Machine 14: Java object size, Object Memory layout, lock status changes, Java Virtual Machine
How many bytes does an object occupy?
For C/C ++, there is a sizeof function that can be directly obtained, but Java does not seem to have such a method. But fortunately, the Instrumentation class was introduced after JDK1.5, which provides a method for calculating the memory usage of objects. As for how to use the specific Instrumentation class, you can refer to this article to accurately measure the size of java objects.
However, the difference is that this article uses the command line to input JVM parameters to specify the proxy. Here I set JVM parameters through Eclipse:
The following is the specific path of the agent. jar I am using. The rest will not be mentioned. Let's take a look at the test code:
1 public class JVMSizeofTest {2 3 @ Test 4 public void testSize () {5 System. out. println ("Object size:" + JVMSizeof. sizeOf (new Object () + "Byte"); 6 System. out. println ("character a size:" + JVMSizeof. sizeOf ('A') + "Byte"); 7 System. out. println ("integer 1 Size:" + JVMSizeof. sizeOf (new Integer (1) + "Byte"); 8 System. out. println ("Size of the string aaaaa:" + JVMSizeof. sizeOf (new String ("aaaaa") + "Byte"); 9 System. out. println ("char array (Length: 1) Size:" + JVMSizeof. sizeOf (new char [1]) + "Byte"); 10} 11 12}
The running result is:
Object size: size of 16-Byte Character a: size of 16-byte integer 1: size of 16-byte string aaaaa: size of 24-byte char array (Length: 1: 24 bytes
Then, the Code remains unchanged. Add a virtual machine parameter "-XX:-UseCompressedOops" and run the test class again. The running result is:
Object size: size of 16-Byte Character a: size of 24-byte integer 1: size of 24-byte string aaaaa: size of 32-byte char array (Length: 1: 32 bytes
The reason is explained in detail later.
Java object size Calculation Method
JVM has different calculation methods for the size of common objects and array objects. I have drawn a diagram to illustrate the following:
Each part is explained as follows:
The size of the metadata pointer. The metadata pointer is a reference type. Therefore, the metadata pointer of 64-bit machines should be 8 bytes, and the metadata pointer of 32-bit machines should be 4 bytes, however, one of the optimizations in hot spot is to compress and store metadata type pointers using JVM parameters:
- -XX: + UseCompressedOops enable Compression
- -XX:-UseCompressedOops: Disable Compression
Hot Spot uses the former by default, that is, enabling metadata pointer compression. When compression is enabled, the metadata pointer on a 64-bit machine occupies 4 bytes. In other wordsWhen compression is enabled, the reference on the 64-bit machine occupies 4 bytes; otherwise, the reference is normal 8 bytes..
Java object memory size Calculation
With the above theoretical basis, we can analyze the execution results of the JVMSizeofTest class and why the same object size will be different after the "-XX:-UseCompressedOops" parameter is added.
The first is the size of the Object:
Followed by the size of the character 'a:
The following is the size of integer 1:
The size of the String "aaaaa" is followed. All static fields are not required. Only instance fields are concerned. The instance fields in the String object include "char value []" and "int hash ", we can see that:
Finally, the size of the char array with a length of 1 is as follows:
Mark Word
Mark Word is an important part of the Java object header. Mark Word stores the running data of objects, such as HashCode) GC generational age, lock status identifier, lock held by the thread, biased thread ID, biased timestamp, and so on.
However, because the object needs to store a large number of runtime data, it actually exceeds the record limit of the 32-bit and 64-Bit Bitmap structures, however, the object header is an additional storage cost unrelated to the data defined by the object. Considering the space efficiency of the virtual machine, Mark Word is designed asNon-fixed Data StructureIn order to store as much information as possible in a very small space. 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 the object age Division, 2 bits is used to store the lock identification bit, and 1 bit is fixed to 0. The storage content of objects in other states (lightweight locking, heavyweight locking, GC marking, and bias) is shown in:
Here, we should pay special attention to the lock status. In the future, we will study the changes in the lock status and lock status.
Lock upgrade
As shown in, there are four lock statuses: unlocked, biased, lightweight, and heavyweight locks, here, the biased locks and lightweight locks are introduced in JDK1.6 to reduce the performance consumption caused by obtaining and Releasing locks.
The status of the four locks will gradually upgrade with the competition. The Locks can be upgraded but cannot be downgraded, meaning that the biased locks can be upgraded to lightweight locks, but the lightweight locks cannot be downgraded to biased locks, the objective is to improve the efficiency of obtaining and Releasing locks. Use a graph to represent the relationship:
Biased lock
The authors of HotSpot have found that in most cases, the lock is not only free from multi-thread competition, but is always obtained multiple times by the same thread. In order to lower the code for the thread to obtain the lock, a biased lock is introduced. The process of obtaining a biased lock is as follows:
If a lock is obtained, it is released. The lock-oriented release point lies in step (4) above,The lock will be released only when other threads attempt to compete for the lock.The thread will not actively release the biased lock. The release process of biased locks is:
Lightweight lock
The process of locking a lightweight lock is as follows:
Comparison of biased locks, lightweight locks, and heavyweight locks
I will use a table below to compare the biased locks, lightweight locks, and heavyweight locks. I think it is very well written on the Internet. I tried it again to deepen my memory: