The layout of Java objects in memory

Let's look at two questions first:

1 The creation process of the object ?

Object t = new Object();

When the object is first created, it will first check whether the object's class has been loaded. If it has not been loaded, the class loading process will be executed.

class loading

Obtain the binary byte stream of a class through the fully qualified name of a class

It is not specified where and how to obtain it. So the byte stream can be a file stored on the hard disk, it can be a binary byte stream dynamically generated at runtime, it can be generated by other files (the class file corresponding to JSP), etc.

Convert the static storage structure represented by the byte stream into the runtime data structure of the method area

Generate a Class object representing this class in memory as the access entry for this class in the method area

class linking(verification、preparation、resolution)

Verification

​ Verify whether the loaded class file complies with the JVM specification

Preparation

Assign default values ​​to static variables of class files

 Such as:

​ static int i = 8 ;

​ Here, i will be assigned a value of 0, not a value of 8.

Resolution

Resolve symbolic references such as classes, methods, attributes, etc. into direct references

​ Analyze various symbol references in the constant pool of class files and convert them into direct references to memory addresses such as pointers and offsets

class initializing

Set the initial value for the class static variable, and execute the static statement block at the same time.

Apply for object memory

Assign default values ​​to member variables

Give class member variables such as int i = 8;

Assign i to 0; i=0;

Call constructor

Assign initial values ​​to member variables in order

i =8;

Execution construction method

2 objects are stored in memory layout ? How many characters does Object o = new Object() occupy in memory ?

The size of the object and the memory layout have a lot to do with the implementation and settings of the virtual machine, so first check the default configuration information of the virtual machine and observe the virtual machine configuration

java -XX:+PrintCommandLineFlags -version

 

-XX:InitialHeapSize initial heap size

-XX:MaxHeapSize maximum heap size

-XX:+UseComparessedClassPointers is related to the object header

-XX:+UseCOmparessedOops is related to object memory layout

-XX:+UseParallelGC is related to GC

Classification of java data types

Data types in java are mainly divided into primitive types, classes, interfaces, and arrays. The primitive types include int, long, double, char, byte and other primitive types. The packaging types corresponding to the primitive types, such as Integer, Long, and Double, are classified into classes in Java. Here, interfaces are also classified into classes for the convenience of description. So in general, the data types of java are mainly classified into three types of primitive types, classes and arrays. Among them, only classes and arrays are created in the form of objects in the java virtual machine, so the objects we say usually refer to instances of classes and arrays.

The composition of the object

According to the description in the Java virtual machine specification: Java objects are divided into three parts: Object Header, instance data, and padding. As shown in the figure:

Below we take the object (Ojbect) as an example to analyze the composition of each part. Arrays are similar to objects, except that the length of the array length is 4 bytes in the header of the object.

Object Header:

It is known from the picture that the object header is divided into two parts: Mark Word and Class Pointer.

Mark Word stores the object's hashCode, GC information, and lock information. Class Pointer stores a pointer to the information of the class object. On the 32-bit JVM, the size of the object header is 8 bytes, and the 64-bit JVM is 16 bytes. The two types of Mark Word and Class Pointer each occupy half of the space.

The following is a picture showing the memory distribution of the object header on the 32-bit JVM, which is easy to understand.

 

64th

ClassPointer pointer

There is a compressed pointer option on the 64-bit JVM- ClassPointer pointer: -XX:+UseCompressedClassPointers is 4 bytes and is not turned on to 8 bytes . After opening, the Class Pointer part will be compressed to 4 bytes, and the object header size will be reduced to 12 bytes.

Instance data:

Reference type: -XX:+UseCompressedOops is 4 bytes and is not turned on as 8 bytes Oops Ordinary Object Pointers

Padding aligned, multiples of 8

This can be verified by the program. Today, the idea verification program has not been adjusted, and the explanation will be added next time.

 

 

 

Guess you like

Origin blog.csdn.net/huzhiliayanghao/article/details/106876053
Recommended