当前位置:网站首页>The first word of JVM -- detailed introduction to JVM and analysis of runtime data area
The first word of JVM -- detailed introduction to JVM and analysis of runtime data area
2022-07-01 14:44:00 【Procedural ape with hair!】
1.JVM What is it?
JVM(Java Virtual Machine) yes Jvm A specification for virtual machines .
C:\Users\pc>java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
When installed JDK in the future , You can see that the default is installed HotSpot The virtual machine ,Java The program needs to run on a virtual machine , Different platforms have their own virtual machines , therefore Java Language is a cross platform language .
This brings out Java Characteristics of language : A compilation , Multiple execution , Cross platform , Security (1. Discard pointer 2、GC Garbage collection mechanism :( Automatically release and recycle the memory of objects that are not used for a long time ))
structure :JDK & JRE & JVM Structure diagram of the three
- jvm Distribution chart
Input class File to virtual machine , Virtual machine output of different platforms CPU Instructions . Memory 、 controller 、 Arithmetic unit by JVM Run time data area
2. One java The compilation and running process of the file
Tools :javac compile 、javap Decompile
Java Source file -> Javac Translate it into class file -> JVM Virtual machine running Class The file is actually the machine code converted to the corresponding operating system -> Major operating systems
2.1 javac
Javac Convert the source file to bytecode ( Lexical analysis , Syntax analysis , Semantic analysis , Bytecode generation code )
class file A storage binary file , Deposit 16 Hexadecimal byte
2.2 javap
take class Decompile file to cpu Instructions
// Original code
static int num = 1;
static final int a1 = 2;
int a2 = 2;
// adopt javap -p -v xxx.class Decompiled results
// Will open a in the heap memory num=0 Space
static int num;
descriptor: I
// jurisdiction
flags: ACC_STATIC
static final int a1;
descriptor: I
flags: ACC_STATIC, ACC_FINAL
// Inform the virtual machine to assign values to static variables It means that the value has been initialized in the preparation stage
ConstantValue: int 2
// Not static The instance variable of the variable will be assigned to java Pile up In the initialization phase, it is initiated by the class constructor
int a2;
descriptor: I
3. Class loading mechanism
Purpose : take class Class is loaded into the memory space of the virtual machine
3.1 load Loading
find class The full path of the file , Then load it into memory . The specific implementation is implemented by the class loader ClassLoader
Overview of class loader types
Class loader | function |
---|---|
Bootstrap ClassLoader Start class loader | load $JAVA_HOME in jre/lib/rt.jar Everything in it class perhaps Xbootclasspath Option specified jar package |
ExClassLoader Extend the classloader | load java Some of the extensions in the platform jar package , Include $JAVA_HOME in jre/lib/*.jar perhaps -Djava.ext-dirs Specify the package under the directory |
AppClassLoader system class loader | load classpath Specified in the jar Package and Djava.class.path Specify the classes under the directory and jar package |
Custom ClassLoader Custom class loaders | adopt ClassLoader Custom loading of subclasses class, It belongs to the application customized according to its own needs ClassLoader, Such as Tomcat,Jboss Will be implemented according to the specification ClassLoader |
Class loading mode
- Overall responsible for
- As long as a loader is responsible for a certain class , Then the class loader is responsible for the parent and child classes of this class
- Parent delegation
- Determine whether the top-level class has been loaded
- Caching mechanisms
- A class with the same name will only be loaded once All must be used to cache
Custom class loaders java Realization
Realization ClassLoader, Loading classes cannot be placed under the classpath ( Otherwise it will be AppClassLoader load )
3.2 link Linking
verification Verify: Verify the correctness of the class
Get ready Prepare: Assign default values to static fields of a class or interface
private static int a =10; At this time, the default value will be assigned first 0
analysis Resolve: Dynamically convert symbolic references in the running constant pool to direct references ( Physical memory address )
3.3 initialization Initalization
Attach the real value to the variable , For example, for the above static int a The assignment is 10
What triggers initialization
- new
- Accessing static variables of a class or interface
- Calling static methods of a class
- Reflection
- Initializing a subclass of a class
- Virtual machine specifies the startup class
4.JVM Run time data area
According to the preceding jvm Distribution chart , Divided into 5 Regions , Stack 、 Pile up 、 Program counter 、 Method area 、 Native Method Stack
41. Method area Method Area
- java Thread shared area , Life cycle and process binding
- Store metadata information of the class , Template information
- When memory is insufficient
OutOfMeoryError: Metespace
- The method area stores content that will not be easily changed , stay jdk8 Is also called meta space
Metaspace
- Runtime constant pool Run-Time Constant pool
- String str = “hello” hello In the constant pool , Can be quoted more
- String str = new String(“hello”) 1.7 before hello In heap memory ,1.8 after String Objects are stored in heap memory ,hello Stored in the string constant pool
4.2 Pile up Heap
- The largest thread sharing area of the virtual machine , Life cycle and process binding
- When memory is insufficient
OutOfMeoryError: Java heap space
, adopt-Xms20M -Xmx20M
Set heap memory size - Instance object of class or member variable , for example new String(“hello”) String Object in heap memory
- JDK1.8 Have already put String The constant pool is moved from the method area to the heap , that new String(“hello”) Two objects will be generated ,String Objects are stored in heap memory ,hello Stored in the string constant pool , If the constant pool exists, it will not be created
4.3 Stack Stacks
- Thread private , By preemption CPU Time slice to execute stack frame
- Stack frame is the smallest working unit , The way is to press the stack 、 Out of the stack , Stack structure first, then out 、 Last in, first out
- Recursion leads to insufficient stack depth and will report
StackOverfolwError
, The default size of stack frame depth 1M, Can pass-Xss 128k
modify - Stack frame press stack stack thread mian() -> a() ->b() -> c(),mian() It must be the last execution , It must be first in and last out
4.4 Composition of stack frame
public static int calc(int num1,num2){
num1 = 2;
int res = num1 + num2;
return res;
}
calc(1,8);
// adopt javap Decompile content Look at the calc Stack frame
public static int calc(int, int);
descriptor: (II)I
flags: ACC_PUBLIC, ACC_STATIC
Code:
// The stack of operands 2 individual local variable 3 individual Parameter values 2 individual
stack=2, locals=3, args_size=2
0: iconst_2 //push One int Value is pushed into the operation stack 2
1: istore_0 // Assign the value of the operand stack out of the stack to the... Of the local variable 0 individual num1=2
2: iload_0 // The first 0 The values of local variables are pushed into the operand stack 2
3: iload_1 // The first 1 The values of local variables are pushed into the operand stack 8
4: iadd // Perform an addition operation 10
5: istore_2 // Assign the value of the operand stack out of the stack to the... Of the local variable 2 individual res = 10
6: iload_2 // The value of the second local variable is pressed against the operand stack res
7: ireturn // The second local variable comes out of the operand stack return 10 Then return the address to main Stack frame
- Local variable table , Storing local variables
- The stack of operands , A temporary stack used to store operations
- Dynamic links , Convert a symbolic reference to a direct reference
- Method return , Continue with the following method
java One line of code in , It may be multiple assembly instructions
4.5 Program calculator PC Register
- Just like the registers in the operating system , When the thread is preempted by other threads during execution, the time slice is executed first , You need to record the method and location being executed
- Thread private
4.6 Native Method Stack Native Method Stacks
- When a thread executes a method, it needs to call local Native Method , There is no such method JVM There is no maintenance , So every thread has a local method stack , The place called is linked
5. What does it do to create an object in memory ?
- First set the specified class File loaded into memory
- perform main When the method is used , In the stack memory, it is opened up to belong to main Stack space of thread , Assign to a variable p
- new Keyword to apply for an instance object space from heap memory , Allocate a memory address value
- Space allocation of attributes in the instance object space , And default initialization
- Dynamically convert symbolic references to direct references for attributes in space
- Initialize the construction code block of the instance , Call the constructor of the instance class , Initialize the constructor
- Assign the first address to p,p Variable refers to this instance , Pointed to the object
That's all for this chapter .
Last one :MongoDB July – MongoDB High availability cluster implementation
Next :JVM July – JVM Memory model and garbage collection
Life Geometry ? Die like morning frost . Time is not heavy to , Hua is no longer Yang
边栏推荐
- Research Report on the development trend and competitive strategy of the global diamond suspension industry
- 首届技术播客月开播在即
- [dynamic programming] interval dp:p1005 matrix retrieval
- 从零开发小程序和公众号【第三期】
- 【R语言数据科学】:机器学习常见评估指标
- 关于重载运算符的再整理
- MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
- Today, with the popularity of micro services, how does service mesh exist?
- Research Report on the development trend and competitive strategy of the global CCTV robot industry
- Sqlachemy common operations
猜你喜欢
Guess lantern riddles, not programmers still can't understand?
Problem note - Oracle 11g uninstall
sqlilabs less10
Music player development example (can be set up)
Vnctf2022 open web gocalc0
官宣:Apache Doris 顺利毕业,成为 ASF 顶级项目!
After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]
TDengine 连接器上线 Google Data Studio 应用商店
Basis of target detection (NMS)
Tdengine connector goes online Google Data Studio app store
随机推荐
How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?
The State Administration of Chia Tai market supervision, the national development and Reform Commission and the China Securities Regulatory Commission jointly reminded and warned some iron ores
Research Report on the development trend and competitive strategy of the global aviation leasing service industry
【14. 区间和(离散化)】
Develop small programs and official account from zero [phase III]
tensorflow2-savedmodel convert to pb(frozen_graph)
Basic operation of database
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
sqlilabs less-11~12
What are the requirements for NPDP product manager international certification registration?
[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion
Basic concepts of programming
Opencv interpolation mode
Blog recommendation | in depth study of message segmentation in pulsar
tensorflow2-savedmodel convert to pb(frozen_graph)
户外LED显示屏应该考虑哪些问题?
Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
使用net core 6 c# 的 NPOI 包,讀取excel..xlsx單元格內的圖片,並存儲到指定服務器
数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology