Writing in the front

Recently I happened to read Teacher Zhou Zhiming’s in-depth Understanding of JAVA Virtual Machine, on the one hand to deepen my impression, on the other hand to share the essence of the book. So according to the chapter only according to their own understanding of the record. thank you

The run-time data area

Defines the data areas that the Java virtual machine manages at the Java runtime

1. Program counter (thread private)

If a Java method is executed, the address of the bytecode instruction being executed is logged.

If it is a local method, the value of the counter is null.

2.Java Virtual Machine stack (thread private)

The virtual stack has the same life cycle as a thread.

When each method is executed, the virtual machine synchronously creates a stack frame.

Storage: local variable table, operand stack, dynamic link, method exit and other information

3. Local method stack (thread private)

The only difference from the Java virtual machine stack is that it stores local methods used by the virtual machine

4.Java heap (Thread sharing)

Store object instances.

There’s an on-stack assignment, scalar substitution. We’ll see in a later chapter

The heap can be divided into multiple threads private allocation buffer TLAB, improve the efficiency of object allocation

Physically discontinuous, logically continuous

5. Method area (thread sharing)

Stores data such as type information, constants, and static variables that have been loaded by VMS

If the method area cannot meet the new memory allocation requirements, OOM will be used, but due to JDk1.8 it will be changed to the local memory implementation of the meta-space. So it’s hard to get OOM due to the method area

6. Runtime constant pool

Part of the method area.

Holds various literal and symbolic references generated by the compiler

7. Direct memory

Note: It is not part of the virtual machine runtime data area, nor is it a memory area defined in the virtual machine specification. Just because it will be used frequently and will result in OOM.

If the direct memory is omitted, the total number of memory areas exceeds the physical memory limit during VM parameter configuration. Causes OOM during dynamic extension.

HotSport VM object

1. Object creation

When the bytecode New instruction is encountered, it will first check whether this parameter can be located in the constant pool to the class symbol reference, and check whether the load, parse, initialization.

② Allocate memory, two ways, pointer collision, free list. Which to use depends on the garbage collector. There are two solutions to the thread-safety problem of allocating memory, namely CAS approach and TLAB approach

③ Set some object header information, such as generation age, hash code, etc

The last step is to initialize the object according to the programmer’s wishes. Such a real object is completely constructed.

Object memory layout

It is divided into three parts: object header, instance data, and alignment padding

The object header consists of two parts: MarkWord and type pointer

MarkWord stores hash codes, generational ages, lock status, locks, biased thread ids, timestamps, and so on

Type pointer A pointer to its type metadata

The main purpose of alignment padding is to ensure that objects are all multiples of 8 bytes

Object access location

Two ways: handle and direct pointer

A handle is simply a pool of addresses that holds the address of an object

A direct pointer holds the address of the object directly

Hotspot takes a direct pointer approach.