JVM Part 10_Instantiated memory layout and access positioning of objects_Silicon Valley

1 Instantiation of the object

Dachang interview questions

Meituan:

How are objects stored in the JVM?

What is in the object header information?

Ant Financial:

What's in the java object header?

object instantiation

image-20220611134237743

Determine whether the class corresponding to the object is loaded, linked, initialized

image-20220611144619833

Steps to create an object

The above is to look at the object creation process from the perspective of bytecode, and now analyze it from the perspective of execution steps:

image-20220611140047283

Allocate memory for an object:

First calculate the space occupied by the object, and then allocate a piece of memory in the heap for the new object.

If the instance member variable is a reference variable, only the space for the reference variable is allocated, which is 4 bytes in size

pointer collision

If the memory is regular, then the virtual machine will use the pointer collision (Bump The Pointer) to allocate memory for the object.

It means that all used memory is on one side, free memory is on the other side, and a pointer is placed in the middle as an indicator of the demarcation point. Allocating memory is just moving the pointer to the free side for a distance equal to the size of the inspection. If the garbage collector chooses a compression algorithm based on Serial or ParNew, the virtual machine adopts this allocation method. Generally, when using a collector with a Compact (finishing) process, pointer collisions are used

If the memory is not regular and the used memory and unused memory are interleaved, then the virtual machine will use the free list method to allocate memory for the object.

It means that the virtual machine maintains a list, records which memory blocks are available, finds a large enough space from the list to allocate to the object instance, and updates the content on the list. This allocation method is called "free list" (Free List)".

Initialize the allocated space

Store data such as the class of the object (that is, the metadata information of the class), the HashCode of the object, the GC information of the object, and the lock information in the object header of the object. Exactly how this process is set up depends on the JVM implementation.

Set the object header of the object

Store data such as the class of the object (that is, the metadata information of the class), the HasdCode of the object, the GC information of the object, and the lock information in the object header of the object. Exactly how this process is set up depends on the JVM implementation.

Execute the init method to initialize

From the perspective of the Java program, the initialization has officially begun. Initialize member variables, execute the instantiation code block, call the constructor of the class, and assign the first address of the method in the heap to the reference variable.

Therefore, generally speaking (determined by whether the invokespecial instruction follows in the bytecode), the new instruction will be followed by the execution method to initialize the object according to the programmer's wishes, and such a truly usable object will be completely created.

/**
 * 测试对象实例化的过程
 * ① 加载类元信息 ② 为对象分配内存 ③ 处理并发问题 ④ 属性的默认初始化
 * ⑤ 设置股对象头的信息 ⑥ 属性的显示初始化、代码块中初始化、构造器初始化
 *
 * 给对象的属性赋值的操作
 * ① 属性的默认初始化 ② 显式初始化 / ③ 代码块中初始化 ④ 构造器中初始化
 */

image-20220611200502054

image-20220611200517148

image-20220611200536015

2 Memory layout of objects

image-20220611200329737

image-20220611201138524

3 Access location of objects

image-20220611231334156

How does the JVM access its internal object instance through the object reference in the stack frame?

image-20220611231415372

Handle access (not used by HotSpot)

image-20220611231821906

Direct pointer (used by HotSpot)

image-20220611231755054

Guess you like

Origin blog.csdn.net/weixin_43811294/article/details/125240234