"Five minutes a day, JVM Fun": The object memory layout

Overview

Depending on circumstances of an object may be divided into two cases, when the object is an array of non-object when the object header, instance data, alignment padding third of the world in memory, and the array of multiple objects in the object header with a It describes an array of objects to a portion of the length

image-20190823221858767

Object header

Object header is divided into two parts, a first part called ** "Mark Word" , is the second portion of the object type for acquiring type ** pointer, if the object is an array further comprises a data record length of the array.

In different operating systems, the different memory regions occupied in the 32-bit system, the space occupied by the 32bit MarkWord (i.e. 4 bytes). Type pointers and arrays length data as the collaboration space 32bit.

In the case of 64-bit operating system, the space occupied by MarkWord 64bit, the type of pointer compression is not turned pointer (CompressedOOPs) is a 64bit (8 byte), in the case of open pointer compression, remaining 32bit (4 byte)

image-20190823225703812

Mark Word

This part is stored in the object itself runtime data , the data structure of content which is not fixed together, it will reuse their own storage space according to the state of the object,

image-20190823230559543

This fragment was extracted from markOop.hpp file, which represents the state of the object of the following five:

Flag Biased locking flag status
01 0 no lock
01 1 Biased locking
00 no Lightweight lock
10 no Heavyweight lock
11 no GC Mark

Then we are going to see the structure of MarkWord:

image-20190823231212050

Here we can see that the initialization of only defines non-biased locking lock and state structures (the upper half is not open COOPs- pointer compressed structure, the lower half is opened pointer to compression structure),

When the lock is lightweight, heavyweight lock object pointer recorded, according to the instructions of the JVM, this time that the pointer 64 still, the lowest two bits assumed to be 0; when in the biased locking, record biased locking obtain the thread pointer which is 64;

image-20190823231522289

More content we will not expand here, and based on feedback, I will open concurrent programming in a single evolutionary path to talk a lock in the back.

Type pointer

This thing is sometimes used to determine if the object belongs to which instance of the class , it also uses less time, this should be carried out to locate the object to achieve selection algorithm based on different virtual machines (such as HotSpot JVM on the use of this type of pointer to acquire the object type data)

Examples of data

Examples of real data is valid the object information stored in the program code but also various types of defined field contents , the field contents herein includes not only the current class field, including his field defined in the parent class.

Storage rules in this section follows the virtual machine allocation strategy parameters and fields define the order in Java source code in, HotSpot JVM the default allocation strategy is long / double, int, short / char, byte / boolean, oops (common object pointer, Ordinary Object pointers) can also be understood as a reference, pointer compression on our next section to say.

It should be noted that variables defined in the parent class appears before the subclass, but we can by CompactFileds parameter set to true, the sub-category variable into a space smaller parent big variable.

image-20190824120847519

Alignment padding

This section is not necessarily present, since the Hot Spot JVM in a predetermined size of the object must be an integer multiple of 8 bytes , a similar function is called in the C / C ++ in the memory alignment , the memory space is divided in accordance with byte , it seems that access to any type of variable can be started from any address in theory, but the reality is often at a particular memory address access when accessing certain types of variables, which requires various types of data according to certain rules They are arranged in space, rather than sequentially one after the discharge, which is aligned.

Memory Alignment follow two rules:

  • Suppose the first member start address is 0, the start address of each member (startPos) its data type must be an integer multiple of the amount of space

  • The final size of the structure must be a multiple of its members (the underlying data type members) in proportion of the size of the largest member.

Here it is not difficult to understand why the size of a predetermined object JVM must be an integer multiple of 8 bytes, because in 64-bit system (pointer compression does not open), there are many occupying 8 byte data type objects. But there are also some of the 4 byte data type, then we Padding to play a role, to supplement dissatisfied 8 byte part, to cobble together an integer multiple of 8.

image-20190824121114907

No public

Guess you like

Origin juejin.im/post/5d76ef776fb9a06b0f2409ee