当前位置:网站首页>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 -Xmx20MSet 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 128kmodify - 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
边栏推荐
- Error-tf. function-decorated function tried to create variables on non-first call
- TDengine 连接器上线 Google Data Studio 应用商店
- What are the requirements for NPDP product manager international certification registration?
- 博文推荐 | 深入研究 Pulsar 中的消息分块
- 深度合作 | 涛思数据携手长虹佳华为中国区客户提供 TDengine 强大企业级产品与完善服务保障
- Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
- Phpcms realizes the direct Alipay payment function of orders
- QT capture interface is displayed as picture or label
- tensorflow2-savedmodel convert to pb(frozen_graph)
- 30 Devops interview questions and answers
猜你喜欢

互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码

Build your own website (14)

Don't want to knock the code? Here comes the chance
![[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](/img/30/aea4ae24f418eb971bca77a1d46bef.png)
[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

Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界

Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables

2022-2-15 learning the imitation Niuke project - Section 3 post details

博文推荐 | 深入研究 Pulsar 中的消息分块

Summary of leetcode's dynamic programming 5

Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models
随机推荐
tensorflow2-savedmodel convert to tflite
Research Report on the development trend and competitive strategy of the global pipeline robot inspection camera industry
Provincial election + noi Part XI others
NPDP产品经理国际认证报名有什么要求?
sqlilabs less-11~12
【15. 区间合并】
Problem note - Oracle 11g uninstall
关于软件测试的一些思考
微服务开发步骤(nacos)
如何看待国企纷纷卸载微软Office改用金山WPS?
Research Report on the development trend and competitive strategy of the global ultrasonic scalpel system industry
Research Report on the development trend and competitive strategy of the global high temperature label industry
It's suitable for people who don't have eloquence. The benefits of joining the China Video partner program are really delicious. One video gets 3 benefits
Research Report on development trend and competitive strategy of global consumer glassware industry
Is the futures company found on Baidu safe? How do futures companies determine the regularity
Don't want to knock the code? Here comes the chance
30 Devops interview questions and answers
[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion
保证生产安全!广州要求危化品企业“不安全不生产、不变通”
Provincial election + noi Part 10 probability statistics and polynomials