Learning the underlying principles of JVM (3) JVM memory area

The previous article mainly learned the class loading subsystem, this article learns the runtime data area of ​​the JVM

Prev: Learning the underlying principles of JVM (two) loading subsystems

Run-Time Data Areas

JVM designers of different types of content stored in different areas of planning JVM These regions sum up the runtime data area (Run-Time Data Areas) below.

The Java Virtual Machine defines various run-time
data areas that are used during execution of a
program. Some of these data areas are created on
Java Virtual Machine start-up and are destroyed only
when the Java Virtual Machine exits. Other data
areas are per thread. Per-thread data areas are
created when a thread is created and destroyed when
the thread exits.
译:
Java虚拟机定义了在程序执行期间使用的各种运行时数据区域。
其中一些数据区域是在Java虚拟机启动时创建的,只有在Java虚拟机
退出时才会被销毁。有的数据区域是线程独享的,数据区域在创建线程
时创建,在线程退出时销毁。

Runtime data area
Insert picture description here

Method Area

  • The method area is a memory area shared by each thread, created when the virtual machine starts
  • What is stored is the data content that does not change frequently. Basically, frequent GC does not occur
  • Although the Java virtual machine specification describes the method area as a logical part of the heap , it has another alias called Non-Heap (non-heap) to distinguish it from the Java heap. Thus, the heap area is collected separately.
  • Used to store class information , constants , static variables (JDK1.6), code compiled by the just-in-time compiler, etc. that have been loaded by the virtual machine
    • Type information (class meta information): (class Class, interface interface, enumeration Enum, annotation)
      • The full valid name of this class (full name = package name. class name)
      • The complete and effective name of the direct parent of this type. There is no parent for interface or Object
      • Modifiers of this type, public, abstract, final —> 2 to the power of N to represent
    • Field information (domain information)
      • JVM must save all the related information of the type definition in the method area, as well as the declaration order of the fields
      • The relevant information of the domain includes: domain name, domain type, domain modifier
    • Method information
      • Method name of this method
      • The modification characteristics of the method are public, abstract, final —> 2 to the power of N to represent
      • The return type of the method (including void)
      • Method parameter list (type + number)
      • Method of bytecode instructions (bytecodes), operand stack, local variable table
    • Run-time constant pool
      In the bytecode file, the constant pool is included. Load the constant pool in the bytecode file into the method area at runtime, which is the runtime constant pool
      • The stringtable string constant pool is stored in the method area in the JDK1.6 version. In JDK1.7, JDK1.8 moved to the heap
    • Static variables (non-static variables)
      JDK1.6 version, static variables are stored in the method area. In JDK1.7, JDK1.8 moved to the heap
    • JIT just-in-time compiled code
  • When the method area cannot meet the memory allocation requirements, an OutOfMemoryError exception will be thrown
  • The method area is a definition (standard), and the implementation of different versions is different; the implementation of the
    method area before 1.7 is called Perm space (JVM direct control)
    After 1.8, it is called meta space (the operating system's direct memory JVM cannot directly monitor and control .)
  • The method area is created when the JVM starts and is shared by all threads, and its life cycle is consistent with the JVM virtual machine

JDK1.6 version method area storage details:

Insert picture description here

JDK1.7 version method area storage details

Insert picture description here

JDK1.8 version method area storage details:

Insert picture description here

Runtime constant pool

Each run-time constant pool is allocated from the Java Virtual Machine's method area
译:
运行时常量池是分配至方法区的. 即 运行时常量池属于方法区的一部分.(jdk1.6之前属于方法区,1.7之后属于堆区)

That is, the constants in the bytecode file are loaded into the runtime constant pool at runtime

Heap

  • The heap is the largest piece of shared memory managed by the Java virtual machine. It is created when the virtual machine starts and is shared by all threads.
  • ** "Almost" ** All Java object instances and arrays are allocated on the heap
  • Heap is the focus of GC to perform garbage collection.
    Heap space diagram:
    Insert picture description here
  • Why is it divided into two parts: Method Area and Heap?

The Method Area stores data content that does not change frequently. Frequent GC
work basically does not occur . For example, class information, method information, field information, jit compiled code, and
Heap stores data objects generated at runtime. For these Content will frequently be in memory

  • Why is the heap divided into two parts, Old and Young?

The heap is the most important shared working memory in our JAVA running process. The JVM mechanism is a virtual machine that automatically manages shared memory garbage... If we scan the entire heap area every time we collect objects, it will bring a lot of work and performance. loss.

  • Why do we need to distinguish young into three areas: Eden, S0, and S1.

When the memory usage of the young area reaches a certain amount, if it is directly cleaned up at this time, it will bring about the problem of space fragmentation... In addition, the objects in the young area are generally life and death. So most of the content will be cleaned up The surviving objects are put into s0 or s1, which helps garbage collection and cleanup.

  • Why is the ratio of Eden:S0:S1 in the new generation 8:1:1 by default?

IBM's special research shows that about 98% of the objects in the new generation are "live and die". 8:1:1 is a reasonable ratio based on a large number of experiments and data collection and analysis.

Memory exception

  • Heap abnormal OutOfMemoryError
    Adjust program operating parameters. -Xmx20M -Xms20M
  • Method Area Abnormal OutOfMemoryError
    Adjust parameters -XX:MetaspaceSize=30M -XX:MaxMetaspaceSize=30M

Layout of Java objects

Since the contents stored in the heap are Java objects, let's take a look at how objects are laid out in our shared memory.

Insert picture description here
A Java object includes three parts in shared memory: object header, instance data, and alignment padding.
If it is an array object, there will be an additional length of the array length in the object header.

Object header
  • markoop(markword) 8 bytes
  • klassOop(Class pointer) 8 bytes if compressed pointer is enabled (4 bytes)
  • length 4 bytes
Object actual data
  • boolean and byte 1 byte
  • short and char 2 bytes
  • int and float 4 bytes
  • long and double 8 bytes
  • reference 8 bytes
Align padding (may exist)

Ensure that the size of the object is an integer multiple of 8 bytes.
Insert picture description here
The lifetime of a Java object.
Under normal circumstances, newly created objects will be allocated to the Eden area, and some special large objects will be directly allocated to the Old area.

我是一个普通的java对象,我出生在Eden区,在Eden区我还看到和我长的很像的小兄弟,我们在Eden
区中玩了挺长时间。有一天Eden区中的人实在是太多了,我就被迫去了Survivor区的“From”区,自从
去了Survivor区,我就开始漂了,有时候在Survivor的“From”区,有时候在Survivor的“To”区,居无定
所。直到我15岁的时候,爸爸说我成人了,该去社会上闯闯了。于是我就去了年老代那边,年老代
里,人很多,并且年龄都挺大的,我在这里也认识了很多人。在年老代里,我生活了20年(每次GC加
一岁),然后被回收。

The flow chart of the subject from birth to recovery:
Insert picture description here

Are Java objects really like this?
Allocation on the stack
  • Why do we need to allocate
    on the stack? In the process of running a JAVA program, there are actually many reference types of object scope that will not escape to the method, that is, the life cycle of the object is consistent with the method. For this type of object, we Is it necessary to consider that the object is not allocated in the heap space? If so
    , after the thread ends, the object will become garbage in the Heap space, resulting in GC performance consumption.
    Therefore, it is aimed at objects that will not escape the method. JVM It is allowed to disperse the properties of the object (aggregation amount) and allocate it on the stack (thread private, belonging to the stack memory). This method is called allocation on the
    stack. Escape objects on the stack, as shown in the figure.
    Insert picture description here

  • How to enable stack allocation
    -XX:+DoEscapeAnalysis
    -server

  • Turn on the advantages of object escape analysis

    • Genlock elimination
      Insert picture description here

    • Scalar replacement
      Scalar replacement: A quantity that can no longer be decomposed.
      (The eight basic types byte, short, int, long, char, float, double
      , boolean) In addition, references to objects are also scalars. Aggregate is the amount
      that can continue to be decomposed, for example, an object can be decomposed into multiple scalars.
      If you disassemble a Java object and restore its member variables to scattered variables, this is called scalar substitution. The disassembled variables can be analyzed and optimized separately, and space can be allocated on the activity record (stack frame or register) separately; the original object does not need to allocate space as a whole

JVM
-XX:+EliminateAllocations	可以开启标量替换
-XX:+PrintEliminateAllocations	查看标量替换情况
TLAB (thread-local allocation blocks)

In the previous content, we know that objects are allocated on heap-Young-Eden in normal scenarios, and the heap is a globally shared area. When multiple threads operate the heap memory to allocate object space at the same time, synchronization or string is required. Row operations. Do not necessarily bring about concurrency problems of the same space contention. Using synchronization (serial) to bring object allocation will definitely make its allocation efficiency worse (although JVM uses CAS to handle allocation failure).

TLAB is the design to solve this problem. In the Eden area, when a thread starts, a small buffer space private to each thread is opened. Subsequent threads need to create objects as long as the TLAB space can be put down. It will be created in this space. Avoid synchronization (string OK) to improve the efficiency of object allocation.

-XX:+UseTLAB
-XX:TLABSize=512K

Object allocation process

Insert picture description here
Interview questions

  • Stack to heap
public static void main(String[] args){ 
	Object obj = new Object();
}
  • Method area points to the heap
public static Object obj = new Object();	// JDK1.6版本
  • The heap points to the method area
    There is a class point in the markword in the object layout
  • The heap points to
    the lightweight lock of the stack object, and the object header information points to the lock record of the stack space

Java Virtual Machine Stacks (virtual machine stack)

  • The virtual machine stack is the exclusive space of the current execution thread. It exists in the form of a stack data structure;
  • The virtual machine stack is the area where the thread executes, and it stores the calling state of a method in a thread. In other words, the running state of a Java thread is saved by the virtual machine stack hooked by the current thread;
  • Each method executed by a thread is a stack frame in the stack, that is, each method corresponds to a stack frame . When a method is called, a stack frame is pushed into the stack; when a method is called, the stack frame is popped from the stack

Stack frame

The life cycle of the stack frame is related to the call of the method. Each stack frame corresponds to a called method, which can be understood as the running space of a method

  • The stack frame is created when the method is called, and destroyed when the method ends. The end is either a normal end or an abnormal end.
    The schematic diagram of the stack frame:
    Insert picture description here
  • Method return address (Normal Method Invocation Completion and Abrupt Method Invocation Completion): method return address: when a method starts to execute, there are only two ways to exit, one is to encounter the bytecode instruction returned by the method; the other is to meet Exception, and this exception is not handled in the method body. Or understood as the exit after the method is executed
  • Dynamic Linking at runtime (Dynamic Linking): Each stack frame contains a reference to the method to which the stack frame belongs in the runtime constant pool. This reference is held to support dynamic linking during method invocation.
    In the class, they are all symbolic references...method_ref is the same, but we must know the specific execution version of the method during the running or calling phase. So dynamic linking is the method reference pointing to the specific method area. Because the method confirmation must be in the running phase Can only be specified, so it is called dynamic link.
  • Local variables table (Local Variables): local variables defined in the method and parameters of the method of storing the local variable table of variables not directly used in this table, as required, it must be loaded into the operand through the relevant instruction (copy) Used as an operand in the stack.
  • The method local variable table has only 3 local variables in the static method . There are 4 local variables in the instance method ;

In the instance method, the subscript in the local variable table starts from 1. Because the position 0 is the
this object by default . The static method is not.

  • Operand Stack (Operand Stack): The data structure of the stack. The value of the operand is stored by pushing and popping the stack.
    Knowledge point:
    Why design the operand stack
    JVM's instruction set architecture is based on the stack's instruction set architecture
    virtual machine Instruction set architecture There is another instruction set architecture based on register instruction set architecture
    Insert picture description here

Stacked instruction set architecture (JVM)

  • Most of the instructions are zero-address instructions, and their execution process is completely dependent on the operand stack in the Java virtual machine stack.
    Instruction generally consists of two major components: opcode and operand.
    Zero-address instructions have only opcodes and no operands. There are two situations for this instruction: one is that no operand is needed, and the other is that the operand is the default (implicit)
  • Because it is a zero address instruction, the generated instruction space takes up less
  • Not limited to physical hardware resources, strong portability, better cross-platform

Register instruction set architecture (Davlik)

  • The instruction adopts one-address instruction, two-address instruction, and three-address instruction. The execution process depends on the hardware register
  • The instruction space takes up more, but the completed function can be more complicated (it takes fewer instructions to complete more complex operations)
  • The performance is better and the execution is more efficient.
  • The instruction set depends on hardware resources, and portability is limited

Case (Method Reversal)

Insert picture description here

  • iconst_3: Push the int constant of 3 as the top of the operand stack
  • istore_0: Assign the value of type int at the top of the operand stack to the variable in the local variable table with index 0
  • i_load_1: Push the int type value with index 1 in the local variable table to the top of the operand stack
  • iadd: Add the two int type values ​​at the top of the operand stack
  • i_load_2: Push the int type value of the variable with index 2 in the local variable table to the top of the operand stack
  • ireturn: the int type value at the top of the operand stack as the return of the method

Stack exception

Unreasonable recursion in java will definitely cause java virtual machine stack overflow

public class StackOverflowDemo {
    private static int count=0;
    public static void method1(){
		count++;
		System.out.println("stack count: "+count); method1();
    }
    public static void main(String[] args) {
		method1(); }
	}

After running, you can print the depth of the stack, and finally report that stackoverflowExecption
can adjust the size of the virtual machine stack space through -Xss256k

The pc Register (program counter)

We all know that the logical execution of the program must be executed based on the smallest execution unit thread.

There must be multiple threads executing in a JVM process, and whether the content in the thread can have the right to execute is based on the CPU scheduling.

If thread A is executing somewhere and suddenly loses the execution right of the CPU and switches to thread B, then when thread A gets the CPU execution right again, how can it keep the previous state and logic to continue execution? This is the need to maintain the current execution status record in the thread, record the position of the thread execution, so that the execution of the thread can be restored based on this state when the CPU execution right is obtained next time. So The pc Register does this.

Tips:

  • If the thread executes the Java method, the counter records the address of the instruction of the virtual machine bytecode being executed
  • If the local method is being executed, the counter value is (undefined)
  • The only area where OOM does not appear in the runtime data area, there is no garbage collection
  • Each thread has an independent program counter, and the threads do not affect each other

Native Method Stacks

There are many Native methods in the JDK source code. For example, hashcode...These methods are all
logic implemented by C/C++ code.

In the running process of java, we often call the native method of C to complete the function implementation. The method for C/C++ code implementation (that is, these native modified methods) call process. A local method stack is required to save the method The execution operation logic and operation data. The local method stack is such a role.

Execution Engine

Official website architecture diagram
Insert picture description here

问:JAVA到底是解释型语言还是编译型语言?
javac是java的前置编译器,他的主要职责是将java源文件编译成class文件.
在jvm虚拟机中运行过程中,还需要将class文件解析机器能识别的机器码.所以在jvm的执行引擎中存在解释器.
所以java语言兼顾编译型和解释型的特点.

Insert picture description here

mixed mode ---> 指的jvm的执行class过程,存在解释执行和即时编译的即时编译器.

-Xint	-Xcomp	参数指定方式进行编译或者解释执行.

Execution engine icon
Insert picture description here

Interpreter

Translate the class bytecode file line by line into machine code and immediately hand it over to the CPU for execution

JIT Compiler

During the running process, the hot code is detected. The hot code is pre-compiled. The pre-compiled code is stored in the method area. The compiled machine code instructions can be used directly in the process of running the hot code again. No need to interpret or compile again.

Garbage Collection

There is a very important reason for the popularity of the Java language, that is, the management of shared memory. The recycling mechanism for shared objects. In the past C/C++ code, our engineers need to manage the memory of shared objects. Now JVM garbage collection The device automatically helps us take care of the life and death of the object. The area method ares & heap targeted by the garbage collection mechanism

JVM overview diagram

Insert picture description here
Next post: Learning the underlying principles of JVM (four) garbage collection

Guess you like

Origin blog.csdn.net/nonage_bread/article/details/108065454