Note: This blog, the main reference from the "in-depth understanding of Java Virtual Machine (second edition)"
1. The layout of objects stored in memory is divided into three blocks
- Object Header
- Store the run-time data for the object itself:Mark Word(32bit and 64bit on 32bit and 64bit virtual machines), with the following information:
- Object Hashcode
- Object GC generational age
- Lock status Flag (lightweight lock, heavyweight lock)
- Thread-held locks (lightweight, heavyweight locks)
- Bias lock Related: Biased lock, spin lock, lightweight lock and other lock optimization strategies are JDK1.6 added, these optimizations make synchronized performance and Reentrantlock performance flat, in synchronized can meet the requirements of the case, Prefer to use synchronized, unless you are using features that are unique to reentrantlock, such as specifying time waits, and so on.
- Type pointer: object pointer to class metadata (32bit-->32bit,64bit-->64bit (uncompressed pointer not turned on), 32bit (with compression pointer on))
- Instance data: Valid information that the object is actually stored
- Snap To fill
- The JVM requires that the size of the object must be an integer multiple of 8, if not, to align the complement
2. Attention
- Mark Word has a non-fixed data structure to store as much information as possible in a very small amount of space
- If the object is an array, the object header must have data together to record the length of the array. The JVM can determine the object length through the metadata of the Java object, but not for the array.
- For object Header length
- 32bit virtual machine must be 32bit+32bit, that is, 8 bytes
- 64bit virtual machine If the compression pointer is not turned on, is 64bit+64bit, that is, 16 bytes, if the compression pointer is turned on, is 64bit+32bit, that is, 12 bytes (not a multiple of 8bit)
- -xx:+usecompressedoops: Turn on the compression pointer
- In the "Deep Understanding Java Virtual Machine (second edition)", said the object header is 8 bytes or 16 bytes, do not know whether it is wrong, their system is not 64bit, no test
- Basic data types and selection of corresponding packaging classes
In practice, we select the base data type or the wrapper class based on the party with the smaller number of bytes.
- In the second chapter of the JVM memory allocation, said that the actual object of the memory size after the completion of the class loading can be known, specifically how to calculate, you can see this article http://www.importnew.com/14948.html
Java Object Memory Layout attached